Skip to content

Commit 4452969

Browse files
committed
Product Pagination & filtering added
1 parent b085b69 commit 4452969

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2531
-35
lines changed

app/Contracts/ProductContract.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface ProductContract
1818
// {"page":"1","perPage":"10","orderBy":"created_at","sortBy":"desc"}
1919
public function all();
2020

21+
public function withFilter($request);
22+
2123
/**
2224
* @param int $id
2325
* @return mixed

app/Http/Controllers/Admin/ProductController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function __construct(ProductContract $productRepository){
2424
*
2525
* @return \Illuminate\Http\Response
2626
*/
27-
public function index()
27+
public function index(Request $request)
2828
{
29-
return $this->productRepository->all();
29+
return $this->productRepository->withFilter($request);
3030
}
3131

3232
/**
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Penguin\Controller;
4+
5+
use App\Http\Controllers\Controller;
6+
use Penguin\Models\Shop;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Cache;
9+
10+
class ShopController extends Controller
11+
{
12+
/**
13+
* Display a listing of the resource.
14+
*
15+
* @return \Illuminate\Http\Response
16+
*/
17+
public function index()
18+
{
19+
$KEY = 'Shop';
20+
return Cache::remember($KEY, now()->addMinutes(120), function () {
21+
return Shop::latest()->get();
22+
});
23+
24+
}
25+
26+
27+
/**
28+
* Store a newly created resource in storage.
29+
*
30+
* @param \Illuminate\Http\Request $request
31+
* @return \Illuminate\Http\Response
32+
*/
33+
public function store(Request $request)
34+
{
35+
return Shop::create($request->all());
36+
}
37+
38+
/**
39+
* Display the specified resource.
40+
*
41+
* @param int $id
42+
* @return \Illuminate\Http\Response
43+
*/
44+
public function show(Shop $shop)
45+
{
46+
return $shop;
47+
}
48+
49+
50+
51+
/**
52+
* Update the specified resource in storage.
53+
*
54+
* @param \Illuminate\Http\Request $request
55+
* @param int $id
56+
* @return \Illuminate\Http\Response
57+
*/
58+
public function update(Request $request, Shop $shop)
59+
{
60+
return $shop->update($request->all());
61+
}
62+
63+
/**
64+
* Remove the specified resource from storage.
65+
*
66+
* @param int $id
67+
* @return \Illuminate\Http\Response
68+
*/
69+
public function destroy(Shop $shop)
70+
{
71+
return $shop->delete();
72+
}
73+
}

app/Models/Product.php

+17
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ public function setNameAttribute($value){
2222
$this->attributes['name'] = $value;
2323
$this->attributes['slug'] = \Str::slug($value);
2424
}
25+
public function scopeSearch($query , $q)
26+
{
27+
if($q == null) return;
28+
return $query->where('name','LIKE', "%$q%")
29+
->orWhere('id','LIKE', "%$q%")
30+
->orWhere('created_at','LIKE', "%$q%");
31+
}
32+
public function scopeFilter($query,$request)
33+
{
34+
$perPage = $request->has('perPage') ? (int)$request->query('perPage') : 10;
35+
$orderBy = $request->has('orderBy') ? $request->query('orderBy') : 'created_at';
36+
$sortBy = $request->has('sortBy') ? $request->query('sortBy') : 'desc';
37+
$q = $request->has('q') ? $request->query('q') : '' ;
38+
39+
return $query->search($q)->orderBy($orderBy , $sortBy)->paginate($perPage);
40+
}
41+
2542
}

app/Models/Shop.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Penguin\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Shop extends Model
9+
{
10+
use HasFactory;
11+
}

app/Repositories/ProductRepository.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
class ProductRepository implements ProductContract
1414
{
1515
protected $model;
16+
protected $CACHE_KEY;
1617

1718
public function __construct(Product $model){
1819
$this->model = $model;
@@ -23,9 +24,38 @@ public function all(){
2324
$products = $this->model->latest()->get();
2425
return ProductResource::collection($products);
2526
});
26-
2727
}
28+
public function withFilter($request){
29+
// string $orderBy = 'id', string $sortBy = 'desc';
30+
// {"page":"1","perPage":"10","orderBy":"created_at","sortBy":"desc"}
31+
$this->SET_CACHE($request);
32+
$this->flush($this->CACHE_KEY);
33+
$KEY = $this->getCachekey();
2834

35+
return Cache::remember($KEY, now()->addMinutes(120), function () use($request) {
36+
$products = $this->model::filter($request);
37+
return ProductResource::collection($products);
38+
});
39+
}
40+
public function SET_CACHE($request)
41+
{
42+
$page = $request->has('page') ? (int)$request->query('page') : 1;
43+
$perPage = $request->has('perPage') ? (int)$request->query('perPage') : 10;
44+
$orderBy = $request->has('orderBy') ? $request->query('orderBy') : 'created_at';
45+
$sortBy = $request->has('sortBy') ? $request->query('sortBy') : 'desc';
46+
$q = $request->has('q') ? $request->query('q') :"";
47+
$this->CACHE_KEY = "products.$page.$perPage.$orderBy.$sortBy.$q";
48+
}
49+
public function getCachekey()
50+
{
51+
return $this->CACHE_KEY;
52+
}
53+
public function flush($key)
54+
{
55+
if(cache()->has($key)){
56+
return cache()->forget($key);
57+
}
58+
}
2959
/**
3060
* @param int $id
3161
* @return mixed
@@ -37,6 +67,7 @@ public function all(){
3767
* @return mixed
3868
*/
3969
public function create(array $params){
70+
$this->flush($this->CACHE_KEY);
4071
$product = $this->model->create($params);
4172
return new ProductResource($product);
4273
}
@@ -59,6 +90,7 @@ public function update( $params,$id){
5990
$product = $this->findById($id);
6091
if($product){
6192
$updated = $product->update($params);
93+
$this->flush($this->CACHE_KEY);
6294
return new ProductResource($product);
6395
}
6496
}
@@ -75,11 +107,15 @@ public function findById( $id){
75107
*/
76108
public function delete(int $id){
77109
$product = $this->findByCriteria('id',$id);
78-
if($product){ return $product->delete(); }
110+
if($product){
111+
$this->flush($this->CACHE_KEY);
112+
return $product->delete();
113+
}
79114
}
80115

81116
public function bulk_delete($selected_data)
82117
{
118+
$this->flush($this->CACHE_KEY);
83119
foreach ($selected_data as $product) {
84120
$found = $this->findById($product['id']);
85121
$path = 'products/'.$found['image'];

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
},
3232
"extra": {
3333
"laravel": {
34-
"dont-discover": []
34+
"dont-discover": [],
35+
"providers": [
36+
"Penguin\\Bread\\BreadServiceProvider"
37+
]
3538
}
3639
},
3740
"autoload": {
3841
"psr-4": {
3942
"App\\": "app/",
43+
"Penguin\\Bread\\": "packages/penguin/bread/src/",
4044
"Database\\Factories\\": "database/factories/",
4145
"Database\\Seeders\\": "database/seeders/"
4246
}

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
/*
166166
* Package Service Providers...
167167
*/
168-
168+
Penguin\Bread\BreadServiceProvider::class,
169169
/*
170170
* Application Service Providers...
171171
*/

packages/penguin/bread/composer.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "penguin/bread",
3+
"description": "CRUD Generator for artisan command",
4+
"type": "meta-package",
5+
"authors": [
6+
{
7+
"name": "Samayun Chowdhury",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"minimum-stability": "dev",
12+
"replace": {
13+
"illuminate/support": "self.version"
14+
},
15+
"extra": {
16+
"laravel": {
17+
"providers": [
18+
"Penguin\\Bread\\BreadServiceProvider"
19+
]
20+
}
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Penguin\\Bread\\": "src/"
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Penguin\Bread;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Penguin\Bread\Commands\PenguinInstallCommand;
7+
8+
class BreadServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Register services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
// $this->load(__DIR__.'/Commands');
18+
if ($this->app->runningInConsole()) {
19+
$this->commands([
20+
PenguinInstallCommand::class,
21+
]);
22+
}
23+
}
24+
25+
/**
26+
* Bootstrap services.
27+
*
28+
* @return void
29+
*/
30+
public function boot()
31+
{
32+
info("BOOT FROM BreadServiceProvider");
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Penguin\Bread\Commands;
4+
use Illuminate\Support\Facades\Str;
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class PenguinInstallCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:observer {name}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Penguin observer description';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
/**
35+
* Execute the console command.
36+
*
37+
* @return int
38+
*/
39+
public function handle()
40+
{
41+
$name = $this->argument('name');
42+
$this->observer($name);
43+
return 1;
44+
}
45+
protected function getStub($type){
46+
$stubpath = __DIR__.'/../stubs/'.$type.".stub";
47+
return file_get_contents($stubpath);
48+
}
49+
protected function observer($name){
50+
$name = \Str::ucfirst($name)."Observer";
51+
$this->info($name);
52+
$template = str_replace(
53+
['{{ modelName }}'],
54+
[$name],
55+
$this->getStub('Observer')
56+
);
57+
if(!file_exists(app_path("/Observers/{$name}.php"))){
58+
file_put_contents(app_path("/Observers/{$name}.php"), $template);
59+
return $this->info($name." Observer Created Sucessfully");
60+
}
61+
return $this->warn(" * ".$name." Observer Already exists");
62+
}
63+
64+
65+
}

0 commit comments

Comments
 (0)