Exemple #1
0
        /// <summary>
        /// Delete a restaurant by ID, including any reviews associated to it.
        /// </summary>
        /// <param name="restaurantId">The ID of the restaurant</param>
        public void DeleteRestaurant(int restaurantId)
        {
            Restaurant entity = _context.Restaurants
                                .Include(r => r.Reviews)
                                .First(r => r.Id == restaurantId);

            _context.Remove(entity);
            _context.SaveChanges();
        }
Exemple #2
0
        public void TestEditWithFullData()
        {
            //Arrange
            var dbContext       = new RestaurantContext();
            var menuManager     = new MenuManager(dbContext);
            var menusController = new MenusController(menuManager);
            var userID          = "8774f57e-f0c7-450a-b17e-09e30668eddc";

            menusController.UserID = userID;
            var newMenu = menuManager.CreateMenu(Guid.NewGuid().ToString(), userID);
            var menu    = new Menu {
                Name = Guid.NewGuid().ToString(), UserID = userID
            };

            menu.ID = newMenu.ID;

            //Act
            menusController.Edit(newMenu.ID, menu);
            var editedMenu = menuManager.GetMenuByID(menu.ID);

            //Assert
            Assert.AreEqual(menu.Name, editedMenu.Name);
            dbContext.Remove(editedMenu);
            dbContext.SaveChanges();
        }
Exemple #3
0
        public async Task <bool> DeleteProductAsync(Guid productId)
        {
            try
            {
                var product = await _context.Products.FirstOrDefaultAsync(p => p.ProductId == productId);

                if (product == null)
                {
                    return(true);
                }
                _context.Remove(product);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemple #4
0
        public bool DeleteRestaurant(long id)
        {
            var restaurant = context.Restaurants.FirstOrDefault(x => x.Id == id);

            if (restaurant == null)
            {
                throw new Exception("No restaurant found");
            }
            context.Remove(restaurant);
            context.SaveChanges();
            return(true);
        }
Exemple #5
0
        public void UpdateCompanyDrinks(RestaurantContext context,
                                        string[] selectedDrinks, Company companyToUpdate)
        {
            if (selectedDrinks == null)
            {
                companyToUpdate.DrinkAssignments = new List <DrinkAssignment>();
                return;
            }

            var selectedDrinkHS = new HashSet <string>(selectedDrinks);
            var companyDrinks   = new HashSet <int>
                                      (companyToUpdate.DrinkAssignments.Select(c => c.Drink.DrinkID));

            foreach (var drink in context.Drinks)
            {
                if (selectedDrinkHS.Contains(drink.DrinkID.ToString()))
                {
                    if (!companyDrinks.Contains(drink.DrinkID))
                    {
                        companyToUpdate.DrinkAssignments.Add(
                            new DrinkAssignment
                        {
                            CompanyID = companyToUpdate.CompanyID,
                            DrinkID   = drink.DrinkID
                        });
                    }
                }
                else
                {
                    if (companyDrinks.Contains(drink.DrinkID))
                    {
                        DrinkAssignment drinkToRemove =
                            companyToUpdate
                            .DrinkAssignments
                            .SingleOrDefault(i => i.DrinkID == drink.DrinkID);
                        context.Remove(drinkToRemove);
                    }
                }
            }
        }
Exemple #6
0
        public void TestCreateWithFullData()
        {
            //Arrange
            var dbContext       = new RestaurantContext();
            var menuManager     = new MenuManager(dbContext);
            var menusController = new MenusController(menuManager);

            menusController.UserID = "8774f57e-f0c7-450a-b17e-09e30668eddc";
            var menu = new Menu {
                Name = Guid.NewGuid().ToString(), UserID = "8774f57e-f0c7-450a-b17e-09e30668eddc"
            };

            //Act
            menusController.Create(menu);
            var menus   = menuManager.GetAllMenus(menu.UserID);
            var newMenu = menus.SingleOrDefault(m => m.Name == menu.Name && m.UserID == menu.UserID);

            //Assert
            Assert.IsNotNull(newMenu);
            dbContext.Remove(newMenu);
            dbContext.SaveChanges();
        }
Exemple #7
0
        public void TestEditWithWrongID()
        {
            //Arrange
            var dbContext       = new RestaurantContext();
            var menuManager     = new MenuManager(dbContext);
            var menusController = new MenusController(menuManager);
            var userID          = "8774f57e-f0c7-450a-b17e-09e30668eddc";

            menusController.UserID = userID;
            var newMenu = menuManager.CreateMenu(Guid.NewGuid().ToString(), userID);
            var menu    = new Menu {
                Name = Guid.NewGuid().ToString(), UserID = userID
            };

            menu.ID = -1;

            //Act
            var result = menusController.Edit(newMenu.ID, menu);

            //Assert
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
            dbContext.Remove(newMenu);
            dbContext.SaveChanges();
        }
Exemple #8
0
        private void UpdateOwnedFoods(string[] selectedFoods, Owners ownersToUpdate)
        {
            if (selectedFoods == null)
            {
                ownersToUpdate.OwnedFoods = new List <OwnedFood>();
                return;
            }
            var selectedFoodsHS = new HashSet <string>(selectedFoods);
            var ownedFoods      = new HashSet <int>
                                      (ownersToUpdate.OwnedFoods.Select(c => c.Food.ID));

            foreach (var food in _context.Food)
            {
                if (selectedFoodsHS.Contains(food.ID.ToString()))
                {
                    if (!ownedFoods.Contains(food.ID))
                    {
                        ownersToUpdate.OwnedFoods.Add(new OwnedFood
                        {
                            OwnerID =
                                ownersToUpdate.ID,
                            FoodID = food.ID
                        });
                    }
                }
                else
                {
                    if (ownedFoods.Contains(food.ID))
                    {
                        OwnedFood foodToRemove = ownersToUpdate.OwnedFoods.FirstOrDefault(i
                                                                                          => i.FoodID == food.ID);
                        _context.Remove(foodToRemove);
                    }
                }
            }
        }
Exemple #9
0
        public bool Delete(int id, bool transactionEndpoint = true)
        {
            if (transactionEndpoint)
            {
                _context.Database.BeginTransaction(IsolationLevel.RepeatableRead);
            }
            {
                var reservation = _context.Reservation
                                  .Where(r => r.Id == id)
                                  .Include(c => c.Customer)
                                  .ThenInclude(c => c.Person)
                                  .ThenInclude(c => c.Location)
                                  .ThenInclude(c => c.ZipCodeNavigation)
                                  .Include(rt => rt.ReservationsTables)
                                  .ThenInclude(t => t.RestaurantTables)
                                  .AsNoTracking()
                                  .FirstOrDefault();

                if (reservation != null)
                {
                    _context.Entry(reservation).State = EntityState.Modified;
                    var order = _context.RestaurantOrder.FirstOrDefault(o => o.ReservationId == id);
                    try
                    {
                        //update order to cancel and remove tables
                        if (order != null)
                        {
                            var or = new OrderRepository(_context);
                            if (or.cancelOrder(order.OrderNo))
                            {
                                // remove tables
                                _context.ReservationsTables.RemoveRange
                                    (_context.ReservationsTables.Where(r => r.ReservationId == reservation.Id));
                                if (transactionEndpoint)
                                {
                                    _context.SaveChanges();
                                    _context.Database.CommitTransaction();
                                    return(true);
                                }
                            }
                            return(false);
                        }

                        _context.Remove(reservation);

                        if (transactionEndpoint)
                        {
                            _context.SaveChanges();
                            _context.Database.CommitTransaction();
                        }

                        return(true);
                    }
                    catch (Exception)
                    {
                        _context.Database.RollbackTransaction();
                        throw;
                    }
                }
            }

            return(false);
        }