Skip to content

Commit 92bb45b

Browse files
committed
Theme(Dark/Light) Added:LanguegeSwitcher improved with image:Product Create Done
1 parent 4cfbb03 commit 92bb45b

31 files changed

+6201
-1940
lines changed

app/Http/Controllers/Admin/ProductController.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use App\Http\Controllers\Controller;
77
use App\Http\Requests\ProductRequest;
88
use App\Models\Product;
9+
use App\Traits\UploadAble;
910
use Illuminate\Http\Request;
1011

1112
class ProductController extends Controller
1213
{
14+
use UploadAble;
15+
1316
protected $productRepository;
1417
public function __construct(ProductContract $productRepository){
1518
$this->productRepository = $productRepository;
@@ -33,7 +36,25 @@ public function index()
3336
*/
3437
public function store(ProductRequest $request)
3538
{
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);
3758
}
3859

3960
/**
@@ -47,8 +68,6 @@ public function show(Product $product)
4768
return $this->productRepository->show($product->id);
4869
}
4970

50-
51-
5271
/**
5372
* Update the specified resource in storage.
5473
*

app/Http/Requests/ProductRequest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ public function rules()
2626
{
2727
return [
2828
'name' => 'required',
29+
'image' => 'required',
2930
'sku' => 'required',
3031
'price' => 'required',
3132
'description' => 'required',
3233
'slug' => !$this->slug ? 'nullable|unique:products,slug' : 'nullable|unique:products,slug,'.$this->id,
33-
'brand_id' => 'required|exists:brands,id'
34+
'brand_id' => 'required|exists:brands,id',
35+
'quantity' => 'required'
3436
];
3537
}
3638
}

app/Http/Resources/ProductResource.php

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Resources;
44

55
use Illuminate\Http\Resources\Json\JsonResource;
6+
use Illuminate\Support\Facades\Storage;
67

78
class ProductResource extends JsonResource
89
{
@@ -23,6 +24,11 @@ public function toArray($request)
2324
"sku" => $this->sku,
2425
"price" => $this->price,
2526
"description" => $this->description,
27+
"quantity" => $this->quantity,
28+
"featured" => $this->featured == 1 ? true : false,
29+
"status" => $this->status == 1 ,
30+
"image" => $this->image ? Storage::url('products/'.$this->image) : Storage::url('products/default.png'),
31+
"description" => $this->description,
2632
"created_at" => $this->created_at->diffForHumans(),
2733
"updated_at" => $this->updated_at->diffForHumans()
2834
];

app/Observers/ProductObserver.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function created(Product $product)
3232
*/
3333
public function updated(Product $product)
3434
{
35+
$this->CACHE_KEY = 'product.'.$product->id;
3536
$this->flush($this->CACHE_KEY);
3637
}
3738

app/Repositories/ProductRepository.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Resources\ProductResource;
66
use App\Models\Product;
77
use Illuminate\Database\Eloquent\ModelNotFoundException;
8+
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Facades\DB;
910

1011
class ProductRepository implements ProductContract
@@ -15,8 +16,12 @@ public function __construct(Product $model){
1516
$this->model = $model;
1617
}
1718
public function all(){
18-
$products = $this->model->latest()->get();
19-
return ProductResource::collection($products);
19+
$KEY = 'products';
20+
return Cache::remember($KEY, now()->addMinutes(120), function () {
21+
$products = $this->model->latest()->get();
22+
return ProductResource::collection($products);
23+
});
24+
2025
}
2126

2227
/**
@@ -36,8 +41,12 @@ public function create(array $params){
3641

3742
public function show(int $id){
3843
$product = $this->findById($id);
44+
$KEY = 'product.'.$id;
3945
if($product){
40-
return new ProductResource($product);
46+
return Cache::remember($KEY, now()->addMinutes(120), function () use($product) {
47+
return new ProductResource($product);
48+
});
49+
4150
}
4251
}
4352
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddImageToProductsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('products', function (Blueprint $table) {
17+
$table->boolean('status')->default(1)->nullable();
18+
// $table->float('discount_price' , 8,2)->nullable();
19+
$table->boolean('featured')->default(0);
20+
$table->string('image')->nullable();
21+
// $table->decimal('weight', 8, 2)->nullable();
22+
// $table->string('color')->nullable();
23+
// $table->string('size')->nullable();
24+
$table->unsignedInteger('quantity')->nullable();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::table('products', function (Blueprint $table) {
36+
//
37+
});
38+
}
39+
}

0 commit comments

Comments
 (0)