/// <summary> /// Update an appointment in the db /// </summary> /// <param name="newAppointment">The appointment to update(updates the apponinemnt with the id given in the model, rest is updated)</param> /// <returns>A bool stating whether or not the update is a success</returns> public int updateAppointment(AppointmentModel newAppointment) { var findAppointmentFilter = Builders <CalendarModel> .Filter.ElemMatch(x => x.appointments, x => x.id == newAppointment.id); CalendarModel result = collection.Find(findAppointmentFilter).FirstOrDefault(); int index = result.appointments.FindIndex(x => x.id == newAppointment.id); result.appointments.RemoveAt(index); result.appointments.RemoveAll(x => x == null); helperClasses.fastSortAdd(result.appointments, newAppointment); for (int i = 1; i < result.appointments.Count; i++) { if (AppointmentModel.conflict(result.appointments[i - 1], result.appointments[i])) //Check every possible conflict { return(1); } } var updateAppointmentFilter = Builders <CalendarModel> .Update.Set(x => x.appointments, result.appointments); var updateResult = collection.UpdateOne(findAppointmentFilter, updateAppointmentFilter); if (updateResult.IsAcknowledged) { return(0); } return(2); }
/// <summary> /// Merges two calenders given their id /// </summary> /// <param name="calenderA">The calender to merge into</param> /// <param name="calenderB">The calender to put into calenderA</param> /// <returns>A list of appointment Conflict models that contains conflict info</returns> public List <AppointmentConflictModel> mergeCalenders(ObjectId calenderA, ObjectId calenderB) { List <AppointmentConflictModel> conflictList = new List <AppointmentConflictModel>(); CalendarModel keepCalender = collection.Find(x => x.id == calenderA).SingleOrDefault(); CalendarModel deleteCalender = collection.Find(x => x.id == calenderB).SingleOrDefault(); List <AppointmentModel> newAppointmentList = new List <AppointmentModel>(); //Generates the new Appointment list will all appointments in sorted order newAppointmentList.AddRange(keepCalender.appointments); DateTime dateModel = keepCalender.startTime; //Updates the date for each of the date times so they are correct. Otherwise the dates for the appointments will be different that the new calender page foreach (var x in deleteCalender.appointments) { x.aptstartTime = new DateTime(dateModel.Year, dateModel.Month, dateModel.Day, x.aptstartTime.Hour, x.aptstartTime.Minute, x.aptstartTime.Second); x.aptendTime = new DateTime(dateModel.Year, dateModel.Month, dateModel.Day, x.aptendTime.Hour, x.aptendTime.Minute, x.aptendTime.Second); } //Add all of the fixed appointments newAppointmentList.AddRange(deleteCalender.appointments); newAppointmentList.Sort(); //Sort them for storageand conflict checking //Finds conflicts to return for (int i = 1; i < newAppointmentList.Count; i++) { if (AppointmentModel.conflict(newAppointmentList[i - 1], newAppointmentList[i])) //Check every possible conflict { conflictList.Add(new AppointmentConflictModel(newAppointmentList[i - 1], newAppointmentList[i])); // Add a conflict to the list } } //Handle conflicts if (conflictList.Count > 0) // If there is at least on conflict { return(conflictList); //Return the list so the api can return it. } //Database update code var updateAppointmentFilter = Builders <CalendarModel> .Update.Set(x => x.appointments, newAppointmentList); var result = collection.UpdateOne(x => x.id == calenderA, updateAppointmentFilter); //Delete old calender appointments updateAppointmentFilter = Builders <CalendarModel> .Update.Unset(x => x.appointments); collection.UpdateOne(x => x.id == calenderB, updateAppointmentFilter); return(conflictList); // Return the list of conflicts(which will be empty to signal the update happened with no conflicts. }
public static bool appointmentListConflict(List <AppointmentModel> appointments) { bool output = false; for (int i = 1; i < appointments.Count; i++) { output = output || AppointmentModel.conflict(appointments[i - 1], appointments[i]); } return(output); }