public int GetInventory(CartVariantMappingDTO cartVariantMappingDTO) { int Inventory = shoppingCartEntities.Variants.Where(c => c.ID == cartVariantMappingDTO.VariantID).Select(p => p.Inventory).FirstOrDefault(); Debug.WriteLine(shoppingCartEntities.Variants.Where(c => c.ID == cartVariantMappingDTO.VariantID).Select(p => p.Inventory).FirstOrDefault()); return(Inventory); }
private bool isItemPresent(CartVariantMappingDTO cartVariantMappingDTO) { CartVariantMapping cart = shoppingCartEntities.CartVariantMappings.Where(c => c.CartID == cartVariantMappingDTO.UserID && c.VariantID == cartVariantMappingDTO.VariantID).FirstOrDefault(); Debug.WriteLine(shoppingCartEntities.CartVariantMappings.Where(c => c.CartID == cartVariantMappingDTO.UserID && c.VariantID == cartVariantMappingDTO.VariantID).FirstOrDefault()); if (cart != null) { return(true); } else { return(false); } }
public int GetOrderLimit(CartVariantMappingDTO cartVariantMappingDTO) { Guid productID = shoppingCartEntities.Variants.Where(c => c.ID == cartVariantMappingDTO.VariantID).Select(p => p.ProductID).FirstOrDefault(); var OrderLimit = shoppingCartEntities.Products.Where(p => p.ID == productID).Select(o => o.OrderLimit).FirstOrDefault(); Debug.WriteLine(shoppingCartEntities.Variants.Where(c => c.ID == cartVariantMappingDTO.VariantID).Select(p => p.ProductID).FirstOrDefault()); Debug.WriteLine(shoppingCartEntities.Products.Where(p => p.ID == productID).Select(o => o.OrderLimit).FirstOrDefault()); if (OrderLimit == null) { int inventoryValue = GetInventory(cartVariantMappingDTO); return(inventoryValue); } return(OrderLimit.GetValueOrDefault()); }
public int PresentInCart(CartVariantMappingDTO cartVariantMappingDTO) { int quantity = shoppingCartEntities.CartVariantMappings.Where(c => c.CartID == cartVariantMappingDTO.UserID && c.VariantID == cartVariantMappingDTO.VariantID) .Select(q => q.Quantity).FirstOrDefault(); Debug.WriteLine(shoppingCartEntities.CartVariantMappings.Where(c => c.CartID == cartVariantMappingDTO.UserID && c.VariantID == cartVariantMappingDTO.VariantID) .Select(q => q.Quantity).FirstOrDefault()); if (quantity == 0) { return(quantity); } return(quantity); }
public ActionResult AddItem([Bind(Include = "VariantID, ProductID, Quantity, OrderLimit, Inventory")] CartViewModel cartViewModel) { CartMessageViewModel cartMessageViewModel = new CartMessageViewModel(); if (ModelState.IsValid) { CartVariantMappingDTO cartVariantMappingDTO = CartMapper.Map <CartViewModel, CartVariantMappingDTO>(cartViewModel); cartVariantMappingDTO.UserID = new Guid(Session["UserID"].ToString()); try { cartBusinessContext.AddItem(cartVariantMappingDTO); cartMessageViewModel.SuccessMessage = "Item Successfuly Added to Cart"; return(View(cartMessageViewModel)); } catch (ItemAlreadyInCartException ex) { cartMessageViewModel.ErrorMessages.Add("Item is already present in the cart"); return(View(cartMessageViewModel)); } catch (OrderLimitExceededException) { cartMessageViewModel.ErrorMessages.Add("Order Quantity Exceeded the limit"); return(View(cartMessageViewModel)); } catch (QuantityUnavailableException) { cartMessageViewModel.ErrorMessages.Add("Requested Quantity is UnAvailable"); return(View(cartMessageViewModel)); } catch (Exception ex) { return(View("InternalError")); } } else { foreach (var modelState in ModelState.Values) { foreach (var err in modelState.Errors) { cartMessageViewModel.ErrorMessages.Add(err.ErrorMessage.ToString()); } } return(View(cartMessageViewModel)); } }
public void AddToCart(CartVariantMappingDTO cartVariantMappingDTO) { CartVariantMapping cart = new CartVariantMapping { ID = Guid.NewGuid(), CartID = cartVariantMappingDTO.UserID, VariantID = cartVariantMappingDTO.VariantID, Quantity = cartVariantMappingDTO.Quantity, SellingPrice = cartVariantMappingDTO.SellingPrice }; if (isItemPresent(cartVariantMappingDTO)) { cart = shoppingCartEntities.CartVariantMappings.Where(c => c.CartID == cartVariantMappingDTO.UserID && c.VariantID == cartVariantMappingDTO.VariantID).FirstOrDefault(); cart.Quantity = cartVariantMappingDTO.Quantity; } else { shoppingCartEntities.CartVariantMappings.Add(cart); } shoppingCartEntities.SaveChanges(); }
public void AddItem(CartVariantMappingDTO cartVariantMappingDTO) { try { int totalQuantity = cartDatabaseContext.PresentInCart(cartVariantMappingDTO) + cartVariantMappingDTO.Quantity; int inventoryValue = cartDatabaseContext.GetInventory(cartVariantMappingDTO); int orderLimitValue = cartDatabaseContext.GetOrderLimit(cartVariantMappingDTO); if (totalQuantity > orderLimitValue) { throw new OrderLimitExceededException(); } if (totalQuantity > inventoryValue) { throw new QuantityUnavailableException(); } cartVariantMappingDTO.Quantity = totalQuantity; cartDatabaseContext.AddToCart(cartVariantMappingDTO); } catch (AlreadyPresentException ex) { throw new ItemAlreadyInCartException(); } }