private void UpdateAssignedInvitations(Guest guest, IEnumerable<string> invitationSelections) { if (invitationSelections == null && guest.Invitations == null) { return; } List<Invite> oldInvites = new List<Invite>(); List<Invite> newInvites = new List<Invite>(); if (guest.Invitations != null) { oldInvites = guest.Invitations.ToList(); // HACK: remove all invites, then add new ones. oldInvites.ForEach(axe => db.Invites.Remove(axe)); } Dictionary<string, string> selections = invitationSelections.ToDictionary(i => i.Split(':')[0], i => i.Split(':')[1]); foreach (var ev in db.Events) { // We have added an invite for the guest. if (!selections.ContainsKey(ev.Id.ToString())) continue; int maxInvites = 1; int.TryParse(selections[ev.Id.ToString()], out maxInvites); var invite = new Invite { GuestId = guest.Id, EventId = ev.Id, IsAttending = false, HasResponded = false, PeopleInvited = maxInvites, PeopleAttending = 0 }; newInvites.Add(invite); } newInvites.ForEach(ni => db.Invites.Add(ni)); }
private void PopulateAssignedInvitations(Guest guest) { guest = db.Guests.Include(i => i.Invitations).Single(i => i.Id == guest.Id); var allEvents = db.Events; var guestEvents = new HashSet<int>(guest.Invitations.Select(c => c.EventId)); var viewModel = new List<GuestInvites>(); foreach (var singleEvent in allEvents) { var eventFound = guestEvents.Contains(singleEvent.Id); var maxInvited = 1; var acceptedInvites = 0; if (eventFound) { maxInvited = guest.Invitations.Single(i => i.EventId == singleEvent.Id).PeopleInvited; acceptedInvites = guest.Invitations.Single(i => i.EventId == singleEvent.Id).PeopleAttending; } viewModel.Add(new GuestInvites { EventId = singleEvent.Id, EventName = singleEvent.Name, InvitationMax = maxInvited, InvitationAccepted = acceptedInvites, isInvited = eventFound }); } ViewBag.Events = viewModel; }