public async Task <ActionResult <BasketDTO> > UpsertBasket([FromBody] BasketDTO basketDto)
        {
            var discountUpdateTasks = new List <Task>();

            foreach (var basketItem in basketDto.Items)
            {
                async Task UpdateDiscount()
                {
                    try
                    {
                        var coupon = await _discountGrpcService.GetDiscount(basketItem.ProductName);

                        basketItem.Price -= coupon.Amount;
                    }
                    catch (RpcException exception) when(exception.StatusCode == Grpc.Core.StatusCode.NotFound)
                    {
                    }
                }

                discountUpdateTasks.Add(UpdateDiscount());
            }

            await Task.WhenAll(discountUpdateTasks);

            var basket = await _basketRepository.UpsertBasket(_mapper.Map <Entities.Basket>(basketDto));

            return(CreatedAtAction(nameof(GetBasket), new { userName = basket.UserName }, _mapper.Map <BasketDTO>(basket)));
        }
示例#2
0
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart basket)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    foreach (var item in basket.Items)
                    {
                        try
                        {
                            var coupon = await _discountRGrpcService.GetDiscount(item.ProductName);

                            item.Price = BasketDiscountExtensions.CalculateDiscount(item.Price, coupon.Amount);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception($"Error updating discount for basket: {basket}", ex);
                        }
                    }

                    return(Ok(await _basketRepository.UpdateBasket(basket)));
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error updating the basket with username: {basket.UserName}", ex);
                    throw new Exception($"Error updating the basket with username: {basket.UserName}", ex);
                }
            }

            return(BadRequest(basket));
        }
 public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart basket)
 {
     foreach (var item in basket.Items)
     {
         var coupon = _discountGrpcService.GetDiscount(item.ProductName);
         item.Price -= 50;
     }
     return(Ok(await _repository.UpdateBasket(basket)));
 }
示例#4
0
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart cart)
        {
            foreach (var item in cart.CartItems)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }
            return(Ok(await _repo.UpdateBasket(cart)));
        }
示例#5
0
        public async Task <ActionResult <ShoppingCart> > SetBasket([FromBody] ShoppingCart shoppingCart)
        {
            foreach (var item in shoppingCart.Items)
            {
                CouponModel couponModel = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= couponModel.Amount;
            }

            return(Ok(await _basketRepository.SetBasket(shoppingCart)));
        }
示例#6
0
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart basket)
        {
            // Communicate with Discount.Grpc and calculate lastest prices of products into sc
            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(Ok(await _repository.UpdateBasket(basket)));
        }
示例#7
0
        public async Task <ActionResult <Cart> > UpdateCart([FromBody] Cart cart)
        {
            // Communicate with discount gRPC and calculate latest prices of products
            foreach (var item in cart.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(Ok(await _repo.UpdateCart(cart)));
        }
示例#8
0
        public async Task <ShoppingCart> UpdateBasket(ShoppingCart basket)
        {
            // Consumming Grpc
            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(await _basketRepo.UpdateBasket(basket));
        }
示例#9
0
        public async Task <ActionResult <CustomerBasket> > UpdateBasket([FromBody] CustomerBasket basket)
        {
            // Communicate with Discount. Grpc and calculate lastest prices of products into shopping cart
            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(Convert.ToInt32(item.Id));

                var discount = Decimal.Divide(Convert.ToDecimal(coupon.DiscountPercentage), 100);
                item.UnitPrice -= item.UnitPrice * discount;
            }

            return(Ok(await _repository.UpdateBasketAsync(basket)));
        }
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart shoppingCart)
        {
            //Consume Discount Service
            foreach (var item in shoppingCart.shoppingCartItems)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            var basket = await _repository.UpdateBasket(shoppingCart);

            return(Ok(basket));
        }
        public async Task <IActionResult> UpdateBasket([FromBody] ShoppingCart basket)
        {
            // Communicate with discount.Grpc
            // and calculate latest prices of product into shopping cart
            // consume Discount Grpc
            foreach (var item in basket.Items)
            {
                var coupon = await discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(Ok(await repository.UpdateBasketAsync(basket)));
        }
示例#12
0
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart basket)
        {
            // Communication with Discount gRPC
            // and calculate latests prices of product into shopping cart
            // consume Discount gRPC
            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(Ok(await _basketRepository.UpdateBasket(basket)));
        }
示例#13
0
        public async Task <ActionResult <ShoppingCart> > UpdateBasket([FromBody] ShoppingCart basket)
        {
            //ToDO:- Communicate Discount.Grpc service
            //and calculate updated cost/price after discount
            //Notes:- Basket.API will be the client of Discount.Grpc service and we will use connected service(Grpc)
            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                item.Price -= coupon.Amount;
            }

            return(Ok(await _repository.UpdateBasket(basket)));
        }
        public async Task <ActionResult <ShoppingCart> > UpdateBasket(ShoppingCart basket)
        {
            // Communicate with Discount.Grpc
            // and calculate latest prices of products into shopping cart
            // consume Discount Grpc

            foreach (var item in basket.Items)
            {
                var coupon = await _discountGrpcService.GetDiscount(item.ProductName);

                System.Console.WriteLine("COUPON" + JsonConvert.SerializeObject(coupon));

                item.Price -= coupon?.Amount ?? 0;
            }

            return(Ok(await _repository.UpdateBasket(basket)));
        }