Merge branch 'backend' into staging

This commit is contained in:
kusowl 2026-03-05 13:48:24 +05:30
commit a57566c1fe
18 changed files with 269 additions and 15 deletions

0
.ai/mcp/mcp.json Normal file
View File

2
.gitignore vendored
View File

@ -38,6 +38,8 @@ yarn-error.log
testem.log testem.log
/typings /typings
__screenshots__/ __screenshots__/
*.cache
.php-cs-fixer.dist.php
# System files # System files
.DS_Store .DS_Store

View File

@ -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,
]); ]);

View File

@ -14,11 +14,15 @@
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,
public ?bool $isFavorite = null
) {} ) {}
/** /**
@ -29,12 +33,16 @@ 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,
'isFavorite' => $this->isFavorite,
]; ];
} }
@ -43,11 +51,16 @@ 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,
// this column is added by where exists query
isFavorite: $product->favorited_by_exists,
); );
} }
} }

View 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,
]);
}
}

View File

@ -6,14 +6,16 @@
use App\Http\Requests\CreateProductRequest; use App\Http\Requests\CreateProductRequest;
use App\Http\Resources\ProductResource; use App\Http\Resources\ProductResource;
use App\Models\Product; use App\Models\Product;
use App\Queries\GetProductsQuery;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProductController extends Controller 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(); $products = $getProductsQuery->get(Auth::user());
$paginatedDtos = $paginator->through(fn ($product) => ProductDTO::fromModel($product)); $paginatedDtos = $products->through(fn ($product) => ProductDTO::fromModel($product));
return ProductResource::collection($paginatedDtos); return ProductResource::collection($paginatedDtos);
} }
@ -23,7 +25,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) {}

View File

@ -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();

View File

@ -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',
]; ];
} }

View 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,
];
}
}

View File

@ -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,22 @@ 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,
'isFavorite' => $this->resource->isFavorite,
];
} }
} }

View File

@ -2,14 +2,19 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
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\BelongsToMany;
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 +30,31 @@ public function images(): HasMany
{ {
return $this->hasMany(ProductImage::class, 'product_id', 'id'); 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',
];
}
} }

View File

@ -11,7 +11,9 @@
class User extends Authenticatable class User extends Authenticatable
{ {
/** @use HasFactory<\Database\Factories\UserFactory> */ /** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable; use HasFactory;
use Notifiable;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
@ -50,4 +52,14 @@ protected function casts(): array
'role' => UserRoles::class, '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();
}
} }

View 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();
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
use App\Models\ProductCategory; use App\Models\ProductCategory;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;

View File

@ -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');
});
}
};

View File

@ -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');
}
};

View File

@ -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');
});
}
};

View File

@ -1,6 +1,7 @@
<?php <?php
use App\Http\Controllers\AuthenticatedUserController; use App\Http\Controllers\AuthenticatedUserController;
use App\Http\Controllers\FavouriteProductController;
use App\Http\Controllers\ProductCategoryController; use App\Http\Controllers\ProductCategoryController;
use App\Http\Controllers\ProductController; use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProductImagesController; use App\Http\Controllers\ProductImagesController;
@ -15,6 +16,9 @@
Route::get('/user', [AuthenticatedUserController::class, 'show']); Route::get('/user', [AuthenticatedUserController::class, 'show']);
Route::post('/logout', [AuthenticatedUserController::class, 'destroy']); Route::post('/logout', [AuthenticatedUserController::class, 'destroy']);
Route::post('/upload/images', action: [ProductImagesController::class, 'store']); Route::post('/upload/images', action: [ProductImagesController::class, 'store']);
// Favorites
Route::post('/products/{product}/favorite', [FavouriteProductController::class, 'toggle']);
}); });
Route::get('/categories', [ProductCategoryController::class, 'index']); Route::get('/categories', [ProductCategoryController::class, 'index']);
Route::apiResource('products', ProductController::class); Route::apiResource('products', ProductController::class);