コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        private static double GetUnitPriceOfItem(AddItemRequestModel item)
        {
            var        UnitsFactory    = new UnitPriceFactory();
            IUnitPrice unitPriceOfItem = UnitsFactory.GetUnitPrice(item.ItemType);
            var        unitPrice       = unitPriceOfItem.GetUnitPrice();

            return(unitPrice);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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());
        }
コード例 #7
0
 public AddItemApiRequest(UserAccount user, AddItemRequestModel model)
 {
     User  = user;
     Model = model;
 }