public void Add_Food_Items()
        {
            //Arrange
            var chips = new FoodItem { FoodItemId = 1, Title = "Chips" };
            var candy = new FoodItem { FoodItemId = 2, Title = "Candy" };
            var pizza = new FoodItem { FoodItemId = 3, Title = "Pizza" };
            var milk = new FoodItem { FoodItemId = 4, Title = "Milk" };

            var model = new EventBaseViewModel
            {
                AllEventFoodItems = new List<FoodItemViewModel>
                    {
                        new FoodItemViewModel(chips),
                        new FoodItemViewModel(candy)
                    },
                WillBringTheseFoodItems = new List<FoodItemViewModel>(new[] { new FoodItemViewModel(pizza), new FoodItemViewModel(milk),  }), // this is what the user is bringing
            };

            //These are in the database
            var dataModel = new Event
            {
                Coordinator = new Person { MyFoodItems = new List<FoodItem> { pizza, milk } },
                FoodItems = new List<FoodItem> { chips, candy } // Pizza and milk will be added
            };

            A.CallTo(() => FoodRepo.GetAll()).Returns(new List<FoodItem>{ chips, candy, pizza, milk }.AsQueryable());

            //Act
            EventService.AppendNewFoodItems(dataModel, model);

            //Assert
            Assert.AreEqual(dataModel.FoodItems.Count, 4); //only the candy is removed.
        }
        public void AppendNewGames(Event dataModel, EventBaseViewModel viewModel)
        {
            var dataGameIds = dataModel.Games.Select(x => x.GameId).ToArray(); //Items in the database
            var newGameItems = viewModel.WillBringTheseGames.Where(x => !dataGameIds.Contains(x.GameId)).ToList();

            //Add new items
            newGameItems.ForEach(game =>
                {
                    var addMe = _gameRepository.GetAll().FirstOrDefault(y => y.GameId == game.GameId);

                    if (addMe != null)
                        dataModel.Games.Add(addMe);
                });
        }
        public void RemoveGames(Event dataModel, EventBaseViewModel viewModel)
        {
            var hostGameIds = viewModel.WillBringTheseGames.Select(x => x.GameId).ToArray(); //Items in local view model
            var thePerson = _personPersonRepo.GetAll().FirstOrDefault(x => x.PersonId == viewModel.PersonId);
            var deletedGameIds = thePerson.MyGames.Where(x => !hostGameIds.Contains(x.GameId)).Select(x => x.GameId).ToList();

            //Delete items
            deletedGameIds.ForEach(id =>
            {
                var removeMe = dataModel.Games.FirstOrDefault(y => y.GameId == id);

                if (removeMe != null)
                    dataModel.Games.Remove(removeMe);
            });
        }
        public ActionResult UpdateGame(EventBaseViewModel model)
        {
            var response = new Response { Error = false };

            try
            {
                var personId = _userService.GetCurrentUserId(User.Identity.Name);

                //Update the food item
                var updateMe = _gameRepository.GetAll().FirstOrDefault(x => x.GameId == model.UpdateGameItem.GameId);
                updateMe.Title = model.UpdateGameItem.Title;
                updateMe.Description = model.UpdateGameItem.Description;

                //Save to the database last
                _gameRepository.SubmitChanges();

                //Get list of pending food ids for this event from session
                var pendingEventFoodItemIds = SessionUtility.Events.GetPendingGames(model.EventId);
                var pendingPersonFoodItemIds = SessionUtility.Person.GetPendingGames(personId);

                var selectedGames = GetSelectedGames(pendingEventFoodItemIds, pendingPersonFoodItemIds, model.EventId);
                response.Data = RenderRazorViewToString("_GameListTemplate", selectedGames);
            }
            catch (Exception)
            {
                //TODO: log error to database
                response.Error = true;
                response.Message = Constants.SERVICE_UPDATE_GAME_FAIL;
            }

            return Json(response);
        }
        public ActionResult AddGame(EventBaseViewModel model)
        {
            var response = new Response { Error = false };

            try
            {
                //Add to the games table if it doesn't exist
                var newGame = new Game { Title = model.AddGameItem.Title, Description = model.AddGameItem.Description};
                _gameRepository.Insert(newGame);
                _gameRepository.SubmitChanges();

                //Add to the user's personal list of food items
                var thePerson = _personRepository.GetAll().FirstOrDefault(x => x.PersonId == model.PersonId);
                thePerson.MyGames.Add(newGame);

                //Add the pending game to session
                SessionUtility.Events.AddGame(newGame.GameId, model.EventId);
                SessionUtility.Person.AddGame(newGame.GameId, model.PersonId);

                //Get list of pending game ids for this event from session
                var pendingEventGameIds = SessionUtility.Events.GetPendingGames(model.EventId);
                var pendingPersonGameIds = SessionUtility.Person.GetPendingGames(model.PersonId);

                //Populate the games already ing brought
                var gameList = GetSelectedGames(pendingEventGameIds, pendingPersonGameIds, model.EventId);
                response.Data = RenderRazorViewToString("_GameListTemplate", gameList);

                //Save to the database last
                _personRepository.SubmitChanges();
            }
            catch (Exception)
            {
                //TODO: log error to database
                response.Error = true;
                response.Message = Constants.SERVICE_ADD_FOOD_ITEM_FAIL;
            }

            return Json(response);
        }
        public ActionResult AddFoodItem(EventBaseViewModel model)
        {
            var response = new Response { Error = false };

            try
            {
                //Add to the food items table if it doesn't exist
                var newFoodItem = new FoodItem { Title = model.AddFoodItem.Title, Description = model.AddFoodItem.Description };
                _foodRepository.Insert(newFoodItem);
                _foodRepository.SubmitChanges();

                //Add to the user's personal list of food items
                var thePerson = _personRepository.GetAll().FirstOrDefault(x => x.PersonId == model.PersonId);
                thePerson.MyFoodItems.Add(newFoodItem);

                //Add the person's pending food item to session
                SessionUtility.Events.AddFoodItem(newFoodItem.FoodItemId, model.EventId);
                SessionUtility.Person.AddFoodItem(newFoodItem.FoodItemId, model.PersonId);

                //Get list of all pending food ids for this event from session
                var pendingEventFoodItemIds = SessionUtility.Events.GetPendingFoodItems(model.EventId);
                var pendingPersonFoodItemIds = SessionUtility.Person.GetPendingFoodItems(model.PersonId);

                //Populate the food items already being brought
                var foodList = GetSelectedFoodItems(pendingEventFoodItemIds, pendingPersonFoodItemIds, model.EventId);
                response.Data = RenderRazorViewToString("_FoodItemListTemplate", foodList);

                //Save to the database if no errors have occurred
                _personRepository.SubmitChanges();
            }
            catch (Exception)
            {
                //TODO: log error to database
                response.Error = true;
                response.Message = Constants.SERVICE_ADD_FOOD_ITEM_FAIL;
            }

            return Json(response);
        }
        public void Remove_Games()
        {
            //Arrange
            var settlers = new Game { GameId = 1, Title = "Settlers" };
            var shadows = new Game { GameId = 2, Title = "Shadows" };
            var heros = new Game { GameId = 3, Title = "Heros" };
            var monopoly = new Game { GameId = 4, Title = "Monopoly" };

            var model = new EventBaseViewModel
            {
                AllEventGames = new List<GameViewModel>
                    {
                        new GameViewModel(settlers),
                        new GameViewModel(shadows),
                        new GameViewModel(heros),
                        new GameViewModel(monopoly)
                    },
                WillBringTheseGames = new List<GameViewModel>(new[] { new GameViewModel(monopoly),  }), // this is what the user is bringing
            };

            //These are in the database
            var dataModel = new Event
            {
                Coordinator = new Person { MyGames = new List<Game> { shadows, monopoly } },
                Games = new List<Game> { settlers, shadows, heros, monopoly}
            };

            A.CallTo(() => PersonRepo.GetAll()).Returns(new List<Person>(new[] { dataModel.Coordinator }).AsQueryable());

            //Act
            EventService.RemoveGames(dataModel, model);

            //Assert
            Assert.AreEqual(dataModel.Games.Count, 3); //only the candy is removed.
        }