public ActionResult Create(EditEventViewModel model) { try { if (ModelState.IsValid) { var createMe = model.GetDataModel(); //Set the event coordinator var userName = (User != null) ? User.Identity.Name : string.Empty; var userId = _userService.GetCurrentUserId(userName); var coordinator = _personRepository.GetAll().FirstOrDefault(x => x.PersonId == userId); createMe.Coordinator = coordinator; //Update the date / time for the event _eventService.SetEventDates(createMe, model); //Invite people _eventService.InviteNewPeople(createMe, model); //Add food _eventService.AppendNewFoodItems(createMe, model); //Add games _eventService.AppendNewGames(createMe, model); _eventRepository.Insert(createMe); _eventRepository.SubmitChanges(); //Send notifications SendUpdateNotifications(createMe, createMe.RegisteredInvites, createMe.UnRegisteredInvites); return RedirectToAction("Index", "Home", new {message = BaseControllerMessageId.SaveModelSuccess}); } //Return the model if the state is invalid... return View(model); } catch (Exception) { //TODO: log to database } //If it makes it this far something is wrong. return RedirectToAction("Index", "Home", new { message = BaseControllerMessageId.SaveModelFailed }); }
public void InviteNewPeople(Event dataModel, EditEventViewModel viewModel) { int tParse; //Add the temp user ids to the pending invitations table var emailList = new List<string>(); var facebookIdList = new List<string>(); var emailInvites = viewModel.PeopleInvited.Where(x => x.PersonId < 0).ToList(); //var facebookInvites = viewModel.PeopleInvited.Where(x => x.Split(delimiter).Length == 2).ToList(); emailInvites.ForEach(x => { var emailAddress = x.Email; emailList.Add(emailAddress); //Make sure it doesn't exist already var exists = _invitationRepository.GetAll().FirstOrDefault(y => y.Email == emailAddress); if (exists == null) { var emailInvite = new PendingInvitation { PersonId = dataModel.Coordinator.PersonId, Email = emailAddress, FirstName = x.FirstName, LastName = x.LastName }; _invitationRepository.Insert(emailInvite); } }); //facebookInvites.ForEach(x => //{ // var tempUserArray = x.Split(delimiter); // var facebookId = tempUserArray[0]; // //Get the first and last name values // var nameArray = tempUserArray[1].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); // var firstName = nameArray[0]; // var lastName = nameArray[nameArray.Length - 1]; // facebookIdList.Add(facebookId); // //Make sure it doesn't exist already // var exists = _invitationRepository.GetAll().FirstOrDefault(y => y.FacebookId == facebookId); // if (exists == null) // { // var facebookInvite = new PendingInvitation // { // PersonId = dataModel.Coordinator.PersonId, // FacebookId = facebookId, // FirstName = firstName, // LastName = lastName // }; // _invitationRepository.Insert(facebookInvite); // } //}); _invitationRepository.SubmitChanges(); //Find the existing user ids var dataPeopleIds = dataModel.RegisteredInvites.Select(x => x.PersonId).ToArray(); //Items in the database var newPeople = viewModel.PeopleInvited .Where(x => x.PersonId > 0) .Where(x => !dataPeopleIds.Contains(x.PersonId)).ToList(); //Add new people newPeople.ForEach(person => { var inviteMe = _personPersonRepo.GetAll().FirstOrDefault(x => x.PersonId == person.PersonId); if (inviteMe != null) { dataModel.RegisteredInvites.Add(inviteMe); //Add the new invite to the user's list of friends if necissary... var exists = dataModel.Coordinator.MyRegisteredFriends.FirstOrDefault( x => x.PersonId == inviteMe.PersonId); if (exists == null) { dataModel.Coordinator.MyRegisteredFriends.Add(inviteMe); } } }); //Find the existing temp emails var emailPeople = dataModel.UnRegisteredInvites.Where(x => emailList.Contains(x.Email)) .Select(x => x.Email).ToArray(); //Items in the database var newEmailPeople = emailList .Where(x => !emailPeople.Contains(x)).ToList(); //Add people invited by email newEmailPeople.ForEach(email => { var inviteMe = _invitationRepository.GetAll().FirstOrDefault(invite => invite.Email == email); if (inviteMe != null) { dataModel.UnRegisteredInvites.Add(inviteMe); //Add the new email invite to the user's list of friends if necissary... var exists = dataModel.Coordinator.MyUnRegisteredFriends.FirstOrDefault( x => x.PendingInvitationId == inviteMe.PendingInvitationId); if (exists == null) { dataModel.Coordinator.MyUnRegisteredFriends.Add(inviteMe); } } }); //Find the existing temp facebook ids var facebookPeople = dataModel.UnRegisteredInvites.Where(x => facebookIdList.Contains(x.FacebookId)) .Select(x => x.FacebookId).ToArray(); //Items in the database var newFacebookPeople = facebookIdList .Where(x => !facebookPeople.Contains(x)).ToList(); //Add new people invited by facebook newFacebookPeople.ForEach(facebookId => { var inviteMe = _invitationRepository.GetAll().FirstOrDefault(invite => invite.FacebookId == facebookId); if (inviteMe != null) { dataModel.UnRegisteredInvites.Add(inviteMe); //Add the new facebook invite to the user's list of friends if necissary... var exists = dataModel.Coordinator.MyUnRegisteredFriends.FirstOrDefault( x => x.PendingInvitationId == inviteMe.PendingInvitationId); if (exists == null) { dataModel.Coordinator.MyUnRegisteredFriends.Add(inviteMe); } } }); }
public ActionResult Edit(EditEventViewModel model) { Event updateMe = null; try { updateMe = _eventRepository.GetAll().IncludeAll("Coordinator").FirstOrDefault(x => x.EventId == model.EventId); if (ModelState.IsValid) { var initialRequiredFieldsState = _eventService.GetSerializedModelState(updateMe); var previousRegisteredInvites = new List<Person>(updateMe.RegisteredInvites); var previousNonRegisteredInvites = new List<PendingInvitation>(updateMe.UnRegisteredInvites); updateMe.Title = model.Title; updateMe.Description = model.Description; updateMe.Location = model.Location; model.PersonId = updateMe.Coordinator.PersonId; //Update the date / time for the event _eventService.SetEventDates(updateMe, model); //Food items _eventService.AppendNewFoodItems(updateMe, model); _eventService.RemoveFoodItems(updateMe, model); //Games _eventService.AppendNewGames(updateMe, model); _eventService.RemoveGames(updateMe, model); //People invited _eventService.InviteNewPeople(updateMe, model); _eventService.UninvitePeople(updateMe, model); _eventRepository.SubmitChanges(); var updatedRequiredFieldsState = _eventService.GetSerializedModelState(updateMe); var newRegisteredInvites = _eventService.GetRegisteredInvites(previousRegisteredInvites, updateMe.RegisteredInvites); var newNonRegisteredInvites = _eventService.GetNonRegisteredInvites(previousNonRegisteredInvites, updateMe.UnRegisteredInvites); var uninvitedRegisteredUsers = _eventService.GetRegisteredUninvites(previousRegisteredInvites, updateMe.RegisteredInvites); var uninvitedNonRegisteredUsers = _eventService.GetNonRegisteredUninvites(previousNonRegisteredInvites, updateMe.UnRegisteredInvites); //Send notifications if the model has changed if (!initialRequiredFieldsState.Equals(updatedRequiredFieldsState)) SendUpdateNotifications(updateMe, newRegisteredInvites, newNonRegisteredInvites); //Even if the model state has not changed, send notifications to newly invited people else { SendInvitations(updateMe, newRegisteredInvites, newNonRegisteredInvites); } //No matter what let people know when they are uninvited SendUnInvitations(updateMe, uninvitedRegisteredUsers, uninvitedNonRegisteredUsers); return RedirectToAction("Index", "Home", new {message = BaseControllerMessageId.SaveModelSuccess}); } model = GetViewModel(updateMe); //Return the model if the state is invalid... return View(model); } catch (Exception) { //TODO: log to database } //If it makes it this far something is wrong. model = GetViewModel(updateMe); ViewBag.StatusMessage = GetMessageFromMessageId(BaseControllerMessageId.SaveModelFailed); return View(model); }
private EditEventViewModel GetViewModel(Event dataModel) { var model = new EditEventViewModel(dataModel); //Populate the total list of people who could be invited to an event. var userName = (User != null) ? User.Identity.Name : string.Empty; var userId = _userService.GetCurrentUserId(userName); //var people = new List<PersonViewModel>(); var coordinator = _personRepository.GetAll().FirstOrDefault(x => x.PersonId == userId); model.TimeList = _eventService.GetTimeList(); //model.FacebookFriends = _userService.GetFacebookFriends(userName); //Populate food and games if (dataModel.FoodItems != null) dataModel.FoodItems.ForEach(x => model.AllEventFoodItems.Add(new FoodItemViewModel(x))); if (dataModel.Games != null) dataModel.Games.ForEach(x => model.AllEventGames.Add(new GameViewModel(x))); model.MyFoodItems = new List<FoodItemViewModel>(); model.MyGames = new List<GameViewModel>(); coordinator.MyFoodItems.ForEach(x => model.MyFoodItems.Add(new FoodItemViewModel(x))); coordinator.MyGames.ForEach(x => model.MyGames.Add(new GameViewModel(x))); model.EventId = dataModel.EventId; model.PersonId = coordinator.PersonId; //Stuff the user is already bringing if (dataModel.FoodItems != null) { var eventFoodItemIds = dataModel.FoodItems.Select(x => x.FoodItemId); var hostFoodItemIds = coordinator.MyFoodItems.Select(x => x.FoodItemId); var selectedFoodItems = hostFoodItemIds.Intersect(eventFoodItemIds); model.WillBringTheseFoodItems = dataModel.FoodItems .Where(x => selectedFoodItems.Contains(x.FoodItemId)) .Select(x => new FoodItemViewModel(x)).ToList(); model.WillBringTheseFoodItems.ForEach(x => { x.EventId = model.EventId; x.Index = model.WillBringTheseFoodItems.IndexOf(x); }); } if (dataModel.Games != null) { var eventGameIds = dataModel.Games.Select(x => x.GameId); var hostGameIds = coordinator.MyGames.Select(x => x.GameId); var selectedGames = hostGameIds.Intersect(eventGameIds); model.WillBringTheseGames = dataModel.Games.Where(x => selectedGames.Contains(x.GameId)).Select(x => new GameViewModel(x)).ToList(); model.WillBringTheseGames.ForEach(x => { x.EventId = model.EventId; x.Index = model.WillBringTheseGames.IndexOf(x); }); } model.PersonId = coordinator.PersonId; return model; }
/// <summary> /// Get a new instance of the edit event view model with the accepted and declined attendee lists populated /// </summary> /// <param name="theEvent">An event</param> /// <returns></returns> private EditEventViewModel GetEventViewModel(Event theEvent) { var viewModel = new EditEventViewModel(); viewModel.EventId = theEvent.EventId; theEvent.PeopleWhoAccepted.ForEach(x => viewModel.PeopleWhoAccepted.Add(new PersonViewModel(x))); theEvent.PeopleWhoDeclined.ForEach(x => viewModel.PeopleWhoDeclined.Add(new PersonViewModel(x))); return viewModel; }
/// <summary> /// Get an event view model for testing purposes /// </summary> /// <param name="id">The id of the view model (default = 0)</param> protected EditEventViewModel GetTestEventViewModel(int id = 0) { //People var theHost = new PersonViewModel { PersonId = 1 }; var guestOne = new PersonViewModel{ PersonId = 2 }; var guestTwo = new PersonViewModel { PersonId = 3 }; var theInvitees = new List<PersonViewModel> { guestOne, guestTwo }; //Food var burgers = new FoodItemViewModel { FoodItemId = 1, Title = "Hambergers", Description = "Apple bacon smoked burgers for 10 people." }; var coke = new FoodItemViewModel { FoodItemId = 2, Title = "Coke", Description = "Two 6 packs" }; var cheese = new FoodItemViewModel { FoodItemId = 3, Title = "Cheese", Description = "Good with crackers" }; var foodForTheParty = new List<FoodItemViewModel> { coke, cheese }; //Games var settlers = new GameViewModel { GameId = 1, Title = "Settlers of Catan", Description = "The best game ever for up to four people" }; var blockus = new GameViewModel { GameId = 2, Title = "Blockus", Description = "Fun game of shape fitting for up four people." }; var gamesForTheParty = new List<GameViewModel> { settlers,blockus }; var viewModel = new EditEventViewModel { EventId = id, Title = "My Test Event", Description = "This is a fun test event", Location = "My House", StartDate = DateTime.Now, StartTime = "5:00 PM", EndTime = "2:00 AM", WillBringTheseFoodItems = foodForTheParty, WillBringTheseGames = gamesForTheParty, PeopleInvited = theInvitees }; return viewModel; }
public ActionResult AddNewGuest(EditEventViewModel model) { var response = new Response { Error = false }; try { //Get the event var theEvent = GetEventById(model.EventId); //Get the host var theHost = GetPersonById(model.PersonId); //Find out the if the user being added already has an email as a registered user... see comments at top of class var exists = _personRepository.GetAll().FirstOrDefault(x => x.Email == model.EmailInvite.Email); if (exists == null) { var userName = model.EmailInvite.FirstName + " " + model.EmailInvite.LastName; response.Data = new { PersonId = 0, model.EmailInvite.Email, UserName = userName, model.EmailInvite.FirstName, model.EmailInvite.LastName, model.EmailInvite.InviteControlId }; //Add the unregistered guest to the database var newGuest = new PendingInvitation { FirstName = model.EmailInvite.FirstName, LastName = model.EmailInvite.LastName, Email = model.EmailInvite.Email }; _inviteRepository.Insert(newGuest); _inviteRepository.SubmitChanges(); //Add the new guest to the host's list of unregistered friends theHost.MyUnRegisteredFriends.Add(newGuest); //Add a negative value to the list. var tempId = -newGuest.PendingInvitationId; //Add the unregistered user to session SessionUtility.Events.AddGuest(tempId, model.EventId); } else { //Add the registered user to the session SessionUtility.Events.AddGuest(exists.PersonId, model.EventId); } //Get list of pending invitation ids var pendingEventInvitations = SessionUtility.Events.GetGuestList(model.EventId); var personFriendsList = GetPersonFriendList(theHost); //Populate the guest list var viewModel = GetEventViewModel(theEvent); viewModel.PeopleInvited = GetSelectedGuests(pendingEventInvitations, personFriendsList, model.EventId); response.Data = RenderRazorViewToString("_InvitedPeopleTemplate", viewModel); //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_GUEST_FAIL; } return Json(response); }
public void Edit_Event_ModelState_Not_Valid() { //Arrange var theHost = new Person { PersonId = 1, FirstName = "Billy", LastName = "Bob", MyFoodItems = new List<FoodItem>(), MyGames = new List<Game>() }; var theEvent = GetTestEventDataModel(1); var viewModel = new EditEventViewModel(theEvent); var contoller = new EventController(RepositoryFactory, EventService, UserService, NotifyService); var modelBinder = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => viewModel, viewModel.GetType()), ValueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), CultureInfo.InvariantCulture) }; A.CallTo(() => EventRepo.GetAll()).Returns(new List<Event> { theEvent }.AsQueryable()); A.CallTo(() => PersonRepo.GetAll()).Returns(new List<Person> { theHost }.AsQueryable()); A.CallTo(() => UserService.GetCurrentUserId("")).Returns(1); //Act //None of these should be null viewModel.Title = string.Empty; viewModel.Description = string.Empty; viewModel.Location = string.Empty; viewModel.StartDate = null; viewModel.StartTime = string.Empty; viewModel.EndTime = string.Empty; //Set model binding var binder = new DefaultModelBinder().BindModel(new ControllerContext(), modelBinder); contoller.ModelState.Clear(); contoller.ModelState.Merge(modelBinder.ModelState); //Get the result var result = contoller.Edit(viewModel) as ViewResult; //Assert Assert.AreEqual(result.ViewData.ModelState.Count, 6); Assert.IsFalse(result.ViewData.ModelState.IsValid); }
public void Parse_Event_Dates() { //Arrange var hours = 4; var startTimeValue = "4:00 AM"; //today var endTimeValue = "2:30 AM"; //This SHOULD be 2AM the next day... var dataModel = new Event(); var viewModel = new EditEventViewModel { StartDate = DateTime.Now, StartTime = startTimeValue, EndTime = endTimeValue }; //Act EventService.SetEventDates(dataModel, viewModel); //Assert Assert.AreEqual(dataModel.StartDate, DateTime.Now.Date.AddHours(4)); Assert.AreEqual(dataModel.EndDate, DateTime.Now.Date.AddHours(26).AddMinutes(30)); }
public void Uninvite_People() { //Arrange var personOne = new Person { PersonId = 1, MyFoodItems = new List<FoodItem>(), MyGames = new List<Game>()}; var personTwo = new Person { PersonId = 2, MyFoodItems = new List<FoodItem>(), MyGames = new List<Game>() }; var personThree = new Person { PersonId = 3, MyFoodItems = new List<FoodItem>(), MyGames = new List<Game>() }; var vmPersonOne = new PersonViewModel(personOne); var vmPersonTwo = new PersonViewModel(personTwo); var vmEmailPerson = new PersonViewModel { PersonId = -3, Email = "*****@*****.**" }; var viewModel = new EditEventViewModel { PeopleInvited = new List<PersonViewModel> { vmPersonOne, vmPersonTwo, vmEmailPerson } }; var dataModel = new Event { RegisteredInvites = new List<Person> { new Person { PersonId = 2 }, new Person { PersonId = 3 } }, UnRegisteredInvites = new List<PendingInvitation> { new PendingInvitation { PendingInvitationId = 3, Email = "*****@*****.**" } }, PeopleWhoAccepted = new List<Person> { new Person { PersonId = 2 } }, PeopleWhoDeclined = new List<Person> { new Person { PersonId = 3 } } }; A.CallTo(() => PersonRepo.GetAll()).Returns(new EnumerableQuery<Person>(new[] {personOne, personTwo, personThree})); //Act viewModel.PeopleInvited.Remove(vmPersonOne); viewModel.PeopleInvited.Remove(vmPersonTwo); viewModel.PeopleInvited.Remove(vmEmailPerson); EventService.UninvitePeople(dataModel, viewModel); //Assert Assert.AreEqual(dataModel.RegisteredInvites.Count, 0); Assert.AreEqual(dataModel.UnRegisteredInvites.Count, 0); Assert.AreEqual(dataModel.PeopleWhoAccepted.Count, 0); Assert.AreEqual(dataModel.PeopleWhoDeclined.Count, 0); }
public void Invite_New_People_By_Email() { //Arrange var ben = new PersonViewModel{ PersonId = -1, Email = "*****@*****.**", FirstName = "Ben", LastName = "Bufford" }; var dan = new PersonViewModel { PersonId = -2, Email = "*****@*****.**", FirstName = "Dan", LastName = "Gidman" }; var herb = new PersonViewModel { PersonId = -3, Email = "*****@*****.**", FirstName = "Herb", LastName = "Neese" }; var viewModel = new EditEventViewModel { PeopleInvited = new List<PersonViewModel> { dan, herb } }; var dataModel = new Event { Coordinator = new Person { PersonId = 1, MyUnRegisteredFriends = new List<PendingInvitation>()}, UnRegisteredInvites = new List<PendingInvitation> { new PendingInvitation { PendingInvitationId = 2, Email = "*****@*****.**" }, new PendingInvitation { PendingInvitationId = 3, Email = "*****@*****.**" } }, RegisteredInvites = new List<Person>() }; A.CallTo(() => InvitationRepo.GetAll()).Returns(new List<PendingInvitation> { new PendingInvitation { PendingInvitationId = 1, Email = "*****@*****.**" } }.AsQueryable()); //Act viewModel.PeopleInvited.Add(ben); EventService.InviteNewPeople(dataModel, viewModel); //Assert Assert.AreEqual(dataModel.UnRegisteredInvites.Count, 3); }
public void Invite_New_People() { //Arrange var personOne = new PersonViewModel{PersonId = 1}; var personTwo = new PersonViewModel { PersonId = 2 }; var personThree = new PersonViewModel { PersonId = 3 }; var theHost = new Person { PersonId = 4, MyRegisteredFriends = new List<Person>(), MyUnRegisteredFriends = new List<PendingInvitation>()}; var viewModel = new EditEventViewModel{ PeopleInvited = new List<PersonViewModel>{personTwo, personThree} }; var dataModel = new Event { Coordinator = theHost, RegisteredInvites = new List<Person> {new Person{PersonId = 2}, new Person{PersonId = 3}}, UnRegisteredInvites = new List<PendingInvitation>() }; A.CallTo(() => PersonRepo.GetAll()).Returns(new List<Person> { new Person() { PersonId = 1 } }.AsQueryable()); //Act viewModel.PeopleInvited.Add(personOne); EventService.InviteNewPeople(dataModel, viewModel); //Assert Assert.AreEqual(dataModel.RegisteredInvites.Count, 3); }
public void Create_Event_ModelState_Not_Valid() { //Arrange var viewModel = new EditEventViewModel(); var contoller = new EventController(RepositoryFactory, EventService, UserService, NotifyService); var modelBinder = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => viewModel, viewModel.GetType()), ValueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), CultureInfo.InvariantCulture) }; var binder = new DefaultModelBinder().BindModel(new ControllerContext(), modelBinder); contoller.ModelState.Clear(); contoller.ModelState.Merge(modelBinder.ModelState); //Act var result = contoller.Create(viewModel) as ViewResult; //Assert Assert.AreEqual(result.ViewData.ModelState.Count, 6); Assert.IsFalse(result.ViewData.ModelState.IsValid); }
public void SetEventDates(Event dataModel, EditEventViewModel viewModel) { DateTime startTime = DateTime.Parse(viewModel.StartDate.Value.ToShortDateString() + " " + viewModel.StartTime); DateTime endTime = DateTime.Parse(viewModel.StartDate.Value.ToShortDateString() + " " + viewModel.EndTime); int hours = startTime.Hour; int minutes = startTime.Minute; //Set the data model start date... dataModel.StartDate = viewModel.StartDate.Value.Date.AddHours(hours).AddMinutes(minutes); int endHour = endTime.Hour; int endMinute = endTime.Minute; dataModel.EndDate = dataModel.StartDate.Date.AddHours(endHour).AddMinutes(endMinute); //Change the end date if... if (dataModel.StartDate.Hour > endTime.Hour) dataModel.EndDate = dataModel.StartDate.Date.AddDays(1).Date.AddHours(endHour).AddMinutes(endMinute); }
public ActionResult UpdateGuestInfo(EditEventViewModel model) { var response = new Response { Error = false }; try { //Get the person var thePerson = GetCurrentUser(); //Get the event var theEvent = GetEventById(model.EventId); //Update the guest info int guestId = Math.Abs(model.UpdateGuest.PersonId); //Make the guest id posative before we hit the database var updateMe = _inviteRepository.GetAll().FirstOrDefault(x => x.PendingInvitationId == guestId); updateMe.FirstName = model.UpdateGuest.FirstName; updateMe.LastName = model.UpdateGuest.LastName; updateMe.Email = model.UpdateGuest.Email; //Get list of pending invitation ids var pendingEventInvitations = SessionUtility.Events.GetGuestList(model.EventId); var personFriendsList = GetPersonFriendList(thePerson); //Populate the guset list var viewModel = GetEventViewModel(theEvent); viewModel.PeopleInvited = GetSelectedGuests(pendingEventInvitations, personFriendsList, model.EventId); response.Data = RenderRazorViewToString("_InvitedPeopleTemplate", viewModel); //Save to the database last _inviteRepository.SubmitChanges(); } catch (Exception) { //TODO: log error to database response.Error = true; response.Message = Constants.SERVICE_UPDATE_GUEST_FAIL; } return Json(response); }
public void UninvitePeople(Event dataModel, EditEventViewModel viewModel) { int parse; //Process peoeple with user accounts var modelPeopleIds = viewModel.PeopleInvited .Where(x => x.PersonId != 0) .Select(x => x.PersonId).ToArray(); //Items in local view model var deletedPeopleIds = dataModel.RegisteredInvites.Where(x => !modelPeopleIds.Contains(x.PersonId)).Select(x => x.PersonId).ToList(); //Delete items deletedPeopleIds.ForEach(id => { var thePerson = _personPersonRepo.GetAll().FirstOrDefault(x => x.PersonId == id); var removeInvitation = dataModel.RegisteredInvites.FirstOrDefault(y => y.PersonId == id); var removeAccepted = dataModel.PeopleWhoAccepted.FirstOrDefault(y => y.PersonId == id); var removeDeclined = dataModel.PeopleWhoDeclined.FirstOrDefault(y => y.PersonId == id); //Remove from the invitation list dataModel.RegisteredInvites.Remove(removeInvitation); //Remove from the accepted list if (removeAccepted != null) dataModel.PeopleWhoAccepted.Remove(removeAccepted); //Remove from the declined list if (removeDeclined != null) dataModel.PeopleWhoDeclined.Remove(removeDeclined); //Remove the person's food items from the event thePerson.MyFoodItems.ForEach(x => { var removeMe = dataModel.FoodItems.FirstOrDefault(y => y.FoodItemId == x.FoodItemId); if (removeMe != null) dataModel.FoodItems.Remove(removeMe); }); //Remove the person's games from the event thePerson.MyGames.ForEach(x => { var removeMe = dataModel.Games.FirstOrDefault(y => y.GameId == x.GameId); if (removeMe != null) dataModel.Games.Remove(removeMe); }); }); //Process people without user accounts var unRegisteredIds = new List<int>(); viewModel.PeopleInvited.Where(x => x.PersonId < 0) .Select(x => Math.Abs(x.PersonId)) //Note that we are making the ids posative... they are negative in the view model to keep from conflicting with registered ids .ToList() .ForEach(unRegisteredIds.Add); var deletedEmailInvites = dataModel.UnRegisteredInvites .Where(x => !unRegisteredIds.Contains(x.PendingInvitationId)) .Select(x => x.PendingInvitationId).ToList(); deletedEmailInvites.ForEach(pendingId => { var removeInvitation = dataModel.UnRegisteredInvites.FirstOrDefault(y => y.PendingInvitationId == pendingId); //Remove from the invitation list dataModel.UnRegisteredInvites.Remove(removeInvitation); }); }
public void Edit_Event_Fail() { //Arrange var theHost = new Person { PersonId = 0, FirstName = "Billy", LastName = "Bob", MyFoodItems = new List<FoodItem>(), MyGames = new List<Game>() }; var theEvent = new Event { EventId = 1, Coordinator = theHost, Title = "My Event", Description = "It's cool", Location = "My House", StartDate = DateTime.Now, RegisteredInvites = new List<Person>(), UnRegisteredInvites = new List<PendingInvitation>(), FoodItems = new List<FoodItem>(), Games = new List<Game>() }; var viewModel = new EditEventViewModel(theEvent); var contoller = new EventController(RepositoryFactory, EventService, UserService, NotifyService); //Act A.CallTo(() => EventRepo.GetAll()).Returns(new List<Event> { theEvent }.AsQueryable()); A.CallTo(() => PersonRepo.GetAll()).Returns(new List<Person> { theHost }.AsQueryable()); A.CallTo(() => EventRepo.SubmitChanges()).Throws(new Exception("Crap I crashed.")); var result = contoller.Edit(viewModel) as ViewResult; //Assert Assert.AreEqual(result.ViewBag.StatusMessage, Constants.BASE_SAVE_FAIL); }