Skip to content

Commit 690d889

Browse files
committed
Brands CRUD Done with App\Traits\Uploadable:Authorization Added:DELETE file from disk
1 parent 69decc7 commit 690d889

File tree

21 files changed

+477
-193
lines changed

21 files changed

+477
-193
lines changed

app/Http/Controllers/Admin/BrandController.php

+63-30
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
namespace App\Http\Controllers\Admin;
44

55
use App\Http\Controllers\Controller;
6+
// use App\Jobs\BrandImageUploading;
67
use App\Models\Brand;
78
use App\Traits\UploadAble;
9+
use Illuminate\Auth\Access\Gate;
810
use Illuminate\Http\Request;
911
use Illuminate\Support\Facades\DB;
1012
use Illuminate\Support\Facades\Storage;
1113
use Image;
1214

1315
class BrandController extends Controller
1416
{
17+
1518
use UploadAble;
1619
/**
1720
* Display a listing of the resource.
1821
*
1922
* @return \Illuminate\Http\Response
2023
*/
24+
public function __construct()
25+
{
26+
$this->authorizeResource(Brand::class, 'brand');
27+
}
2128
public function index(Request $request)
2229
{
2330

@@ -33,11 +40,12 @@ public function index(Request $request)
3340
public function store(Request $request)
3441
{
3542
$this->validate($request , [
36-
'name' => 'bail|required|min:3"unique:brands,slug',
43+
'name' => 'bail|required|min:3|unique:brands,slug',
3744
'logo' => 'required'
3845
]);
46+
// EVENT LISTENER
3947

40-
$image = $this->base64ToImage($request->logo)['data'];
48+
$image = $this->base64ToImage($request->logo)['image'];
4149
$extension = $this->base64ToImage($request->logo)['extension'];
4250
$FileError = $this->setImageValidationError($extension,'logo',['jpg','jpeg','png','svg']);
4351
if ($FileError) {
@@ -48,14 +56,12 @@ public function store(Request $request)
4856
]
4957
], $FileError['status']);
5058
}
51-
52-
53-
54-
$uploadedFile = $this->uploadBase64File($request->logo , 'uploads/brands/','public');
59+
// $uploadedFile = dispatch(new BrandImageUploading($request->logo));
60+
$uploadedFile = $this->uploadBase64File($request->logo , 'brands/','public');
5561

5662
return Brand::create([
5763
'name' => $request->name,
58-
'logo' => '/storage/uploads/brands/'.$uploadedFile['name']
64+
'logo' => '/storage/brands/'.$uploadedFile['name']
5965
]);
6066
}
6167

@@ -101,7 +107,52 @@ public function show(Brand $brand)
101107
*/
102108
public function update(Request $request, Brand $brand)
103109
{
104-
return $brand->update($request->all());
110+
$this->validate($request , [
111+
'name' => 'bail|required|min:3',
112+
'slug' => 'required|unique:brands,slug,'.$request->id,
113+
'logo' => 'required'
114+
]);
115+
if($request->logo !== $brand->logo){
116+
// DELETE OLD IMAGE FIRST
117+
$this->deleteBase64RequestedFile($brand->logo);
118+
// PROCESS :: FOR NEW UPLOAD
119+
$image = $this->base64ToImage($request->logo)['image'];
120+
$extension = $this->base64ToImage($request->logo)['extension'];
121+
$FileError = $this->setImageValidationError($extension,'logo',['jpg','jpeg','png','svg']);
122+
if ($FileError) {
123+
return response()->json([
124+
'message' => $FileError['error'],
125+
'errors' => [
126+
$FileError['feild'] => [ $FileError['error'] ]
127+
]
128+
], $FileError['status']);
129+
}
130+
$uploadedFile = $this->uploadBase64File($request->logo , 'brands/','public');
131+
return $brand->update([
132+
'name' => $request->name ,
133+
'slug' => $request->slug ,
134+
'logo' => '/storage/brands/'.$uploadedFile['name']
135+
]);
136+
}
137+
138+
return $brand->update($request->except('id'));
139+
140+
}
141+
142+
public function multiDelete(Request $request)
143+
{
144+
$this->authorize('multi_delete');
145+
try {
146+
DB::beginTransaction();
147+
foreach ($request->all() as $brand) {
148+
$this->deleteFileFromServer($brand['logo']);
149+
Brand::find($brand['id'])->delete();
150+
}
151+
DB::commit();
152+
return response()->json(['message' => "SUCCESS"], 200);
153+
} catch (\Throwable $th) {
154+
155+
}
105156
}
106157

107158
/**
@@ -112,38 +163,20 @@ public function update(Request $request, Brand $brand)
112163
*/
113164
public function destroy(Brand $brand)
114165
{
115-
// try {
166+
try {
116167
// DB::beginTransaction();
117-
118-
// $logo = preg_replace('/^/\/storage/\//',"",$brand->logo);
119-
// $this->deleteOne($logo);
120-
// WORKED FINE : IF INSERT : logo' => $uploadedFile['name'] 566.png
121-
// $path = "/uploads/brands/".$brand->logo;
122-
// if(Storage::delete($path)){
123-
// $brand->delete();
124-
// return response()->json([
125-
// 'message' => $brand->name." deleted successfully"
126-
// ], 200);
127-
// }
128-
$path = preg_replace("/storage/","", $brand->logo);
129-
130-
if(Storage::delete($path)){
168+
if($this->deleteBase64RequestedFile($brand->logo) ){
131169
$brand->delete();
132170
return response()->json([
133171
'message' => $brand->name." deleted successfully"
134172
], 202);
135173
}
136-
137-
// $this->deleteFileFromServer($brand->logo,true);
138-
// return Storage::disk('public')->delete(public_path().'storage/uploads/brands/1604244861.png');
139-
140-
// $brand->delete();
141174
// DB::commit();
142175
return response()->json([
143176
'message' => $brand->name." deleted failed"
144177
], 404);
145-
// } catch (\Throwable $th) {
178+
} catch (\Throwable $th) {
146179
// DB::rollback();
147-
// }
180+
}
148181
}
149182
}

app/Http/Controllers/Admin/CategoryController.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function store(Request $request)
4444
}
4545
public function multiDelete(Request $request)
4646
{
47-
if (Gate::forUser(Auth::guard('admin')->user())->allows('super')) {
47+
$this->authorize('multi_delete');
4848
try {
4949
DB::beginTransaction();
5050
foreach ($request->all() as $category) {
@@ -56,8 +56,6 @@ public function multiDelete(Request $request)
5656
} catch (\Throwable $th) {
5757
DB::rollback();
5858
}
59-
}
60-
return response()->json(['message' => 'Action Not Permitted'], 403);
6159
}
6260
/**
6361
* Display the specified resource.

app/Jobs/BrandImageUploading.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Brand;
6+
use App\Traits\UploadAble;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class BrandImageUploading implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UploadAble;
16+
public $logo;
17+
/**
18+
* Create a new job instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct($request_logo)
23+
{
24+
$this->logo = $request_logo;
25+
}
26+
27+
/**
28+
* Execute the job.
29+
*
30+
* @return void
31+
*/
32+
public function handle()
33+
{
34+
info("UPLOADING JOB");
35+
return $this->uploadBase64File($this->logo , 'uploads/brands/','public');
36+
}
37+
}

app/Policies/BrandPolicy.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Admin;
6+
use App\Models\Brand;
7+
use Illuminate\Auth\Access\HandlesAuthorization;
8+
9+
class BrandPolicy
10+
{
11+
use HandlesAuthorization;
12+
13+
/**
14+
* Determine whether the user can view any models.
15+
*
16+
* @param \App\Models\Admin $admin
17+
* @return mixed
18+
*/
19+
public function viewAny(Admin $admin)
20+
{
21+
return true;
22+
}
23+
24+
/**
25+
* Determine whether the user can view the model.
26+
*
27+
* @param \App\Models\Admin $admin
28+
* @param \App\Models\Brand $brand
29+
* @return mixed
30+
*/
31+
public function view(Admin $admin, Brand $brand)
32+
{
33+
return true;
34+
}
35+
36+
/**
37+
* Determine whether the user can create models.
38+
*
39+
* @param \App\Models\Admin $admin
40+
* @return mixed
41+
*/
42+
public function create(Admin $admin)
43+
{
44+
return $admin->is_super == 1;
45+
}
46+
47+
/**
48+
* Determine whether the user can update the model.
49+
*
50+
* @param \App\Models\Admin $admin
51+
* @param \App\Models\Brand $brand
52+
* @return mixed
53+
*/
54+
public function update(Admin $admin, Brand $brand)
55+
{
56+
return $admin->is_super == 1 || $admin->is_super == 0;
57+
}
58+
59+
/**
60+
* Determine whether the user can delete the model.
61+
*
62+
* @param \App\Models\Admin $admin
63+
* @param \App\Models\Brand $brand
64+
* @return mixed
65+
*/
66+
public function delete(Admin $admin, Brand $brand)
67+
{
68+
return $admin->is_super == 1;
69+
}
70+
71+
/**
72+
* Determine whether the user can restore the model.
73+
*
74+
* @param \App\Models\Admin $admin
75+
* @param \App\Models\Brand $brand
76+
* @return mixed
77+
*/
78+
public function restore(Admin $admin, Brand $brand)
79+
{
80+
return $admin->is_super == 1 || $admin->is_super == 0;
81+
}
82+
83+
/**
84+
* Determine whether the user can permanently delete the model.
85+
*
86+
* @param \App\Models\Admin $admin
87+
* @param \App\Models\Brand $brand
88+
* @return mixed
89+
*/
90+
public function forceDelete(Admin $admin, Brand $brand)
91+
{
92+
return $admin->is_super == 1;
93+
}
94+
}

app/Providers/AppServiceProvider.php

-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ public function register()
2323
*/
2424
public function boot()
2525
{
26-
//
2726
}
2827
}

app/Providers/AuthServiceProvider.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
66
use Illuminate\Support\Facades\Gate;
77
use App\Models\Admin;
8+
use App\Policies\BrandPolicy;
89
use App\Policies\CategoryPolicy;
910

1011
class AuthServiceProvider extends ServiceProvider
@@ -15,7 +16,10 @@ class AuthServiceProvider extends ServiceProvider
1516
* @var array
1617
*/
1718
protected $policies = [
18-
Admin::class => CategoryPolicy::class,
19+
Admin::class => [
20+
CategoryPolicy::class,
21+
BrandPolicy::class
22+
]
1923
];
2024

2125
/**
@@ -25,10 +29,13 @@ class AuthServiceProvider extends ServiceProvider
2529
*/
2630
public function boot()
2731
{
28-
$this->registerPolicies();
32+
2933
Gate::define('super' , function(Admin $admin){
3034
return $admin->is_super == 1;
3135
});
32-
Gate::define('multiDelete' , 'App\Policies\CategoryPolicy@multiDelete');
36+
Gate::define('multi_delete' , function(Admin $admin){
37+
return $admin->is_super == 1;
38+
});
39+
$this->registerPolicies();
3340
}
3441
}

0 commit comments

Comments
 (0)