6
6
use App \Http \Controllers \Controller ;
7
7
use App \Http \Requests \ProductRequest ;
8
8
use App \Models \Product ;
9
+ use App \Traits \UploadAble ;
9
10
use Illuminate \Http \Request ;
10
11
11
12
class ProductController extends Controller
12
13
{
14
+ use UploadAble;
15
+
13
16
protected $ productRepository ;
14
17
public function __construct (ProductContract $ productRepository ){
15
18
$ this ->productRepository = $ productRepository ;
@@ -33,7 +36,25 @@ public function index()
33
36
*/
34
37
public function store (ProductRequest $ request )
35
38
{
36
- return $ this ->productRepository ->create ($ request ->all ());
39
+ $ image = $ this ->base64ToImage ($ request ->image )['image ' ];
40
+ $ extension = $ this ->base64ToImage ($ request ->image )['extension ' ];
41
+
42
+ $ FileError = $ this ->setImageValidationError ($ extension ,'image ' ,['jpg ' ,'jpeg ' ,'png ' ,'svg ' ]);
43
+
44
+ if ($ FileError ) {
45
+ return response ()->json ([
46
+ 'message ' => $ FileError ['error ' ],
47
+ 'errors ' => [
48
+ $ FileError ['feild ' ] => [ $ FileError ['error ' ] ]
49
+ ]
50
+ ], $ FileError ['status ' ]);
51
+ }
52
+ $ uploadedFile = $ this ->uploadBase64File ($ request ->image , 'products/ ' ,'public ' );
53
+ $ attributes = [
54
+ 'image ' => $ uploadedFile ['name ' ]
55
+ ];
56
+ $ merged = array_merge ($ request ->all (),$ attributes );
57
+ return $ this ->productRepository ->create ($ merged );
37
58
}
38
59
39
60
/**
@@ -47,8 +68,6 @@ public function show(Product $product)
47
68
return $ this ->productRepository ->show ($ product ->id );
48
69
}
49
70
50
-
51
-
52
71
/**
53
72
* Update the specified resource in storage.
54
73
*
0 commit comments