Ejemplo n.º 1
0
        public async Task <IHttpActionResult> PostAsync(BasketInDTO basketIn)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            BasketOutDTO basketOut = null;

            try
            {
                basketOut = await BasketService.CalculatePrice(basketIn);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok <BasketOutDTO>(basketOut));
        }
Ejemplo n.º 2
0
        public Task <BasketOutDTO> CalculatePrice(BasketInDTO basketIn)
        {
            // First load all the products
            var products      = GetBasketProducts(basketIn);
            var productsPrice = products.Sum(x => x.Price);

            // Now determine the discounts that can be applied
            var appliedDiscounts       = AppliedDiscountService.GetAppliedDiscounts(products);
            var appliedDiscountsAmount = appliedDiscounts.Sum(x => x.Amount);

            // Finally wrap up and return
            var basketOut = new BasketOutDTO
            {
                Products         = products,
                AppliedDiscounts = appliedDiscounts,
                TotalPrice       = productsPrice + appliedDiscountsAmount
            };

            return(Task.FromResult <BasketOutDTO>(basketOut));
        }