Exemplo n.º 1
0
        public int AddSingleTopping(string name, string description)
        {
            int     modifications = 0;
            Topping newTopping    = new Topping {
                name = name, description = description
            };

            using (var context = new PizzaShopEntities())
            {
                context.Toppings.Add(newTopping);
                modifications = context.SaveChanges();
            }
            return(modifications == 1 ? newTopping.id : -1);
        }
Exemplo n.º 2
0
        public bool AddTopping(int pizzaId, int toppingId)
        {
            int modifications = 0;

            using (var context = new PizzaShopEntities())
            {
                Pizza   pizza   = context.Pizzas.Include(e => e.Toppings).FirstOrDefault(e => e.id == pizzaId);
                Topping topping = (from o in context.Toppings select o).FirstOrDefault(o => o.id == toppingId);
                if (pizza != null & topping != null)
                {
                    pizza.Toppings.Add(topping);
                    modifications = context.SaveChanges();
                }
            }
            return(modifications >= 1);
        }
Exemplo n.º 3
0
        public Topping GetTopping(int toppingId)
        {
            Topping topping = new Topping();

            try
            {
                using (var context = new PizzaShopEntities())
                {
                    topping = context.Toppings.First(x => x.id == toppingId);
                }
            }
            catch (System.InvalidOperationException)
            {
                return(null);
            }
            return(topping);
        }
Exemplo n.º 4
0
        public bool DeleteSingleTopping(int toppingId)
        {
            int modifications = 0;

            using (var context = new PizzaShopEntities())
            {
                Topping topping = context.Toppings.Include(e => e.Pizzas).FirstOrDefault(e => e.id == toppingId);
                if (topping != null)
                {
                    foreach (Pizza p in topping.Pizzas)
                    {
                        p.Toppings.Remove(topping);
                    }
                    context.Toppings.Remove(topping);
                    modifications = context.SaveChanges();
                }
            }
            return(modifications >= 1);
        }
Exemplo n.º 5
0
        public int AddPizza(int[] ingredients, string name, string description)
        {
            int   modifications = 0;
            Pizza newPizza      = new Pizza {
                name = name, description = description
            };

            using (var context = new PizzaShopEntities())
            {
                foreach (int toppingId in ingredients)
                {
                    Topping topping = (from o in context.Toppings select o).FirstOrDefault(o => o.id == toppingId);
                    if (topping != null)
                    {
                        newPizza.Toppings.Add(topping);
                    }
                }
                context.Pizzas.Add(newPizza);
                modifications = context.SaveChanges();
            }
            return(modifications > 1 ? newPizza.id : -1);
        }