public ShoppingCart Execute(DTOInsertProductOnShoppingCart model) { var shoppingCart = _getOrCreateShoppingService.Execute(model.UserId); var hasProductOnShoppingCart = shoppingCart.ProductShoppingCarts.FirstOrDefault(x => x.ProductId == model.ProductId); if (hasProductOnShoppingCart == null) { throw new ValidationOnServiceException("O Produto não foi encotrado."); } if (hasProductOnShoppingCart.Quantity == 1) { shoppingCart.ProductShoppingCarts.Remove(hasProductOnShoppingCart); } else { hasProductOnShoppingCart.Quantity -= model.Quantity; } var product = _productRepository.GetById(model.ProductId); product.Quantity += model.Quantity; _productRepository.Update(product); _shoppingCartRepository.Update(shoppingCart); _unitOfWork.Save(); return(shoppingCart); }
public IActionResult AddItemOnCartOpen(DTOInsertProductOnShoppingCart model) { model.UserId = HttpContext.GetUserID(); try { var shoppingcart = _addProductOnShoppingCartService.Execute(model); return(Ok(_mapper.Map <DTOShoppingCart>(shoppingcart))); } catch (ValidationOnServiceException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public ShoppingCart Execute(DTOInsertProductOnShoppingCart item) { var shoppingCart = _getOrCreateShoppingService.Execute(item.UserId); var product = _productRepository.GetById(item.ProductId); if (product.Quantity < item.Quantity) { throw new ValidationOnServiceException("O produto selecionado não possue essa quantidade no estoque."); } product.Quantity -= item.Quantity; var hasProductOnShoppingCart = shoppingCart.ProductShoppingCarts.FirstOrDefault(x => x.ProductId == product.Id); if (hasProductOnShoppingCart != null) { hasProductOnShoppingCart.Quantity += item.Quantity; } else { shoppingCart.ProductShoppingCarts.Add(new ProductShoppingCart { ProductId = product.Id, ShoppingCartId = shoppingCart.Id, Quantity = item.Quantity }); } _productRepository.Update(product); _shoppingCartRepository.Update(shoppingCart); _unitOfWork.Save(); return(shoppingCart); }