Merge branch 'backend' into staging
This commit is contained in:
commit
136d6cf97e
46
backend/app/Actions/AddProductToCartAction.php
Normal file
46
backend/app/Actions/AddProductToCartAction.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use App\Data\AddToCartDTO;
|
||||||
|
use App\Data\CartDTO;
|
||||||
|
use App\Enums\CartStatus;
|
||||||
|
use App\Models\Product;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
final readonly class AddProductToCartAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Execute the action.
|
||||||
|
*/
|
||||||
|
public function execute(AddToCartDTO $cartData, User $user)
|
||||||
|
{
|
||||||
|
$cart = $user->carts()->active()->firstOrCreate([
|
||||||
|
'status' => CartStatus::Active,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$price = Product::query()->whereId($cartData->productId)->value('actual_price');
|
||||||
|
|
||||||
|
$productInCart = $cart->products()->find($cartData->productId);
|
||||||
|
|
||||||
|
if ($productInCart) {
|
||||||
|
$newQuantity = $productInCart->pivot->quantity + $cartData->quantity;
|
||||||
|
|
||||||
|
$cart->products()->updateExistingPivot($cartData->productId, [
|
||||||
|
'quantity' => $newQuantity,
|
||||||
|
'price' => $price,
|
||||||
|
]);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$cart->products()->attach($cartData->productId, [
|
||||||
|
'quantity' => $cartData->quantity,
|
||||||
|
'price' => $price,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return CartDTO::fromModel($cart->load(['products' => function ($query) {
|
||||||
|
$query->withPivot('quantity', 'price');
|
||||||
|
}]));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
17
backend/app/Actions/GetActiveUserCartAction.php
Normal file
17
backend/app/Actions/GetActiveUserCartAction.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use App\Data\CartDTO;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
final readonly class GetActiveUserCartAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Execute the action.
|
||||||
|
*/
|
||||||
|
public function execute(User $user)
|
||||||
|
{
|
||||||
|
return CartDTO::fromModel($user->carts()->active()->withProducts()->first());
|
||||||
|
}
|
||||||
|
}
|
||||||
21
backend/app/Actions/RemoveProductFromCartAction.php
Normal file
21
backend/app/Actions/RemoveProductFromCartAction.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
final readonly class RemoveProductFromCartAction
|
||||||
|
{
|
||||||
|
public function __construct(private GetActiveUserCartAction $activeCartAction) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the action.
|
||||||
|
*/
|
||||||
|
public function execute(int $productId, User $user)
|
||||||
|
{
|
||||||
|
$cart = $user->carts()->active()->sole();
|
||||||
|
$cart->products()->detach($productId);
|
||||||
|
|
||||||
|
return $this->activeCartAction->execute($user);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
backend/app/Actions/UpdateProductInCartAction.php
Normal file
34
backend/app/Actions/UpdateProductInCartAction.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use App\Data\AddToCartDTO;
|
||||||
|
use App\Data\CartDTO;
|
||||||
|
use App\Models\User;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
final readonly class UpdateProductInCartAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Execute the action.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function execute(AddToCartDTO $cartData, User $user)
|
||||||
|
{
|
||||||
|
$cart = $user->carts()->active()->sole();
|
||||||
|
|
||||||
|
$productInCart = $cart->products()->find($cartData->productId);
|
||||||
|
|
||||||
|
throw_if($productInCart === null, new InvalidArgumentException('Product not found'));
|
||||||
|
|
||||||
|
$cart->products()->updateExistingPivot($cartData->productId, [
|
||||||
|
'quantity' => $cartData->quantity,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return CartDTO::fromModel($cart->load(['products' => function ($query) {
|
||||||
|
$query->withPivot('quantity', 'price');
|
||||||
|
}]));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
33
backend/app/Data/AddToCartDTO.php
Normal file
33
backend/app/Data/AddToCartDTO.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use App\Contracts\InputDataTransferObject;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
final readonly class AddToCartDTO implements InputDataTransferObject
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $productId,
|
||||||
|
public int $quantity
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'productId' => $this->productId,
|
||||||
|
'quantity' => $this->quantity,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromRequest(FormRequest $request): InputDataTransferObject
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
productId: $request->productId,
|
||||||
|
quantity: $request->quantity
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
49
backend/app/Data/CartDTO.php
Normal file
49
backend/app/Data/CartDTO.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use App\Contracts\OutputDataTransferObject;
|
||||||
|
use App\Models\Cart;
|
||||||
|
|
||||||
|
final readonly class CartDTO implements OutputDataTransferObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param CartItemDTO[] $items
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public int $id,
|
||||||
|
public ?int $itemsCount = null,
|
||||||
|
public ?int $totalPrice = null,
|
||||||
|
public array $items = []
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'itemsCount' => $this->itemsCount,
|
||||||
|
'totalPrice' => $this->totalPrice,
|
||||||
|
'items' => $this->items,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromModel(Cart $cart)
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
id: $cart->id,
|
||||||
|
itemsCount: $cart->products->count(),
|
||||||
|
totalPrice: $cart->products->sum(fn ($product) => $product->pivot->price * $product->pivot->quantity),
|
||||||
|
items: $cart->products->map(fn ($product) => new CartItemDTO(
|
||||||
|
id: $product->id,
|
||||||
|
title: $product->title,
|
||||||
|
quantity: $product->pivot->quantity,
|
||||||
|
price: $product->actual_price,
|
||||||
|
subtotal: $product->actual_price * $product->pivot->quantity,
|
||||||
|
image: $product->images->first()->path,
|
||||||
|
))->toArray()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
backend/app/Data/CartItemDTO.php
Normal file
32
backend/app/Data/CartItemDTO.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Data;
|
||||||
|
|
||||||
|
use App\Contracts\OutputDataTransferObject;
|
||||||
|
|
||||||
|
final readonly class CartItemDTO implements OutputDataTransferObject
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $id,
|
||||||
|
public string $title,
|
||||||
|
public int $quantity,
|
||||||
|
public float $price,
|
||||||
|
public float $subtotal,
|
||||||
|
public string $image
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'title' => $this->title,
|
||||||
|
'quantity' => $this->quantity,
|
||||||
|
'price' => $this->price,
|
||||||
|
'subtotal' => $this->subtotal,
|
||||||
|
'image' => $this->image,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
11
backend/app/Enums/CartStatus.php
Normal file
11
backend/app/Enums/CartStatus.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum CartStatus: string
|
||||||
|
{
|
||||||
|
case Active = 'active'; // freshly created
|
||||||
|
case Converted = 'converted'; // user ordered
|
||||||
|
case Abandoned = 'abandoned'; // older than 24hrs
|
||||||
|
case Expired = 'expired'; // left for a long period
|
||||||
|
}
|
||||||
83
backend/app/Http/Controllers/CartController.php
Normal file
83
backend/app/Http/Controllers/CartController.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Actions\AddProductToCartAction;
|
||||||
|
use App\Actions\GetActiveUserCartAction;
|
||||||
|
use App\Actions\RemoveProductFromCartAction;
|
||||||
|
use App\Actions\UpdateProductInCartAction;
|
||||||
|
use App\Data\AddToCartDTO;
|
||||||
|
use App\Http\Requests\AddProductToCartRequest;
|
||||||
|
use App\Http\Requests\RemoveProductFromCartRequest;
|
||||||
|
use App\Http\Requests\UpdateProductInCartRequest;
|
||||||
|
use App\Http\Resources\CartResource;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class CartController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*/
|
||||||
|
public function store(AddProductToCartRequest $request, AddProductToCartAction $addProductAction)
|
||||||
|
{
|
||||||
|
$addToCartData = AddToCartDTO::fromRequest($request);
|
||||||
|
|
||||||
|
$cart = $addProductAction->execute($addToCartData, Auth::user());
|
||||||
|
|
||||||
|
return new CartResource($cart);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
public function show(GetActiveUserCartAction $action)
|
||||||
|
{
|
||||||
|
return new CartResource($action->execute(Auth::user()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(UpdateProductInCartRequest $request, UpdateProductInCartAction $action)
|
||||||
|
{
|
||||||
|
$updateCartData = AddToCartDTO::fromRequest($request);
|
||||||
|
$cart = $action->execute($updateCartData, $request->user());
|
||||||
|
|
||||||
|
return new CartResource($cart);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(RemoveProductFromCartRequest $request, RemoveProductFromCartAction $action)
|
||||||
|
{
|
||||||
|
$user = $request->user();
|
||||||
|
try {
|
||||||
|
$cart = $action->execute($request->productId, $user);
|
||||||
|
|
||||||
|
return new CartResource($cart);
|
||||||
|
} catch (ModelNotFoundException $e) {
|
||||||
|
|
||||||
|
Log::error('No active cart found when removing a product from cart.', [
|
||||||
|
'user' => $user->id,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'No active cart found.',
|
||||||
|
], 404);
|
||||||
|
|
||||||
|
} catch (MultipleRecordsFoundException $e) {
|
||||||
|
Log::error('Multiple active carts found for the user', [
|
||||||
|
'user' => $user->id,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Multiple Active carts found.',
|
||||||
|
], 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
29
backend/app/Http/Requests/AddProductToCartRequest.php
Normal file
29
backend/app/Http/Requests/AddProductToCartRequest.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class AddProductToCartRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'productId' => 'required|exists:products,id',
|
||||||
|
'quantity' => 'required|numeric|min:1',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
28
backend/app/Http/Requests/RemoveProductFromCartRequest.php
Normal file
28
backend/app/Http/Requests/RemoveProductFromCartRequest.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class RemoveProductFromCartRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'productId' => ['required', 'exists:products,id'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
29
backend/app/Http/Requests/UpdateProductInCartRequest.php
Normal file
29
backend/app/Http/Requests/UpdateProductInCartRequest.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateProductInCartRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'productId' => ['required', 'exists:products,id'],
|
||||||
|
'quantity' => ['required', 'min:0', 'max:10'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
19
backend/app/Http/Resources/CartItemCollection.php
Normal file
19
backend/app/Http/Resources/CartItemCollection.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||||
|
|
||||||
|
class CartItemCollection extends ResourceCollection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource collection into an array.
|
||||||
|
*
|
||||||
|
* @return array<int|string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return parent::toArray($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
backend/app/Http/Resources/CartItemResource.php
Normal file
31
backend/app/Http/Resources/CartItemResource.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use App\Data\CartItemDTO;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property CartItemDTO $resource
|
||||||
|
*/
|
||||||
|
class CartItemResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->resource->id,
|
||||||
|
'title' => $this->resource->title,
|
||||||
|
'quantity' => $this->resource->quantity,
|
||||||
|
'price' => $this->resource->price,
|
||||||
|
'subtotal' => $this->resource->subtotal,
|
||||||
|
'image' => Storage::disk('public')->url($this->resource->image),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
30
backend/app/Http/Resources/CartResource.php
Normal file
30
backend/app/Http/Resources/CartResource.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use App\Data\CartDTO;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property CartDTO $resource
|
||||||
|
*/
|
||||||
|
class CartResource extends JsonResource
|
||||||
|
{
|
||||||
|
public static $wrap = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->resource->id,
|
||||||
|
'itemsCount' => $this->resource->itemsCount,
|
||||||
|
'totalPrice' => $this->resource->totalPrice,
|
||||||
|
'items' => CartItemResource::collection($this->resource->items),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
46
backend/app/Models/Cart.php
Normal file
46
backend/app/Models/Cart.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\CartStatus;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Cart extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['user_id', 'status'];
|
||||||
|
|
||||||
|
protected function casts()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'status' => CartStatus::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function products()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Product::class)
|
||||||
|
->withPivot('price', 'quantity')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Scope]
|
||||||
|
protected function active(Builder $query)
|
||||||
|
{
|
||||||
|
return $query->where('status', CartStatus::Active);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Scope]
|
||||||
|
protected function withProducts(Builder $query)
|
||||||
|
{
|
||||||
|
return $query->with(['products' => function ($product) {
|
||||||
|
$product->withPivot('quantity', 'price');
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -36,6 +36,11 @@ public function favoritedBy(): BelongsToMany
|
|||||||
return $this->belongsToMany(User::class, 'favorite_products');
|
return $this->belongsToMany(User::class, 'favorite_products');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function carts()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Cart::class);
|
||||||
|
}
|
||||||
|
|
||||||
#[Scope]
|
#[Scope]
|
||||||
protected function active(Builder $query): Builder
|
protected function active(Builder $query): Builder
|
||||||
{
|
{
|
||||||
|
|||||||
@ -62,4 +62,9 @@ public function hasFavorited(Product $product): bool
|
|||||||
{
|
{
|
||||||
return $this->favoriteProducts()->where('product_id', $product->id)->exists();
|
return $this->favoriteProducts()->where('product_id', $product->id)->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function carts()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Cart::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\CartStatus;
|
||||||
|
use App\Models\User;
|
||||||
|
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::create('carts', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignIdFor(User::class);
|
||||||
|
$table->enum('status', array_column(CartStatus::cases(), 'value'));
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('carts');
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Cart;
|
||||||
|
use App\Models\Product;
|
||||||
|
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::create('cart_product', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignIdFor(Cart::class);
|
||||||
|
$table->foreignIdFor(Product::class);
|
||||||
|
$table->decimal('price', 10, 2);
|
||||||
|
$table->integer('quantity');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['cart_id', 'product_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('cart_product');
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\AuthenticatedUserController;
|
use App\Http\Controllers\AuthenticatedUserController;
|
||||||
|
use App\Http\Controllers\CartController;
|
||||||
use App\Http\Controllers\FavouriteProductController;
|
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;
|
||||||
@ -19,6 +20,10 @@
|
|||||||
|
|
||||||
// Favorites
|
// Favorites
|
||||||
Route::post('/products/{product}/favorite', [FavouriteProductController::class, 'toggle']);
|
Route::post('/products/{product}/favorite', [FavouriteProductController::class, 'toggle']);
|
||||||
|
|
||||||
|
Route::apiSingleton('/cart', CartController::class)
|
||||||
|
->creatable()
|
||||||
|
->destroyable();
|
||||||
});
|
});
|
||||||
Route::get('/categories', [ProductCategoryController::class, 'index']);
|
Route::get('/categories', [ProductCategoryController::class, 'index']);
|
||||||
Route::apiResource('products', ProductController::class);
|
Route::apiResource('products', ProductController::class);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user