/// <summary> /// This function saves the events /// </summary> /// <param name="context"></param> /// <param name="eventItem"></param> /// <param name="eventDetails"></param> /// <returns></returns> private int SaveEvent(ref dbDataContext context, tbl_Event eventItem, Event eventDetails) { // Assign event values eventItem.EventTypeId = eventDetails.EventTypeId; eventItem.MainType = eventDetails.MainType; eventItem.Title = eventDetails.Title; eventItem.Location = eventDetails.Location; eventItem.Description = eventDetails.Description; eventItem.StartDate = Convert.ToDateTime(eventDetails.StartDate); eventItem.EndDate = Convert.ToDateTime(eventDetails.EndDate); if (!eventDetails.IsDayEvent) { //start date var minute = string.IsNullOrEmpty(eventDetails.StartTime) ? 0 : (int.Parse(eventDetails.StartTime.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[0])); var second = string.IsNullOrEmpty(eventDetails.StartTime) ? 0 : (int.Parse(eventDetails.StartTime.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1])); eventItem.StartDate = new DateTime(eventItem.StartDate.Year, eventItem.StartDate.Month, eventItem.StartDate.Day, minute, second, 0); //end date minute = string.IsNullOrEmpty(eventDetails.EndTime) ? 0 : int.Parse(eventDetails.EndTime.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[0]); second = string.IsNullOrEmpty(eventDetails.EndTime) ? 0 : int.Parse(eventDetails.EndTime.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1]); eventItem.EndDate = new DateTime(eventItem.EndDate.Year, eventItem.EndDate.Month, eventItem.EndDate.Day, minute, second, 0); } eventItem.UserId = eventDetails.UserId; eventItem.ClientId = eventDetails.Client != null ? eventDetails.Client.id : 0; eventItem.ClientCandidateId = eventDetails.ClientContact != null ? eventDetails.ClientContact.id : 0; eventItem.VacancyId = eventDetails.Vacancy != null ? eventDetails.Vacancy.id : 0; eventItem.RecurrenceId = eventDetails.RecurrenceId; eventItem.Completed = eventDetails.Completed; eventItem.Outcome = eventDetails.Outcome; eventItem.OutcomeNotes = eventDetails.OutcomeNotes; eventItem.Deleted = eventDetails.Deleted; eventItem.LastUpdatedDate = DateTime.Now; eventItem.LastUpdatedBy = eventDetails.LastUpdatedBy; eventItem.AllDayEvent = eventDetails.IsDayEvent; eventItem.Locked = eventDetails.Locked; // Add/Update Event if (eventItem.EventId <= 0) { if (eventItem.MainType.Equals("Task")) { eventItem.UserId = eventDetails.LastUpdatedBy; } eventItem.CreatedBy = eventDetails.LastUpdatedBy; eventItem.CreatedDate = DateTime.Now; context.tbl_Events.InsertOnSubmit(eventItem); } context.SubmitChanges(); // add contacts if (eventItem.EventId > 0) { // delete existing var contacts = context.tbl_EventContacts.Where(t => t.EventId == eventItem.EventId).ToList(); if (contacts.Count > 0) { context.tbl_EventContacts.DeleteAllOnSubmit(contacts); context.SubmitChanges(); } // add new event contact if (eventDetails.Contacts != null) { foreach (var contact in eventDetails.Contacts) { var eventContact = new tbl_EventContact { EventId = eventItem.EventId, CandidateId = contact.id }; context.tbl_EventContacts.InsertOnSubmit(eventContact); } context.SubmitChanges(); } return eventItem.EventId; } return -1; }
/// <summary> /// This function add/edit event /// </summary> /// <param name="eventDetails"></param> /// <returns></returns> public List<int> AddEditEvent(Event eventDetails) { var response = new List<int>(); var context = new dbDataContext(); // Create or retrieve the event var eventItem = context.tbl_Events.FirstOrDefault(t => t.EventId == eventDetails.EventId) ?? new tbl_Event(); int eventId; // if new event, check for recurrence pattern if (eventDetails.RecPattern != null) { //add recurrence pattern var recPattern = new tbl_EventRecurrence { Interval = eventDetails.RecPattern.Interval, EndsOn = Convert.ToDateTime(eventDetails.RecPattern.EndsOn), Repeats = eventDetails.RecPattern.Repeats, StartDate = Convert.ToDateTime(eventDetails.RecPattern.StartDate), Summary = eventDetails.RecPattern.Summary }; // save the rec pattern context.tbl_EventRecurrences.InsertOnSubmit(recPattern); context.SubmitChanges(); // Recurrence id eventDetails.RecurrenceId = recPattern.RecurrenceId; // Create the occurrences var recEndDate = Convert.ToDateTime(eventDetails.RecPattern.EndsOn).AddDays(1); recEndDate = new DateTime(recEndDate.Year, recEndDate.Month, recEndDate.Day, 0, 0, 0); var recStartDate = Convert.ToDateTime(eventDetails.RecPattern.StartDate); var startDate = recStartDate; var daySpan = (Convert.ToDateTime(eventDetails.EndDate) - Convert.ToDateTime(eventDetails.StartDate)).Days; var endDate = startDate.AddDays(daySpan); var count = 0; while (recEndDate > startDate && recEndDate >= endDate) { var newEvent = eventDetails; newEvent.StartDate = new Utils().GetUkDate(startDate); newEvent.EndDate = new Utils().GetUkDate(endDate); //create the events for rec pattern if (count != 0) { //for all the occurrences except first one set the event id to 0 - happens only when user added a recurrence to existing appointment eventItem = new tbl_Event(); } eventId = SaveEvent(ref context, eventItem, newEvent); response.Add(eventId); //next event switch (eventDetails.RecPattern.Repeats) { case "days": startDate = startDate.AddDays(eventDetails.RecPattern.Interval); break; case "weeks": startDate = startDate.AddDays(eventDetails.RecPattern.Interval * 7); break; case "months": startDate = startDate.AddMonths(eventDetails.RecPattern.Interval); break; case "years": startDate = startDate.AddYears(eventDetails.RecPattern.Interval); break; } endDate = startDate.AddDays(daySpan); count += 1; } return response; } eventId = SaveEvent(ref context, eventItem, eventDetails); response.Add(eventId); return response; }