예제 #1
0
        public async Task <double> TrolleyCalculator(TrolleyItems items)
        {
            Uri endpointUri = new Uri(_readConfig.ReadBaseUrl() + _readConfig.ReadTrollyCaclulator());

            endpointUri = endpointUri.AddQuery("Token", _readConfig.UserId());
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, endpointUri)
            {
                Content = new StringContent(_serializer.Serialise(items), Encoding.UTF8, "application/json")
            };

            _logger.Information("Sending request to {uri}", endpointUri);
            var response = await _client.SendAsync(requestMessage);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                _logger.Error("Error while sending request to {uri}", endpointUri);
                throw new HttpResponseException(response);
            }
            var contents = await response.Content.ReadAsStringAsync();

            double total;

            double.TryParse(contents, out total);
            return(total);
        }
예제 #2
0
        public async Task <decimal> GetTrolleyTotalX(TrolleyItems trolleyItems)
        {
            try
            {
                var totalPrice = await _wooliesXClient.PostAsync <decimal>("trolleyCalculator", trolleyItems);

                return(totalPrice);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #3
0
        public decimal GetTrolleyTotal(TrolleyItems trolleyItems)
        {
            if (trolleyItems.Products == null || trolleyItems.Products.Count == 0)
            {
                throw new Exception("No products are specified.");
            }

            if (trolleyItems.Quantities == null || trolleyItems.Quantities.Count == 0)
            {
                throw new Exception("No quantites are specified.");
            }


            decimal totalPrice = 0;

            foreach (var quantityItem in trolleyItems.Quantities)
            {
                decimal trolleyProductPrice = 0;
                var     product             = trolleyItems.Products.FirstOrDefault(p => p.Name == quantityItem.Name);

                if (product == null)
                {
                    throw new Exception("Failed to find matching product.");
                }

                var quantityToCalculate = quantityItem.Quantity;

                var specialItem = trolleyItems.Specials.FirstOrDefault(y => y.Quantities.Any(z => z.Name == quantityItem.Name));
                if (specialItem != null)
                {
                    var specialQunatityItem          = specialItem.Quantities.First(x => x.Name == quantityItem.Name);
                    var actualPriceForSpecialQuanity = product.Price * specialQunatityItem.Quantity;

                    if (specialItem.Total < actualPriceForSpecialQuanity)
                    {
                        while (quantityToCalculate > specialQunatityItem.Quantity)
                        {
                            quantityToCalculate -= specialQunatityItem.Quantity;
                            trolleyProductPrice += specialItem.Total;
                        }
                    }
                }

                trolleyProductPrice += quantityToCalculate * product.Price;

                totalPrice += trolleyProductPrice;
            }
            return(totalPrice);
        }
        public async Task <IActionResult> GetTrolleyTotal([FromBody, Required] TrolleyItems trolleyItems)
        {
            try
            {
                var totalPrice = await _trolleyService.GetTrolleyTotalX(trolleyItems);

                return(Ok(totalPrice));
            }
            catch (Exception ex)
            {
                var errorMessage = "Failed to calculate total price for trolley items.";
                _logger.LogError(ex, errorMessage);
                return(BadRequest(errorMessage));
            }
        }
예제 #5
0
        public async Task <IHttpActionResult> PostToTrolleyCalculator(TrolleyItems items)
        {
            //if (items == null)
            //{
            //    items.products = new List<TrollyProduct>()
            //    {
            //        new TrollyProduct()
            //        {
            //            name = "A",
            //            price = 2
            //        }
            //    };
            //    items.quantities = new List<ProductQuantity>()
            //    {
            //        new ProductQuantity()
            //        {
            //            name = "A",
            //            quantity = 2
            //        }
            //    };
            //    items.specials = new List<Special>()
            //    {
            //        new Special()
            //        {
            //            total =0,
            //           quantities = new List<ProductQuantity>()
            //            {
            //                new ProductQuantity()
            //                {
            //                    name = "A",
            //                    quantity = 2
            //                }
            //            }
            //        }
            //    };
            //}

            return(Ok(await _trolleyCalculator.CalculateTrolleyTotal(items)));
        }
 public Task <double> CalculateTrolleyTotal(TrolleyItems items)
 {
     return(_trolleyCalculatorRepository.TrolleyCalculator(items));
 }