public IHttpActionResult UpdateCart(UpdateCartModel updateCartModel) { int ProdQuantityToCart = db.Products.Where(w => w.ProductID == updateCartModel.ProductID).Select(x => x.Quantity).FirstOrDefault(); var cartData = db.Carts.Where(w => w.UserID == updateCartModel.UserID && w.ProductID == updateCartModel.ProductID).FirstOrDefault(); if (cartData != null) { if (updateCartModel.Quantity > ProdQuantityToCart) { return(Ok("Cart Quantity is more than the Product Quantity.Please Choose less quantity")); } else { cartData.Quantity = updateCartModel.Quantity; } cartData.TotalPrice = updateCartModel.TotalPrice; db.Entry(cartData).State = EntityState.Modified; db.SaveChanges(); return(Ok("Success")); } else { return(Ok("Error updating cart, Please try again later.")); } }
public ActionResult UpdateCart(UpdateCartModel model) { var product = _mainRepository.Products .FirstOrDefault(x => x.ProductId == model.ProductId); if (product != null) { var cart = GetCart(); cart.Update(product, model.Quantity); } return(RedirectToAction("Index")); }
private static ObjectResponse <GetCartViewModel> CreateNotFoundValidation(UpdateCartModel model) { return(new ObjectResponse <GetCartViewModel> { Validations = new List <ValidationResponse> { new ValidationResponse { Type = ResponseType.NotFoundedObject, Property = nameof(model) } }, Success = false }); }
public async Task <ActionResult <bool> > UpdateCart([FromBody] UpdateCartModel value) { if (await CartSvc.HasCartID(value.CartID) == false) { ModelState.AddModelError("CartID", "Item in cart does not exists!"); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } await CartSvc.UpdateCart(value.CartID, value.Qty); return(true); }
public IHttpActionResult UpdateCart(UpdateCartModel updateCartModel) { var cartData = db.Carts.Where(w => w.UserID == updateCartModel.UserID && w.ProductID == updateCartModel.ProductID).FirstOrDefault(); if (cartData != null) { cartData.Quantity = updateCartModel.Quantity; cartData.TotalPrice = updateCartModel.TotalPrice; db.Entry(cartData).State = EntityState.Modified; db.SaveChanges(); return(Ok("Success")); } else { return(Ok("Error updating cart, Please try again later.")); } }
public async Task <IActionResult> UpdateCartValue(string externalKey, [FromBody] UpdateCartModel request) { var response = await _service.UpdateCart(externalKey, request); if (response.Type == ResponseType.ValidationErrorOfObject) { return(UnprocessableEntity(response)); } else if (response.Type == ResponseType.NotFoundedObject) { return(NotFound(response)); } else { return(Ok(response)); } }
public ActionResult <UpdateCartModel> UpdateCartItem(int id, [FromBody] UpdateCartModel model) { try { if ((model == null) || (model.Id == 0)) { return(NotFound()); } cartService.UpdateCartItem(model); } catch (DbUpdateConcurrencyException) { if (!CartItemExists(id)) { return(NotFound()); } else { throw; } } return(model); }
public IActionResult UpdateCart(UpdateCartModel cartModel) { if (!cartModel.DiscountCoupon.IsNullEmptyOrWhiteSpace()) { var discountStatus = _cartService.SetDiscountCoupon(ApplicationEngine.CurrentUser.Id, cartModel.DiscountCoupon); if (discountStatus == DiscountApplicationStatus.Success) { return(R.Success.Result); } else { return(R.Fail.With("status", discountStatus).Result); } } else if (cartModel.RemoveCoupon) { _cartService.ClearDiscountCoupon(ApplicationEngine.CurrentUser.Id); return(R.Success.Result); } if (!cartModel.GiftCode.IsNullEmptyOrWhiteSpace()) { //do nothing } if (cartModel.CartItemId.HasValue && cartModel.Quantity.HasValue) { //get the cart var cart = cartModel.IsWishlist ? _cartService.GetWishlist(ApplicationEngine.CurrentUser.Id) : _cartService.GetCart(ApplicationEngine.CurrentUser.Id); var cartItem = cart.CartItems.FirstOrDefault(x => x.Id == cartModel.CartItemId.Value); if (cartItem == null) { return(R.Fail.Result); } //is it to remove it completely if (cartModel.Quantity.Value == 0) { _cartItemService.Delete(cartItem); return(R.Success.Result); } var product = _productService.Get(cartItem.ProductId); if (product == null) { return(R.Fail.Result); } //reset shipping details cart.ShippingFee = 0; cart.ShippingMethodName = string.Empty; cart.ShippingMethodDisplayName = string.Empty; cart.ShippingOptionsSerialized = string.Empty; cart.SelectedShippingOption = string.Empty; _cartService.Update(cart); IActionResult validationResult = null; if (!product.HasVariants) { ValidateQuantityRange(cartModel.Quantity.Value, product, null, out validationResult); } //is the new quantity validated if (validationResult != null) { return(validationResult); } //are we tracking? if (product.TrackInventory) { if (!product.HasVariants) { //this shouldn't be hit ValidateProductQuantity(product, out validationResult); } else { //get the variant var variant = _productVariantService.Get(cartItem.ProductVariantId); ValidateVariantQuantity(cartModel.Quantity.Value, variant, out validationResult); } if (validationResult != null) { return(validationResult); } } cartItem.Quantity = cartModel.Quantity.Value; _cartService.UpdateCart(cart.UserId, cartItem); return(R.Success.Result); } return(R.Fail.Result); }
public async Task <ObjectResponse <GetCartViewModel> > UpdateCart(string externalKey, UpdateCartModel model) { if (model.ChargeValue == null && model.IsActive != false) { return(CreateNotFoundValidation(model)); } GetCartViewModel result = null; using (_transactionManager.Begin()) { if (model.ChargeValue != null) { _updateCartValueCommand.Model = model; if (_updateCartValueCommand.Validations?.Any() == true) { return(new ObjectResponse <GetCartViewModel> { Validations = _updateCartValueCommand.Validations, Success = false }); } result = await _updateCartValueCommand.Execute(externalKey); } if (model.IsActive == false) { result = await _disableCartCommand.Execute(externalKey); if (_disableCartCommand.Validations?.Any() == true) { return(new ObjectResponse <GetCartViewModel> { Validations = _disableCartCommand.Validations, Success = false }); } } _transactionManager.Commit(); } return(new ObjectResponse <GetCartViewModel> { Data = result, Success = true }); }