public void Delete(Location entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            // Ensure the location exists.
            //  GetById will throw an exception for us
            entity = GetById(entity.Id);

            // Remove all orders that were placed at this location
            //  from the order table
            List <Order> orderList = Database.Order
                                     .Where(o => o.LocationId == entity.Id).ToList();
            // Using OrderRepo for cascading removal
            OrderRepository orderRepo = new OrderRepository(Database);

            foreach (var order in orderList)
            {
                orderRepo.Delete(order);
            }
            // Reset any users default locations that refer to
            //  this location.
            List <User> userList = Database.User
                                   .Where(u => u.DefaultLocationId == entity.Id).ToList();

            foreach (var user in userList)
            {
                // Reset the default location
                user.DefaultLocationId = null;
                // Update the user in the User table
                UserRepository userRepo = new UserRepository(Database);
                userRepo.Update(user);
            }
            // Need to remove all InventoryJunction entries
            //  that refer to this location
            List <InventoryJunction> inventoryList = Database.InventoryJunction
                                                     .Where(inv => inv.LocationId == entity.Id).ToList();
            InventoryJunctionRepository inventoryRepo = new InventoryJunctionRepository(Database);

            foreach (var inventoryJunction in inventoryList)
            {
                inventoryRepo.Delete(inventoryJunction);
            }

            // We can now safely remove the location
            Database.Remove(entity);
        }
Ejemplo n.º 2
0
        public void Delete(Ingredient entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            // Ensure the entity exists within the
            //  database. GetById will throw an
            //  exception for us if it does not
            entity = GetById(entity.Id);

            // Need to remove ingredient from the InventoryJunction
            //  and PizzaJunction tables
            // Find all InventoryJunctions that contain this ingredient
            List <InventoryJunction> inventoryList = Database.InventoryJunction
                                                     .Where(inv => inv.IngredientId == entity.Id).ToList();
            // For each inventory with ingredient in it, remove
            //  it from the InventoryJunction table
            InventoryJunctionRepository inventoryRepo = new InventoryJunctionRepository(Database);

            foreach (var inventoryJunction in inventoryList)
            {
                inventoryRepo.Delete(inventoryJunction);
            }
            // Now do the same for the PizzaJunction table
            List <PizzaJunction> pizzaList = Database.PizzaJunction
                                             .Where(pizza => pizza.IngredientId == entity.Id).ToList();
            PizzaJunctionRepository pizzaRepo = new PizzaJunctionRepository(Database);

            foreach (var pizzaJunction in pizzaList)
            {
                pizzaRepo.Delete(pizzaJunction);
            }

            Database.Remove(GetById(entity.Id));
        }