public ShoppingCartItemViewModel Add(ShoppingCartItemViewModel shoppingCartItemViewModel) { var shoppingCartItem = Mapper.Map <ShoppingCartItemViewModel, ShoppingCartItem>(shoppingCartItemViewModel); BeginTransaction(); if (!shoppingCartItem.ValidationResult.IsValid) { return(shoppingCartItemViewModel); } _shoppingCartItemService.Add(shoppingCartItem); Commit(); return(shoppingCartItemViewModel); }
public ShoppingCartItemViewModel Add(ShoppingCartItemViewModel shoppingCartItemViewModel) { var shoppingCartItem = new ShoppingCartItem { Id = shoppingCartItemViewModel.Id, Active = shoppingCartItemViewModel.Active, ProductId = shoppingCartItemViewModel.ProductId, ShoppingCartId = shoppingCartItemViewModel.ShoppingCartId }; BeginTransaction(); _shoppingCartItemService.Add(shoppingCartItem); Commit(); return(shoppingCartItemViewModel); }
public ActionResult <string> Post([FromBody] ShoppingCartItemViewModel item) { try { _logger.LogInformation("Received post request"); if (ModelState.IsValid) { _service.Add(item.ShoppingCartId, item.ProductId, item.ProductQuantity); return(Ok("success")); } else { return(BadRequest(ModelState)); } } catch (Exception exception) { _logger.LogError(exception, exception.Message); return(new StatusCodeResult(500)); } }
public ShoppingCartItemViewModel AddItemToCart(int cartNumber, int productId) { ShoppingCartViewModel shoppingCartViewModel = Mapper.Map <ShoppingCart, ShoppingCartViewModel>(_shoppingCartService.Search(x => x.Number == cartNumber) .FirstOrDefault()); if (shoppingCartViewModel == null) { Add(new ShoppingCartViewModel { Number = cartNumber }); shoppingCartViewModel = Mapper.Map <ShoppingCart, ShoppingCartViewModel>(_shoppingCartService.Search(x => x.Number == cartNumber).FirstOrDefault()); } BeginTransaction(); ShoppingCartItem item = new ShoppingCartItem(); item.ProductId = productId; item.ShoppingCartId = shoppingCartViewModel.Id; ShoppingCartItemViewModel shoppingCartItemViewModel = Mapper.Map <ShoppingCartItem, ShoppingCartItemViewModel>(_shoppingCartItemService.Add(item)); Commit(); return(shoppingCartItemViewModel); }
public async Task <ShoppingCartItems> AddItemToCart(ShoppingCartAddItemDto dto, int userId) { try { var cartList = await _shoppingCartService.GetListWithShoppingCartItems_ShoppingCarts(x => x.User_Id == userId && x.Status && !x.Is_Deleted); var cart = cartList.FirstOrDefault(); var product = await _productService.Get(x => x.Id == dto.productId); if (product.InStock < dto.quantity) { throw new Exception("no stock"); } else { if (cart != null) { var cartItems = cart.ShoppingCartItems; if (cartItems != null && cartItems.Any(x => x.Product_Id == dto.productId)) { var cartProduct = await _shoppingCartItemService.Get(x => x.Product_Id == dto.productId && x.ShoppingCart_Id == cart.Id); cartProduct.Quantity += dto.quantity; if (product.InStock < cartProduct.Quantity) { throw new Exception("no stock"); } return(await _shoppingCartItemService.Update(cartProduct)); } else { var itemCartItem = new ShoppingCartItems() { Product_Id = dto.productId, Quantity = dto.quantity, ShoppingCart_Id = cart.Id, }; return(await _shoppingCartItemService.Add(itemCartItem)); } } else { var itemCart = new ShoppingCarts() { User_Id = userId, Status = true, Is_Deleted = false, }; var newCart = await _shoppingCartService.Add(itemCart); var itemCartItem = new ShoppingCartItems() { Product_Id = dto.productId, Quantity = dto.quantity, ShoppingCart_Id = newCart.Id, }; return(await _shoppingCartItemService.Add(itemCartItem)); } } } catch (Exception) { throw; } }