public static void CheckToSeeIfEventAlreadyExists(PersonEventViewModel personEvents, oikonomosEntities context, EventViewModel personEvent, OldEvent pe) { string groupId = personEvent.GroupId.ToString(); //Check to see if this event already exists OldEvent duplicateEvent = null; var check = (from e in context.OldEvents where e.TableId == (int)Tables.Person && e.Reference == personEvents.PersonId && e.EventDate == personEvent.Date && e.Description.StartsWith(personEvent.Name) //Caters for the attended homegroup (blah) select e); if (personEvent.GroupId != 0) { duplicateEvent = (from e in check where e.Value == groupId select e).FirstOrDefault(); } else { duplicateEvent = (from e in check select e).FirstOrDefault(); } if (duplicateEvent == null) { context.OldEvents.AddObject(pe); } if (personEvent.Name == EventNames.AttendedGroup) { //Remove a "did not attend event on the same date" var atCheck = (from e in context.OldEvents where e.TableId == (int)Tables.Person && e.Reference == personEvents.PersonId && e.EventDate == personEvent.Date && e.Description.StartsWith(EventNames.DidNotAttendGroup) select e); if (personEvent.GroupId != 0) { duplicateEvent = (from e in atCheck where e.Value == groupId select e).FirstOrDefault(); } else { duplicateEvent = (from e in check select e).FirstOrDefault(); } if (duplicateEvent != null) { context.DeleteObject(duplicateEvent); } } if (personEvent.Name == EventNames.DidNotAttendGroup) { //Remove an "attended" event on the same date var atCheck = (from e in context.OldEvents where e.TableId == (int)Tables.Person && e.Reference == personEvents.PersonId && e.EventDate == personEvent.Date && e.Description.StartsWith(EventNames.AttendedGroup) select e); if (personEvent.GroupId != 0) { duplicateEvent = (from e in atCheck where e.Value == groupId select e).FirstOrDefault(); } else { duplicateEvent = (from e in check select e).FirstOrDefault(); } if (duplicateEvent != null) { context.DeleteObject(duplicateEvent); } } }
public static OldEvent SavePersonEvent(oikonomosEntities context, PersonEventViewModel personEvents, Person currentPerson, EventViewModel personEvent) { var pe = new OldEvent { Created = DateTime.Now, CreatedByPersonId = currentPerson.PersonId, Changed = DateTime.Now, ChangedByPersonId = currentPerson.PersonId, Description = personEvent.Name, TableId = (int) Tables.Person, EventVisibilityId = (int) EventVisibilities.GroupAdministrators, Reference = personEvents.PersonId, EventDate = personEvent.Date, ChurchId = currentPerson.ChurchId }; if (personEvent.GroupId != 0) { pe.Value = personEvent.GroupId.ToString(); if (personEvent.Name == EventNames.AttendedGroup || personEvent.Name == EventNames.DidNotAttendGroup || personEvent.Name == EventNames.LeftTheGroup) { var hgName = (from g in context.Groups where g.GroupId == personEvent.GroupId select g.Name).FirstOrDefault(); pe.Description += string.Format(" ({0})", hgName); } } return pe; }