Merge branch 'backend' into staging
This commit is contained in:
commit
a57566c1fe
0
.ai/mcp/mcp.json
Normal file
0
.ai/mcp/mcp.json
Normal file
2
.gitignore
vendored
2
.gitignore
vendored
@ -38,6 +38,8 @@ yarn-error.log
|
||||
testem.log
|
||||
/typings
|
||||
__screenshots__/
|
||||
*.cache
|
||||
.php-cs-fixer.dist.php
|
||||
|
||||
# System files
|
||||
.DS_Store
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
{
|
||||
public function execute(UploadImageDTO $uploadImageDTO): void
|
||||
{
|
||||
$path = $uploadImageDTO->image->store('public/images');
|
||||
$path = $uploadImageDTO->image->store('images', 'public');
|
||||
$uploadImageDTO->product->images()->create([
|
||||
'path' => $path,
|
||||
]);
|
||||
|
||||
@ -14,11 +14,15 @@
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $title,
|
||||
public string $slug,
|
||||
public string $description,
|
||||
public int $actualPrice,
|
||||
public int $listPrice,
|
||||
public ProductCategoryDTO $category,
|
||||
public array $productImages,
|
||||
public ?string $updatedAt = null,
|
||||
public ?string $createdAt = null,
|
||||
public ?bool $isFavorite = null
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -29,12 +33,16 @@ public function toArray(): array
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
'actualPrice' => $this->actualPrice,
|
||||
'listPrice' => $this->listPrice,
|
||||
'category' => $this->category->toArray(),
|
||||
'productImage' => array_map(fn (ProductImageDTO $productImage) => $productImage->toArray(),
|
||||
$this->productImages),
|
||||
'updatedAt' => $this->updatedAt,
|
||||
'createdAt' => $this->createdAt,
|
||||
'isFavorite' => $this->isFavorite,
|
||||
];
|
||||
}
|
||||
|
||||
@ -43,11 +51,16 @@ public static function fromModel(Product $product): self
|
||||
return new self(
|
||||
id: $product->id,
|
||||
title: $product->title,
|
||||
slug: $product->slug,
|
||||
description: $product->description,
|
||||
actualPrice: $product->actual_price,
|
||||
listPrice: $product->list_price,
|
||||
category: ProductCategoryDTO::fromModel($product->category),
|
||||
productImages: $product->images->map(fn (ProductImage $productImage) => ProductImageDTO::fromModel($productImage))->all(),
|
||||
updatedAt: $product->updated_at,
|
||||
createdAt: $product->created_at,
|
||||
// this column is added by where exists query
|
||||
isFavorite: $product->favorited_by_exists,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
36
backend/app/Http/Controllers/FavouriteProductController.php
Normal file
36
backend/app/Http/Controllers/FavouriteProductController.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\FavouriteProductResource;
|
||||
use App\Models\FavouriteProduct;
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class FavouriteProductController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return FavouriteProductResource::collection(FavouriteProduct::all());
|
||||
}
|
||||
|
||||
public function toggle(Request $request, Product $product)
|
||||
{
|
||||
/**
|
||||
* @var User $user
|
||||
*/
|
||||
$user = $request->user();
|
||||
|
||||
$changes = $user->favoriteProducts()->toggle($product);
|
||||
Log::info('hi again');
|
||||
// If changes has any item, that means a product has been attached.
|
||||
$isFavorite = count($changes['attached']) > 0;
|
||||
|
||||
return response()->json([
|
||||
'message' => $isFavorite ? 'Product added to favorites' : 'Product removed from favorites',
|
||||
'isFavorite' => $isFavorite,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -6,14 +6,16 @@
|
||||
use App\Http\Requests\CreateProductRequest;
|
||||
use App\Http\Resources\ProductResource;
|
||||
use App\Models\Product;
|
||||
use App\Queries\GetProductsQuery;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function index(GetProductsQuery $getProductsQuery)
|
||||
{
|
||||
$paginator = Product::query()->with(['category:id,name,slug', 'images:id,path,product_id'])->paginate();
|
||||
$paginatedDtos = $paginator->through(fn ($product) => ProductDTO::fromModel($product));
|
||||
$products = $getProductsQuery->get(Auth::user());
|
||||
$paginatedDtos = $products->through(fn ($product) => ProductDTO::fromModel($product));
|
||||
|
||||
return ProductResource::collection($paginatedDtos);
|
||||
}
|
||||
@ -23,7 +25,12 @@ public function store(CreateProductRequest $request)
|
||||
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) {}
|
||||
|
||||
|
||||
@ -16,11 +16,6 @@ public function store(UploadImageRequest $request, UploadImageAction $action)
|
||||
return response()->json(['message' => 'Image uploaded successfully']);
|
||||
}
|
||||
|
||||
public function show(ProductImages $productImages)
|
||||
{
|
||||
return $productImages;
|
||||
}
|
||||
|
||||
public function destroy(ProductImages $productImages)
|
||||
{
|
||||
$productImages->delete();
|
||||
|
||||
@ -22,7 +22,7 @@ public function authorize(): bool
|
||||
public function rules(): array
|
||||
{
|
||||
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',
|
||||
];
|
||||
}
|
||||
|
||||
23
backend/app/Http/Resources/FavouriteProductResource.php
Normal file
23
backend/app/Http/Resources/FavouriteProductResource.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\FavouriteProduct;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin FavouriteProduct */
|
||||
class FavouriteProductResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
|
||||
'user_id' => $this->user_id,
|
||||
'product_id' => $this->product_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,10 @@
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Data\ProductDTO;
|
||||
use App\Data\ProductImageDTO;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* @property ProductDTO $resource
|
||||
@ -13,6 +15,22 @@ class ProductResource extends JsonResource
|
||||
{
|
||||
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,
|
||||
'isFavorite' => $this->resource->isFavorite,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,14 +2,19 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'actual_price',
|
||||
'list_price',
|
||||
'description',
|
||||
@ -25,4 +30,31 @@ public function images(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProductImage::class, 'product_id', 'id');
|
||||
}
|
||||
|
||||
public function favoritedBy(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'favorite_products');
|
||||
}
|
||||
|
||||
#[Scope]
|
||||
protected function active(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function ($product) {
|
||||
if (empty($product->slug) || $product->isDirty('title')) {
|
||||
$product->slug = Str::slug($product->title);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts()
|
||||
{
|
||||
return [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,7 +11,9 @@
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasFactory;
|
||||
|
||||
use Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@ -50,4 +52,14 @@ protected function casts(): array
|
||||
'role' => UserRoles::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function favoriteProducts()
|
||||
{
|
||||
return $this->belongsToMany(Product::class, 'favorite_products', 'user_id', 'product_id');
|
||||
}
|
||||
|
||||
public function hasFavorited(Product $product): bool
|
||||
{
|
||||
return $this->favoriteProducts()->where('product_id', $product->id)->exists();
|
||||
}
|
||||
}
|
||||
|
||||
27
backend/app/Queries/GetProductsQuery.php
Normal file
27
backend/app/Queries/GetProductsQuery.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Queries;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
final readonly class GetProductsQuery
|
||||
{
|
||||
public function get(?User $user = null): LengthAwarePaginator
|
||||
{
|
||||
return Product::query()
|
||||
->active()
|
||||
->when($user, function (Builder $query) use ($user) {
|
||||
$query->withExists(
|
||||
[
|
||||
'favoritedBy' => fn (Builder $query) => $query->where('user_id', $user->id),
|
||||
]
|
||||
);
|
||||
|
||||
})
|
||||
->with(['category:id,name,slug', 'images:id,path,product_id'])
|
||||
->paginate();
|
||||
}
|
||||
}
|
||||
@ -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');
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('favorite_products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class);
|
||||
$table->foreignIdFor(Product::class);
|
||||
$table->timestamps();
|
||||
$table->unique(['user_id', 'product_id']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('favorite_products');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,29 @@
|
||||
<?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) {
|
||||
$table->boolean('is_active')->default(true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
$table->dropColumn('is_active');
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\AuthenticatedUserController;
|
||||
use App\Http\Controllers\FavouriteProductController;
|
||||
use App\Http\Controllers\ProductCategoryController;
|
||||
use App\Http\Controllers\ProductController;
|
||||
use App\Http\Controllers\ProductImagesController;
|
||||
@ -15,6 +16,9 @@
|
||||
Route::get('/user', [AuthenticatedUserController::class, 'show']);
|
||||
Route::post('/logout', [AuthenticatedUserController::class, 'destroy']);
|
||||
Route::post('/upload/images', action: [ProductImagesController::class, 'store']);
|
||||
|
||||
// Favorites
|
||||
Route::post('/products/{product}/favorite', [FavouriteProductController::class, 'toggle']);
|
||||
});
|
||||
Route::get('/categories', [ProductCategoryController::class, 'index']);
|
||||
Route::apiResource('products', ProductController::class);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user