Skip to content

Commit 4cfbb03

Browse files
committed
Product(v1) CRUD Done- No Image: REQUEST <RESOURCE <POLICY <CONTRACT <REPOSITORY <> SERVICE PROVIDER <PRODUCT OBSERVER
1 parent 90fb676 commit 4cfbb03

Some content is hidden

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

47 files changed

+5478
-2025
lines changed

README.md

Lines changed: 2 additions & 656 deletions
Large diffs are not rendered by default.

app/Contracts/BrandContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface BrandContract
1616
*/
1717
// string $orderBy = 'id', string $sortBy = 'desc', array $columns = ['*']
1818
// {"page":"1","perPage":"10","orderBy":"created_at","sortBy":"desc"}
19-
public function lists($request);
19+
public function lists();
2020

2121
/**
2222
* @param int $id

app/Contracts/ProductContract.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Contracts;
4+
5+
/**
6+
* Interface BrandContract
7+
* @package App\Contracts
8+
*/
9+
interface ProductContract
10+
{
11+
/**
12+
* @param string $orderBy
13+
* @param string $sortBy
14+
* @param array $columns
15+
* @return mixed
16+
*/
17+
// string $orderBy = 'id', string $sortBy = 'desc', array $columns = ['*']
18+
// {"page":"1","perPage":"10","orderBy":"created_at","sortBy":"desc"}
19+
public function all();
20+
21+
/**
22+
* @param int $id
23+
* @return mixed
24+
*/
25+
public function findById(int $id);
26+
public function show(int $id);
27+
28+
/**
29+
* @param array $params
30+
* @return mixed
31+
*/
32+
public function create(array $params);
33+
34+
/**
35+
* @param array $params
36+
* @return mixed
37+
*/
38+
public function update($params, $id);
39+
40+
/**
41+
* @param $id
42+
* @return bool
43+
*/
44+
public function delete(int $id);
45+
46+
public function bulk_delete(array $selected_data);
47+
}

app/Http/Controllers/Admin/BrandController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(BrandContract $brandrepositories)
3131
}
3232
public function index(Request $request)
3333
{
34-
return $this->brandrepositories->lists($request);
34+
return $this->brandrepositories->lists();
3535
}
3636

3737

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
<?php
22

3-
namespace App\Http\Controllers;
3+
namespace App\Http\Controllers\Admin;
44

5+
use App\Contracts\ProductContract;
6+
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\ProductRequest;
58
use App\Models\Product;
69
use Illuminate\Http\Request;
710

811
class ProductController extends Controller
912
{
13+
protected $productRepository;
14+
public function __construct(ProductContract $productRepository){
15+
$this->productRepository = $productRepository;
16+
$this->authorizeResource(Product::class, 'product');
17+
}
1018
/**
1119
* Display a listing of the resource.
1220
*
1321
* @return \Illuminate\Http\Response
1422
*/
1523
public function index()
1624
{
17-
//
18-
}
19-
20-
/**
21-
* Show the form for creating a new resource.
22-
*
23-
* @return \Illuminate\Http\Response
24-
*/
25-
public function create()
26-
{
27-
//
25+
return $this->productRepository->all();
2826
}
2927

3028
/**
@@ -33,9 +31,9 @@ public function create()
3331
* @param \Illuminate\Http\Request $request
3432
* @return \Illuminate\Http\Response
3533
*/
36-
public function store(Request $request)
34+
public function store(ProductRequest $request)
3735
{
38-
//
36+
return $this->productRepository->create($request->all());
3937
}
4038

4139
/**
@@ -46,19 +44,10 @@ public function store(Request $request)
4644
*/
4745
public function show(Product $product)
4846
{
49-
//
47+
return $this->productRepository->show($product->id);
5048
}
5149

52-
/**
53-
* Show the form for editing the specified resource.
54-
*
55-
* @param \App\Models\Product $product
56-
* @return \Illuminate\Http\Response
57-
*/
58-
public function edit(Product $product)
59-
{
60-
//
61-
}
50+
6251

6352
/**
6453
* Update the specified resource in storage.
@@ -67,9 +56,9 @@ public function edit(Product $product)
6756
* @param \App\Models\Product $product
6857
* @return \Illuminate\Http\Response
6958
*/
70-
public function update(Request $request, Product $product)
59+
public function update(ProductRequest $request, Product $product)
7160
{
72-
//
61+
return $this->productRepository->update($request->all(),$request->id);
7362
}
7463

7564
/**
@@ -80,6 +69,11 @@ public function update(Request $request, Product $product)
8069
*/
8170
public function destroy(Product $product)
8271
{
83-
//
72+
return $this->productRepository->delete($product->id);
73+
}
74+
75+
public function bulk_delete(Request $request){
76+
$selected_item = $request->selected_data;
77+
return $this->productRepository->bulk_delete($selected_item);
8478
}
8579
}

app/Http/Requests/ProductRequest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rule;
7+
8+
class ProductRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return true;
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
'name' => 'required',
29+
'sku' => 'required',
30+
'price' => 'required',
31+
'description' => 'required',
32+
'slug' => !$this->slug ? 'nullable|unique:products,slug' : 'nullable|unique:products,slug,'.$this->id,
33+
'brand_id' => 'required|exists:brands,id'
34+
];
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class ProductResource extends JsonResource
8+
{
9+
/**
10+
* Transform the resource into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
// return parent::toArray($request);
18+
return [
19+
"id" => $this->id,
20+
"name" =>$this->name,
21+
"slug" => $this->slug ? $this->slug: "none",
22+
"brand" => ['id' => $this->brand->id, 'name' => $this->brand->name , 'image' => url($this->brand->logo)],
23+
"sku" => $this->sku,
24+
"price" => $this->price,
25+
"description" => $this->description,
26+
"created_at" => $this->created_at->diffForHumans(),
27+
"updated_at" => $this->updated_at->diffForHumans()
28+
];
29+
}
30+
}

app/Models/Product.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@
88
class Product extends Model
99
{
1010
use HasFactory;
11+
protected $guarded = [];
12+
public function brand()
13+
{
14+
return $this->belongsTo('App\Models\Brand');
15+
}
16+
17+
public function setNameAttribute($value){
18+
$this->attributes['name'] = $value;
19+
$this->attributes['slug'] = \Str::slug($value);
20+
}
1121
}

app/Observers/BrandObserver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ public function deleted(Brand $brand)
4949
$this->flush($this->CACHE_KEY);
5050
}
5151

52-
5352
}

app/Observers/ProductObserver.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use App\Models\Product;
6+
7+
class ProductObserver
8+
{
9+
protected $CACHE_KEY = 'products';
10+
private function flush($key)
11+
{
12+
if(cache()->has($key)){
13+
return cache()->forget($key);
14+
}
15+
}
16+
/**
17+
* Handle the product "created" event.
18+
*
19+
* @param \App\Models\Product $product
20+
* @return void
21+
*/
22+
public function created(Product $product)
23+
{
24+
$this->flush($this->CACHE_KEY);
25+
}
26+
27+
/**
28+
* Handle the product "updated" event.
29+
*
30+
* @param \App\Models\Product $product
31+
* @return void
32+
*/
33+
public function updated(Product $product)
34+
{
35+
$this->flush($this->CACHE_KEY);
36+
}
37+
38+
/**
39+
* Handle the product "deleted" event.
40+
*
41+
* @param \App\Models\Product $product
42+
* @return void
43+
*/
44+
public function deleted(Product $product)
45+
{
46+
$this->flush($this->CACHE_KEY);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)