public async Task <ActionResult> CreateBasket(CreateBasketDTO createBasket) { var basketItems = new List <BasketItemDTO>(); var products = await _productService.GetProductsAsync( createBasket.Products.Select(p => p.ProductId)); foreach (var product in products) { basketItems.Add(new BasketItemDTO { Product = _mapper.Map <ProductDTO>(product), Quantity = createBasket.Products.Single(p => p.ProductId == product.Id).Quantity }); } var basket = new BasketDTO(); foreach (var basketItem in basketItems) { basket.BasketTotal += basketItem.Product.Price * basketItem.Quantity; basket.BasketItems.Add(basketItem); } if (!string.IsNullOrEmpty(createBasket.OfferVoucherCode)) { var offerVoucher = await _voucherService.GetOfferVoucherAsync(createBasket.OfferVoucherCode); if (offerVoucher == null) { return(NotFound($"{nameof(offerVoucher)} not found")); } basket.Message = _voucherService.CheckVoucherIsValidAsync(basket, offerVoucher); if (string.IsNullOrEmpty(basket.Message)) { basket.BasketDiscountTotal = basket.BasketTotal - offerVoucher.Amount; } } if (createBasket.GiftVoucherCodes.Any()) { basket.BasketDiscountTotal = await _voucherService.DeductGiftVouchers( createBasket.GiftVoucherCodes, string.IsNullOrEmpty(basket.Message) && string.IsNullOrEmpty(createBasket.OfferVoucherCode)? basket.BasketTotal : basket.BasketDiscountTotal); } return(Ok(basket)); }