示例#1
0
 private IActionResult getOrderPriceFromApi(OrderDefinitionDTO pizzaDefinition)
 {
     try
     {
         return(Ok(getOrderPrice(pizzaDefinition)));
     }
     catch (BadRequestException ex)
     {
         return(BadRequest());
     }
 }
示例#2
0
        private int getOrderPrice(OrderDefinitionDTO pizzaDefinition)
        {
            var    DELIVERY_PRICE = 10.0;
            var    price          = DELIVERY_PRICE;
            double minPizzaPrice  = double.MaxValue;

            if (pizzaDefinition.pizza.Count == 0)
            {
                return(0);
            }

            foreach (var p in pizzaDefinition.pizza)
            {
                foreach (var i in p.Ingredients)
                {
                    var tmpPizzaPrice = 0.0;
                    var ingredient    = _context.Ingredient.FirstOrDefault(it => it.Name == i);
                    if (ingredient == null)
                    {
                        throw new BadRequestException();
                    }
                    else
                    {
                        price         += ingredient.Price;
                        tmpPizzaPrice += ingredient.Price;
                    }
                    if (tmpPizzaPrice < minPizzaPrice)
                    {
                        minPizzaPrice = tmpPizzaPrice;
                    }
                }
                ;
            }
            ;

            if (pizzaDefinition.PromotionName == null)
            {
                return((int)price);
            }
            else
            {
                var promotion = _context.Promotion.FirstOrDefault(it => it.Name == pizzaDefinition.PromotionName);
                if (promotion == null)
                {
                    throw new BadRequestException();
                }
                else
                {
                    Dto.PromotionType type = (Dto.PromotionType)Enum.Parse(typeof(Dto.PromotionType), promotion.PromotionTypeName);
                    switch (type)
                    {
                    case Dto.PromotionType.FREE_DELIVERY:
                    {
                        if (price >= promotion.MinPrice)
                        {
                            price -= DELIVERY_PRICE;
                        }
                        break;
                    }

                    case Dto.PromotionType.THIRD_PIZZA_GRATIS:
                    {
                        if (price >= promotion.MinPrice && pizzaDefinition.pizza.Count > 2)
                        {
                            price -= minPizzaPrice;
                        }
                        break;
                    }

                    case Dto.PromotionType.THIRTY_PERCENT_OFF:
                    {
                        if (price >= promotion.MinPrice)
                        {
                            price *= 0.7;
                        }
                        break;
                    }
                    }
                }
                return((int)price);
            }
        }