public async Task <RequestResult> Handle(AddItemShoppingCartRequest request, CancellationToken cancellationToken) { var shoppingCart = await _shoppingCartRepository.GetShoppingCartByCustomerIdAsync(_customerId); var item = new ItemShoppingCart(request.ProductId, request.Name, request.Quantity, request.Value, request.Image, shoppingCart?.Id ?? Guid.Empty); if (shoppingCart == null) { shoppingCart = new CustomerShoppingCart(_customerId); shoppingCart.AddItem(item); return(CommitResult(await _shoppingCartRepository.InsertAsync(shoppingCart))); } var itemExists = shoppingCart.HasItem(item.ProductId); if (!shoppingCart.AddItem(item)) { return(ErrorResult(AddItemShoppingCartRequestValidator.InvalidMaxQuantityError)); } if (!itemExists) { await _itemShoppingCartRepository.InsertAsync(item); } return(CommitResult(await _shoppingCartRepository.UpdateAsync(shoppingCart))); }
// // GET: /Cart/ public ActionResult MyCart(int?id) { if (Session["Authentication"] == null) { return(RedirectToAction("LoginModal", "Account")); } else if (id != null) { string uname = Session["UserName"].ToString(); int pid = (int)id; var proexistintable = (from x in db.CustomerShoppingCarts where x.ItemId == id && x.CustomerUserName == uname select x).OrderByDescending(z => z.DateTime).ToList(); if (proexistintable.Count != 0) { proexistintable = (from x in db.CustomerShoppingCarts where x.CustomerUserName == uname select x).OrderByDescending(z => z.DateTime).ToList(); return(View("CartListView", proexistintable)); } else { var ob = new CustomerShoppingCart(); List <ItemTable> itemInfo = (from x in db.ItemTables where x.ItemId == (int)id select x).ToList(); foreach (var n in itemInfo) { ob.ItemName = n.ItemName; ob.ItemImageName = n.ItemImageName; ob.ItemPrice = n.ItemPrice; ob.NoOfItem = 1; } ob.ItemId = pid; ob.CustomerUserName = Session["UserName"].ToString(); ob.DateTime = DateTime.Now; db.CustomerShoppingCarts.Add(ob); db.SaveChanges(); } proexistintable = (from x in db.CustomerShoppingCarts where x.CustomerUserName == uname select x).OrderByDescending(z => z.DateTime).ToList(); return(View("CartListView", proexistintable)); } else { string uname = Session["UserName"].ToString(); var proexistintable = (from x in db.CustomerShoppingCarts where x.CustomerUserName == uname select x).OrderByDescending(z => z.DateTime).ToList(); return(View("CartListView", proexistintable)); } }
public async Task <bool> CreateShoppingCart(ShoppingCartItem cartItem, int customerId) { CustomerShoppingCart shoppingCart = new CustomerShoppingCart { CreateDate = DateTime.Now, CustomerId = customerId, ShoppingCartItems = new List <ShoppingCartItem> { cartItem } }; return(await _provider.GetService <IShoppingCartRepository>().AddAsyncIfNotExists(shoppingCart)); }
public async Task <CustomerShoppingCart> UpdateShoppingCartAsync(CustomerShoppingCart shoppingCart) { var created = await _database.StringSetAsync(shoppingCart.Id, JsonSerializer.Serialize(shoppingCart), TimeSpan.FromDays(30)); if (!created) { return(null); } return(await GetShoppingCartAsync(shoppingCart.Id)); }
public async Task <IActionResult> Details(int id) { var MedicineFromDb = await _db.Medicine.Include(m => m.MedicineType).Include(m => m.MedicineSubtype).Where(m => m.Id == id).FirstOrDefaultAsync(); CustomerShoppingCart customercartObj = new CustomerShoppingCart() { Medicine = MedicineFromDb, MedicineId = MedicineFromDb.Id }; return(View(customercartObj)); }
//Basket action- Can be called for all operations-changes whhile the shopping cart screen is open. increase item count,decrease item count etc. public async Task <bool> Update(ShoppingCartUpdateRequest request) { CustomerShoppingCart shoppingCart = new CustomerShoppingCart(); var item = await GetProduct(request.ProductItem.Barcode); var existingCart = await Get(new ShoppingCartServiceRequest { CustomerId = request.Customer.CustomerId }); ShoppingCartItem cartItem = new ShoppingCartItem { GiftPrice = request.GiftPrice, ItemBasketQuantity = request.ItemBasketQuantity, ProductItem = item }; //müşterinin sepeti yoksa sepet oluştur if (existingCart.ShoppingCartItems == null) { return(await CreateShoppingCart(cartItem, request.Customer.CustomerId)); } int basketExistingItemCount = existingCart.ShoppingCartItems.Where(i => i.ProductItem.Barcode == request.ProductItem.Barcode).Sum(i => i.ItemBasketQuantity); if (item == null || item.StockQuantity < request.ItemBasketQuantity) { return(false); } //ürün sepette yoksa ve req edlen toplamı toplam ürün stoğunu geçmiyorsa if (basketExistingItemCount == 0) { shoppingCart = await _provider .GetService <IShoppingCartRepository>() .PushCartItem(cartItem, request.Customer.CustomerId, Guid.NewGuid()); } //ürün sepette varsa stoğu güncellensin else { existingCart.ShoppingCartItems.Where(i => i.ProductItem.Barcode == request.ProductItem.Barcode).ToList().ForEach(i => i.ItemBasketQuantity = request.ItemBasketQuantity); shoppingCart = await UpdateBasketItemInfo(item, existingCart); } return(shoppingCart.ShoppingCartItems != null ? true : false); }
public async Task <IActionResult> Details(CustomerShoppingCart CartObject) { CartObject.Id = 0; if (ModelState.IsValid) { var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); CartObject.ApplicationUserId = claim.Value; CustomerShoppingCart cartFromDb = await _db.CustomerShoppingCart.Where(c => c.ApplicationUserId == CartObject.ApplicationUserId && c.MedicineId == CartObject.MedicineId).FirstOrDefaultAsync(); if (cartFromDb == null) { await _db.CustomerShoppingCart.AddAsync(CartObject); } else { cartFromDb.Count = cartFromDb.Count + CartObject.Count; } await _db.SaveChangesAsync(); var count = _db.CustomerShoppingCart.Where(c => c.ApplicationUserId == CartObject.ApplicationUserId).ToList().Count(); HttpContext.Session.SetInt32(SD.ssCustomerShoppingCartCount, count); return(RedirectToAction("Index")); } else { var MenuItemFromDb = await _db.Medicine.Include(m => m.MedicineType).Include(m => m.MedicineSubtype).Where(m => m.Id == CartObject.MedicineId).FirstOrDefaultAsync(); CustomerShoppingCart cartObj = new CustomerShoppingCart() { Medicine = MenuItemFromDb, MedicineId = MenuItemFromDb.Id }; return(View(cartObj)); } }
public async Task <IActionResult> Index(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/Customer/Home"); bool isAuthenticated = User.Identity.IsAuthenticated; if (isAuthenticated) { CustomerShoppingCart = new List <CustomerShoppingCart>(); List <ShoppingCart> lstShoppingMirge = new List <ShoppingCart>(); List <ShoppingCart> lstShoppingCart = HttpContext.Session.Get <List <ShoppingCart> >("ssShoppingCart"); string UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; List <CustomerShoppingCart> CustomerShoppingDb = _db.CustomerShoppingCart.Where(x => x.PersonID == UserId).ToList(); if (lstShoppingCart != null && lstShoppingCart.Count > 0) { // string UserId = _userManager.GetUserId(User); foreach (ShoppingCart cartItem in lstShoppingCart) { if (CustomerShoppingDb.Contains(CustomerShoppingDb.Find(x => x.ProductId == cartItem.ProductId))) { // lstShoppingCart.Remove(cartItem); } else { var item = new CustomerShoppingCart() { PersonID = UserId, ProductId = cartItem.ProductId, Quantity = cartItem.Quantity }; CustomerShoppingDb.Add(item); _db.CustomerShoppingCart.Add(item); } } lstShoppingCart = new List <ShoppingCart>(); foreach (var customercart in CustomerShoppingDb) { lstShoppingCart.Add(new ShoppingCart { ProductId = customercart.ProductId, Quantity = customercart.Quantity }); } } else if (CustomerShoppingDb != null && CustomerShoppingDb.Count > 0) { lstShoppingCart = new List <ShoppingCart>(); foreach (var customercart in CustomerShoppingDb) { lstShoppingCart.Add(new ShoppingCart { ProductId = customercart.ProductId, Quantity = customercart.Quantity }); } } await _db.SaveChangesAsync(); HttpContext.Session.Set("ssShoppingCart", lstShoppingCart); } return(LocalRedirect(returnUrl)); // return RedirectToAction("Index", "Home", new { area = "Customer" }); }
public async Task <CustomerShoppingCart> UpdateBasketItemInfo(Product product, CustomerShoppingCart shoppingCart) { var existingBasketItem = shoppingCart.ShoppingCartItems.Where(i => i.ProductItem.Barcode == product.Barcode).ToList(); existingBasketItem.ForEach(i => i.ProductItem = product); if (product.StockQuantity < existingBasketItem.Sum(i => i.ItemBasketQuantity)) { existingBasketItem.ForEach(i => i.ItemBasketQuantity = product.StockQuantity); } return(await _provider.GetService <IShoppingCartRepository>().UpdateAsync(shoppingCart.Id, shoppingCart)); }