Skip to content

Commit

Permalink
Add to Cart completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliarmaganuygun committed Oct 15, 2024
1 parent 2a5bad3 commit 8501799
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BulkyWeb/Areas/Customer/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using System.Diagnostics;
using System.Security.Claims;
using BookBazaar.DataAccess.Repository.IRepository;
using BookBazaar.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BookBazaar.Areas.Customer.Controllers
Expand All @@ -28,10 +30,37 @@ public IActionResult Details(int productId)
ShoppingCart cart = new()
{
Product = _unitOfWork.Product.Get(p => p.Id == productId, includeProperties: "Category"),
Count = 1,
ProductId = productId
};
return View(cart);
}

[HttpPost]
[Authorize]
public IActionResult Details(ShoppingCart shoppingCart)
{
var claimsIdentity = (ClaimsIdentity)User.Identity;
var userId = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier).Value;
shoppingCart.ApplicationUserId = userId;

ShoppingCart cardFromDb = _unitOfWork.ShoppingCart.Get(
u => u.ApplicationUserId == userId && u.ProductId == shoppingCart.ProductId);

if (cardFromDb != null)
{
// card exists in db for that user so we will just update the count
cardFromDb.Count += shoppingCart.Count;
_unitOfWork.ShoppingCart.Update(cardFromDb);
}
else
{
_unitOfWork.ShoppingCart.Add(shoppingCart);
}

_unitOfWork.Save();
return RedirectToAction(nameof(Index));
}

public IActionResult Privacy()
{
Expand Down

0 comments on commit 8501799

Please sign in to comment.