コード例 #1
0
        private OrderedPizza BuildNewPizza(int sizeId, int depthId, List <int> toppingIds)
        {
            // Retrieve the selected pizza size and depth
            var orderedSize  = _sizeService.GetById(sizeId);
            var orderedDepth = _depthService.GetById(depthId);

            // Build promos
            string promo1 = "Pepperoni" + "Green Peppers" + "Anchovies";
            string promo2 = "Pepperoni" + "Onions" + "Red Peppers";

            // Create a new pizza for the cutomer and build aout the size and depth details which have been chosen
            OrderedPizza orderedPizza = new OrderedPizza()
            {
                SizeId            = orderedSize.Id,
                SizeDescription   = orderedSize.Description,
                SizePrice         = orderedSize.Price,
                DepthId           = orderedDepth.Id,
                DepthDescription  = orderedDepth.Description,
                DepthPrice        = orderedDepth.Price,
                OrderedPizzaPrice = orderedSize.Price + orderedDepth.Price
            };

            // Process each topping - add to a list, add list to the pizza entity and add topping prices to total pizza price
            List <OrderedPizzaTopping> orderedToppings = new List <OrderedPizzaTopping>();
            string toppingDescs = "";

            if (toppingIds?.Any() ?? false)
            {
                foreach (var item in toppingIds)
                {
                    var _t = _toppingService.GetById(item);
                    toppingDescs += _t.Description;
                    orderedToppings.Add(new OrderedPizzaTopping
                    {
                        Id          = _t.Id,
                        Description = _t.Description,
                        Price       = _t.Price
                    });
                    orderedPizza.OrderedPizzaPrice += _t.Price;
                }
                ;
                // Test and apply promos
                if (toppingDescs == promo1 || toppingDescs == promo2)
                {
                    orderedPizza.OrderedPizzaPrice = orderedPizza.OrderedPizzaPrice - 2;
                }
                ;
            }
            ;

            orderedPizza.OrderedPizzaToppings = orderedToppings;

            return(orderedPizza);
        }