From c921d62d16033c2f78ae6517e0aa2b2518640c30 Mon Sep 17 00:00:00 2001 From: Slymee Date: Tue, 19 Mar 2024 13:43:12 +0545 Subject: [PATCH] Cart feature: wip --- app/Http/Controllers/CartController.php | 28 ++++--- app/Models/Cart.php | 13 +++- app/Models/Product.php | 2 +- app/Repositories/CartRepository.php | 74 ++++++++++++++----- .../Interfaces/CartRepositoryInterface.php | 3 + .../2024_03_18_080811_create_carts_table.php | 35 --------- ... 2024_03_19_050909_create_carts_table.php} | 15 ++-- resources/css/cart-items.css | 9 +++ resources/views/userend/cart-items.blade.php | 21 +++++- 9 files changed, 121 insertions(+), 79 deletions(-) delete mode 100644 database/migrations/2024_03_18_080811_create_carts_table.php rename database/migrations/{2024_03_18_080919_create_cart_products_table.php => 2024_03_19_050909_create_carts_table.php} (67%) diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index d36c343..0b2b33a 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -1,10 +1,9 @@ cartRepository->showCartItems($userId); + return view('userend.cart-items', compact('cartItems')); } /** @@ -34,12 +34,12 @@ public function create() /** * Store a newly created resource in storage. */ - public function store(CartItemsRequest $request) + public function store(CartItemsRequest $request): \Illuminate\Http\RedirectResponse { - $this->cartRepository->store($request->all()); - - return redirect()->back()->with('message', 'Product added to cart.'); - + if ($this->cartRepository->store($request->all())){ + return redirect()->back()->with('message', 'Product Added to Cart.'); + } + return redirect()->back()->with('message', 'Product failed to add!'); } /** @@ -69,8 +69,14 @@ public function update(Request $request, Cart $cart) /** * Remove the specified resource from storage. */ - public function destroy(Cart $cart) + public function destroy(Request $request) { - // + $validated = $request->validate([ + 'cart_id' => 'required' + ]); + if ($this->cartRepository->removeFromCart($validated['cart_id'])){ + return redirect()->back()->with('message', 'Removed from cart.'); + } + return redirect()->back()->with('message', 'Could not remove.'); } } diff --git a/app/Models/Cart.php b/app/Models/Cart.php index 8d185ea..8ebf51e 100644 --- a/app/Models/Cart.php +++ b/app/Models/Cart.php @@ -8,15 +8,22 @@ class Cart extends Model { use HasFactory; - protected $table = 'carts'; protected $fillable = [ 'buyer_id', + 'product_id', + 'quantity', + 'amount', ]; - public function products() + public function user() + { + return $this->belongsTo(User::class, 'buyer_id'); + } + + public function product() { - return $this->belongsToMany(Product::class, 'cart_products')->withPivot('quantity'); + return $this->belongsTo(Product::class); } } diff --git a/app/Models/Product.php b/app/Models/Product.php index f714a61..b71c39b 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -58,7 +58,7 @@ public function comments(): MorphMany public function carts() { - return $this->belongsToMany(CartItems::class)->withPivot('quantity'); + return $this->hasMany(Cart::class); } public function getRouteKeyName(): string diff --git a/app/Repositories/CartRepository.php b/app/Repositories/CartRepository.php index 6e09dc7..c0bb4ec 100644 --- a/app/Repositories/CartRepository.php +++ b/app/Repositories/CartRepository.php @@ -6,33 +6,73 @@ use App\Models\Cart; use App\Repositories\Interfaces\CartRepositoryInterface; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; + class CartRepository implements CartRepositoryInterface { - public function store(array $data) + /** + * @param string $userId + * @return mixed + */ + public function showCartItems(string $userId) + { + // TODO: Implement showCartItems() method. + return Cart::where('buyer_id', $userId) + ->with('product') + ->paginate(10); + } + + /** + * @param array $data + * @return bool + */ + public function store(array $data): bool { // TODO: Implement store() method. try { - $cart = Cart::where('buyer_id', $data['buyer_id'])->first(); - - if (!$cart){ - $cart = Cart::create(['buyer_id' => $data['buyer_id']]); - } + $cart = Cart::where('buyer_id', $data['buyer_id']) + ->where('product_id', $data['product_id']) + ->first(); - if ($cart->products->contains($data['product_id'])) { - $existingProduct = $cart->products->find($data['product_id']); - $cart->products() - ->updateExistingPivot($data['product_id'], ['quantity' => $existingProduct->pivot->quantity + $data['quantity']]); - } else { - $cart->products()->attach($data['product_id'], ['quantity' => $data['quantity']]); + if ($cart){ + $cart->quantity += $data['quantity']; + $cart->amount = $cart->quantity * $data['price']; + $cart->save(); + }else{ + Cart::create([ + 'buyer_id' => $data['buyer_id'], + 'product_id' => $data['product_id'], + 'quantity' => $data['quantity'], + 'amount' => ($data['quantity']*$data['price']), + ]); } - return true; - }catch (\Exception $e) { - Log::error('Caught Exception: ' . $e->getMessage()); - Log::error('Exception Details: ' . $e); - throw $e; + }catch (\Exception $e){ + DB::rollBack(); + Log::error('Caught Exception: ' . $e->getMessage()); + Log::error('Exception Details: ' . $e); + return false; + } + } + + /** + * @param string $cartId + * @return bool + */ + public function removeFromCart(string $cartId): bool + { + try { + if (Cart::find($cartId)->delete()){ + return true; } + return false; + }catch (\Exception $e){ + DB::rollBack(); + Log::error('Caught Exception: ' . $e->getMessage()); + Log::error('Exception Details: ' . $e); + return false; } + } } diff --git a/app/Repositories/Interfaces/CartRepositoryInterface.php b/app/Repositories/Interfaces/CartRepositoryInterface.php index 4a26388..f46cf3e 100644 --- a/app/Repositories/Interfaces/CartRepositoryInterface.php +++ b/app/Repositories/Interfaces/CartRepositoryInterface.php @@ -4,5 +4,8 @@ interface CartRepositoryInterface { + public function showCartItems(string $userId); public function store(array $data); + + public function removeFromCart(string $cartId); } diff --git a/database/migrations/2024_03_18_080811_create_carts_table.php b/database/migrations/2024_03_18_080811_create_carts_table.php deleted file mode 100644 index aed77ff..0000000 --- a/database/migrations/2024_03_18_080811_create_carts_table.php +++ /dev/null @@ -1,35 +0,0 @@ -id(); - - $table->unsignedBigInteger('buyer_id'); - $table->foreign('buyer_id') - ->references('id') - ->on('users') - ->onUpdate('cascade') - ->onDelete('cascade'); - - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('carts'); - } -}; diff --git a/database/migrations/2024_03_18_080919_create_cart_products_table.php b/database/migrations/2024_03_19_050909_create_carts_table.php similarity index 67% rename from database/migrations/2024_03_18_080919_create_cart_products_table.php rename to database/migrations/2024_03_19_050909_create_carts_table.php index 0854c66..1377950 100644 --- a/database/migrations/2024_03_18_080919_create_cart_products_table.php +++ b/database/migrations/2024_03_19_050909_create_carts_table.php @@ -11,24 +11,23 @@ */ public function up(): void { - Schema::create('cart_products', function (Blueprint $table) { + Schema::create('carts', function (Blueprint $table) { $table->id(); - $table->unsignedBigInteger('cart_id'); - $table->foreign('cart_id') - ->references('id') - ->on('carts') + $table->unsignedBigInteger('buyer_id'); + $table->foreign('buyer_id')->references('id') + ->on('users') ->onUpdate('cascade') ->onDelete('cascade'); $table->unsignedBigInteger('product_id'); - $table->foreign('product_id') - ->references('id') + $table->foreign('product_id')->references('id') ->on('products') ->onUpdate('cascade') ->onDelete('cascade'); $table->tinyInteger('quantity'); + $table->integer('amount'); $table->timestamps(); }); @@ -39,6 +38,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('cart_products'); + Schema::dropIfExists('carts'); } }; diff --git a/resources/css/cart-items.css b/resources/css/cart-items.css index feeefa6..97432e9 100644 --- a/resources/css/cart-items.css +++ b/resources/css/cart-items.css @@ -43,6 +43,15 @@ justify-content: space-between; } +.cart-item-img>img{ + width: 100px; + height: 80px; +} +.cart-item-info{ + width: 70%; + font-size: 110%; +} + *::-webkit-scrollbar { width: 8px; /* Adjust this value to make the scrollbar thinner or thicker */ } diff --git a/resources/views/userend/cart-items.blade.php b/resources/views/userend/cart-items.blade.php index 867b0d1..5d3ec7f 100644 --- a/resources/views/userend/cart-items.blade.php +++ b/resources/views/userend/cart-items.blade.php @@ -12,10 +12,23 @@