Example #1
0
        public async Task <PriceResult> CalculatePriceAsync(PriceRequestDTO request, Guid userId)
        {
            var products = request.Products.Where(g => g.Quantity > 0).ToArray();

            if (!products.Any())
            {
                return new PriceResult
                       {
                           Discount     = 0,
                           SummaryPrice = 0,
                           Products     = new System.Collections.Generic.List <PriceResult.CalculatedProductPrice>()
                       }
            }
            ;

            _warehouseServiceClient.AddHeader(Constants.USERID_HEADER, userId.ToString());
            var basePrices = await _warehouseServiceClient.ProductPriceAsync(products.Select(g => Guid.Parse(g.ProductId)).ToArray());

            var         discount = (100 - new Random().Next(0, 30)) / 100.0m;
            PriceResult result   = new();

            foreach (var prod in products)
            {
                var price = basePrices.First(k => k.Id == Guid.Parse(prod.ProductId)).BasePrice;

                result.SummaryPrice += price * prod.Quantity;
            }
            result.Discount      = 100 - discount * 100;
            result.SummaryPrice *= discount;
            result.SummaryPrice  = decimal.Round(result.SummaryPrice, 2);
            return(result);
        }
    }
Example #2
0
        public async Task <ActionResult <CalculatedPriceResponseDTO> > GetPrice([FromRoute] Guid userId, [FromBody] PriceRequestDTO request)
        {
            var result = await _priceService.CalculatePriceAsync(request, userId);

            return(Ok(_mapper.Map <CalculatedPriceResponseDTO>(result)));
        }