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 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); }