public async Task <IActionResult> AddItem([Required] AddItemRequestModel model) { if (!ModelState.IsValid) { _logger.LogInformation("Model state is invalid, returning bad request"); return(BadRequest()); } var user = await _jwtService.GetUserAccountAsync(User); if (user == null) { _logger.LogInformation($"Could not get user account from token {User.Identity.Name}, returning unauthorized"); return(Unauthorized()); } var created = await _mediator.Send(new AddItemApiRequest(user, model)); if (!created) { return(BadRequest()); } return(Ok()); }
public IActionResult AddItemToCart([FromBody] AddItemRequestModel addItemModel) { // verify parameters if (addItemModel == null || addItemModel.ItemId == null || addItemModel.Quantity < 1) { return(BadRequest(new { res = "Invalid parameters" })); } // try to get the product by id ProductModel product = _productService.GetProductById(addItemModel.ItemId); if (product == null) { return(BadRequest(new { err = "Could not add item to the cart" })); } // try to add item to the user's cart Guid userId = GetUserId(); bool itemAdded = _cartService.AddCartItem(userId, addItemModel.ItemId, addItemModel.Quantity); // return an error message if the item wasn't added if (!itemAdded) { return(BadRequest(new { err = "Could not add item to the cart" })); } return(Ok()); }
private static double GetUnitPriceOfItem(AddItemRequestModel item) { var UnitsFactory = new UnitPriceFactory(); IUnitPrice unitPriceOfItem = UnitsFactory.GetUnitPrice(item.ItemType); var unitPrice = unitPriceOfItem.GetUnitPrice(); return(unitPrice); }
private double CheckPromoDiscount(double unitPrice, AddItemRequestModel item) { var promotionForItem = new DiscountsFactory().GetPromotions(item.ItemType); var OnDiscountPrice = (item.ItemQuantity / promotionForItem.NoOfItems) * promotionForItem.DiscountOffered; if (!Flag) { isPromoApplied = OnDiscountPrice > 0; Flag = true; } var OffDiscountPrice = (item.ItemQuantity % promotionForItem.NoOfItems) * unitPrice; return(OnDiscountPrice + OffDiscountPrice); }
public AddItemResponseModel GenerateBill(AddItemRequestModel item) { double unitPrice = GetUnitPriceOfItem(item); if (item.ItemType.Equals("C") || item.ItemType.Equals("D")) { Bill.Total = unitPrice * item.ItemQuantity; } else { Bill.Total = CheckPromoDiscount(unitPrice, item); } Bill.ItemsPurchased.Add(item.ItemType); Bill.IsPromoApplied = isPromoApplied; return(Bill); }
public IActionResult UpdateCartItemQuantity([FromBody] AddItemRequestModel itemModel) { // verify paramters if (itemModel == null || itemModel.ItemId == null || itemModel.Quantity < 1) { return(BadRequest(new { res = "Invalid parameters" })); } // try to update the user's cart Guid userId = GetUserId(); bool valueUpdated = _cartService.UpdateCartItemQuantity(userId, itemModel.ItemId, itemModel.Quantity); // return an error message of the cart wasn't updated if (!valueUpdated) { return(BadRequest(new { err = "Could update your cart" })); } return(Ok()); }
public AddItemApiRequest(UserAccount user, AddItemRequestModel model) { User = user; Model = model; }