Merge branch 'backend'
This commit is contained in:
commit
194424bfb2
@ -8,7 +8,7 @@
|
|||||||
{
|
{
|
||||||
public function execute(UploadImageDTO $uploadImageDTO): void
|
public function execute(UploadImageDTO $uploadImageDTO): void
|
||||||
{
|
{
|
||||||
$path = $uploadImageDTO->image->store('public/images');
|
$path = $uploadImageDTO->image->store('images', 'public');
|
||||||
$uploadImageDTO->product->images()->create([
|
$uploadImageDTO->product->images()->create([
|
||||||
'path' => $path,
|
'path' => $path,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@ -14,11 +14,14 @@
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
public int $id,
|
public int $id,
|
||||||
public string $title,
|
public string $title,
|
||||||
|
public string $slug,
|
||||||
public string $description,
|
public string $description,
|
||||||
public int $actualPrice,
|
public int $actualPrice,
|
||||||
public int $listPrice,
|
public int $listPrice,
|
||||||
public ProductCategoryDTO $category,
|
public ProductCategoryDTO $category,
|
||||||
public array $productImages,
|
public array $productImages,
|
||||||
|
public ?string $updatedAt = null,
|
||||||
|
public ?string $createdAt = null,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,12 +32,15 @@ public function toArray(): array
|
|||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'title' => $this->title,
|
'title' => $this->title,
|
||||||
|
'slug' => $this->slug,
|
||||||
'description' => $this->description,
|
'description' => $this->description,
|
||||||
'actualPrice' => $this->actualPrice,
|
'actualPrice' => $this->actualPrice,
|
||||||
'listPrice' => $this->listPrice,
|
'listPrice' => $this->listPrice,
|
||||||
'category' => $this->category->toArray(),
|
'category' => $this->category->toArray(),
|
||||||
'productImage' => array_map(fn (ProductImageDTO $productImage) => $productImage->toArray(),
|
'productImage' => array_map(fn (ProductImageDTO $productImage) => $productImage->toArray(),
|
||||||
$this->productImages),
|
$this->productImages),
|
||||||
|
'updatedAt' => $this->updatedAt,
|
||||||
|
'createdAt' => $this->createdAt,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,11 +49,14 @@ public static function fromModel(Product $product): self
|
|||||||
return new self(
|
return new self(
|
||||||
id: $product->id,
|
id: $product->id,
|
||||||
title: $product->title,
|
title: $product->title,
|
||||||
|
slug: $product->slug,
|
||||||
description: $product->description,
|
description: $product->description,
|
||||||
actualPrice: $product->actual_price,
|
actualPrice: $product->actual_price,
|
||||||
listPrice: $product->list_price,
|
listPrice: $product->list_price,
|
||||||
category: ProductCategoryDTO::fromModel($product->category),
|
category: ProductCategoryDTO::fromModel($product->category),
|
||||||
productImages: $product->images->map(fn (ProductImage $productImage) => ProductImageDTO::fromModel($productImage))->all(),
|
productImages: $product->images->map(fn (ProductImage $productImage) => ProductImageDTO::fromModel($productImage))->all(),
|
||||||
|
updatedAt: $product->updated_at,
|
||||||
|
createdAt: $product->created_at,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,12 @@ public function store(CreateProductRequest $request)
|
|||||||
return Product::create($request->validated());
|
return Product::create($request->validated());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(Product $product) {}
|
public function show(string $slug)
|
||||||
|
{
|
||||||
|
$product = Product::where('slug', $slug)->with(['category:id,name,slug', 'images:id,path,product_id'])->firstOrFail();
|
||||||
|
|
||||||
|
return new ProductResource(ProductDTO::fromModel($product));
|
||||||
|
}
|
||||||
|
|
||||||
public function update(Request $request, Product $product) {}
|
public function update(Request $request, Product $product) {}
|
||||||
|
|
||||||
|
|||||||
@ -16,11 +16,6 @@ public function store(UploadImageRequest $request, UploadImageAction $action)
|
|||||||
return response()->json(['message' => 'Image uploaded successfully']);
|
return response()->json(['message' => 'Image uploaded successfully']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(ProductImages $productImages)
|
|
||||||
{
|
|
||||||
return $productImages;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function destroy(ProductImages $productImages)
|
public function destroy(ProductImages $productImages)
|
||||||
{
|
{
|
||||||
$productImages->delete();
|
$productImages->delete();
|
||||||
|
|||||||
@ -22,7 +22,7 @@ public function authorize(): bool
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
|
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:10240',
|
||||||
'product_id' => 'required|exists:products,id',
|
'product_id' => 'required|exists:products,id',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
namespace App\Http\Resources;
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
use App\Data\ProductDTO;
|
use App\Data\ProductDTO;
|
||||||
|
use App\Data\ProductImageDTO;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property ProductDTO $resource
|
* @property ProductDTO $resource
|
||||||
@ -13,6 +15,21 @@ class ProductResource extends JsonResource
|
|||||||
{
|
{
|
||||||
public function toArray(Request $request): array
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
return $this->resource->toArray();
|
return [
|
||||||
|
'id' => $this->resource->id,
|
||||||
|
'title' => $this->resource->title,
|
||||||
|
'slug' => $this->resource->slug,
|
||||||
|
'description' => $this->resource->description,
|
||||||
|
'actualPrice' => $this->resource->actualPrice,
|
||||||
|
'listPrice' => $this->resource->listPrice,
|
||||||
|
'category' => [
|
||||||
|
'name' => $this->resource->category->name,
|
||||||
|
'slug' => $this->resource->category->slug,
|
||||||
|
],
|
||||||
|
'productImages' => array_map(function (ProductImageDTO $productImage) {
|
||||||
|
return Storage::disk('public')->url($productImage->path);
|
||||||
|
}, $this->resource->productImages),
|
||||||
|
'updatedAt' => $this->resource->updatedAt,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,13 @@
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class Product extends Model
|
class Product extends Model
|
||||||
{
|
{
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'title',
|
'title',
|
||||||
|
'slug',
|
||||||
'actual_price',
|
'actual_price',
|
||||||
'list_price',
|
'list_price',
|
||||||
'description',
|
'description',
|
||||||
@ -25,4 +27,13 @@ public function images(): HasMany
|
|||||||
{
|
{
|
||||||
return $this->hasMany(ProductImage::class, 'product_id', 'id');
|
return $this->hasMany(ProductImage::class, 'product_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
static::saving(function ($product) {
|
||||||
|
if (empty($product->slug) || $product->isDirty('title')) {
|
||||||
|
$product->slug = Str::slug($product->title);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('products', function (Blueprint $table) {
|
||||||
|
Schema::table('products', function (Blueprint $table) {
|
||||||
|
$table->string('slug')->unique()->after('title')->nullable();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('products', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('slug');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user