Пример #1
0
        public async Task <decimal> GetTrolleyTotal(TrolleyDto trolley)
        {
            var url      = GetApiUrl(ApiCatalog.GetTrolleyTotal);
            var response = await PostAsync <TrolleyDto>(url, trolley);

            var total = await ReadApiResponse <decimal>(response);

            return(total);
        }
Пример #2
0
        public async Task <ActionResult <string> > Post(TrolleyDto trolley)
        {
            _logger.LogInformation($"GetTrolleyTotal: Get total of Trolley");

            var total = await Mediator.Send(new GetTrolleyTotalRequest
            {
                Trolley = trolley
            });

            return(Ok(total));
        }
Пример #3
0
        public async Task <decimal> GetTrolleyTotal(TrolleyDto trolley)
        {
            var total = await _api.GetTrolleyTotal(trolley);

            return(total);
        }
Пример #4
0
        public void Handle_ShouldGetTrolleyTotal()
        {
            var expected = "123";

            var trolley = new TrolleyDto
            {
                Products = new List <TrolleyProduct>
                {
                    new TrolleyProduct {
                        Name = "TP1", Price = 1
                    },
                    new TrolleyProduct {
                        Name = "TP2", Price = 2
                    },
                    new TrolleyProduct {
                        Name = "TP3", Price = 3
                    }
                },
                Quantities = new List <TrolleyProductQuantity>
                {
                    new TrolleyProductQuantity {
                        Name = "TPQ1", Quantity = 1
                    },
                    new TrolleyProductQuantity {
                        Name = "TPQ2", Quantity = 2
                    },
                    new TrolleyProductQuantity {
                        Name = "TPQ3", Quantity = 3
                    }
                },
                Specials = new List <TrolleySpecial>
                {
                    new TrolleySpecial {
                        Quantities = new List <TrolleyProductQuantity>
                        {
                            new TrolleyProductQuantity {
                                Name = "TPQS1", Quantity = 1
                            },
                            new TrolleyProductQuantity {
                                Name = "TPQS2", Quantity = 2
                            },
                            new TrolleyProductQuantity {
                                Name = "TPQS3", Quantity = 3
                            }
                        },
                        Total = 3
                    }
                }
            };

            var request = new GetTrolleyTotalRequest
            {
                Trolley = trolley
            };

            var mapper = new MapperConfiguration(c =>
            {
                c.AddProfile(new MappingProfile());
            }).CreateMapper();
            var entity = mapper.Map <TrolleyEntity>(request.Trolley);

            _mockMapper.Setup(s => s.Map <TrolleyEntity>(request.Trolley)).Returns(entity);

            _mockTrolleyService.Setup(s => s.GetTrolleyTotal(entity)).ReturnsAsync(expected);

            var result = _sut.Handle(request, It.IsAny <CancellationToken>()).Result;

            Assert.Equal(expected, result);
        }
 public async Task <decimal> CalculateTotal([FromBody] TrolleyDto trolley)
 {
     return(await _mediator.Send(new GetTrolleyTotalQuery(trolley)));
 }
 public GetTrolleyTotalQuery(TrolleyDto trolley)
 {
     Trolley = trolley;
 }
Пример #7
0
        public decimal TrolleyCalculator([FromQuery] string token, [FromBody] TrolleyDto trolley)
        {
            var total = _trolleyService.CalculatePrice(trolley);

            return(total);
        }
Пример #8
0
        public decimal CalculatePrice(TrolleyDto trolley)
        {
            decimal finalSum = 0;

            // Calculate max price
            foreach (var quantity in trolley.Quantities)
            {
                //find the product price
                var product = trolley.Products.Where(p => p.Name == quantity.Name).FirstOrDefault();

                if (product != null)
                {
                    product.Quantity += quantity.Quantity;
                    finalSum         += product.Price * quantity.Quantity;
                }
            }

            //loop over all the offers
            foreach (var special in trolley.Specials)
            {
                var doesSpecialApply = true;
                Dictionary <ProductDto, int> products = new Dictionary <ProductDto, int>();
                foreach (var quantity in special.Quantities)
                {
                    var product = trolley.Products.Where(p => p.Name == quantity.Name && p.Quantity > 0).FirstOrDefault();
                    if (product != null)
                    {
                        products.Add(product, quantity.Quantity);
                        if (product.Quantity < quantity.Quantity)
                        {
                            doesSpecialApply = false;
                        }
                    }
                    else
                    {
                        doesSpecialApply = false;
                    }
                }
                if (doesSpecialApply)
                {
                    decimal currentSum = 0;
                    foreach (var product in trolley.Products.Where(p => p.Quantity > 0))
                    {
                        if (products.ContainsKey(product))
                        {
                            currentSum += (product.Quantity - products[product]) * product.Price;
                        }
                        else
                        {
                            currentSum += product.Quantity * product.Price;
                        }
                    }
                    currentSum += special.Total;
                    if (finalSum > currentSum)
                    {
                        finalSum = currentSum;
                    }
                }
            }
            return(finalSum);
        }