public ActionResult Edit(WorkItemDataInputModel model) { PopulateDropDownLists(model); if (ModelState.IsValid) { var hasErrors = HandleLogicErrors(model); if (hasErrors == true) { return(View(model)); } WorkItem workItem = this.dataContext.WorkItems.Where(o => o.Id == model.WorkItemId).FirstOrDefault(); if (workItem != null) { model.UpdateWorkItem(workItem); //update categories var categories = workItem.Categories.ToList(); categories.ForEach(category => workItem.Categories.Remove(category)); AddCategories(model, workItem); if (workItem.isRecurring == true) { var newPattern = model.TransformToRecurrencePattern(); if (workItem.RecurrencePattern == null) { workItem.RecurrencePattern = newPattern; workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } else { //Check if recurrence pattern has not changed var existingPattern = workItem.RecurrencePattern; int mismatch = 0; if (existingPattern.Interval != newPattern.Interval) { mismatch = 1; } if ((int)existingPattern.DayOfWeekMask != (int)newPattern.DayOfWeekMask) { mismatch = 1; } if ((int)existingPattern.Instance != (int)newPattern.Instance) { mismatch = 1; } if (existingPattern.DayOfMonth != newPattern.DayOfMonth) { mismatch = 1; } if ((int)existingPattern.MonthOfYear != (int)newPattern.MonthOfYear) { mismatch = 1; } if (mismatch == 1) { //if the pattern has changed delete all of the old recurring items, save the new pattern and asociate it with the work item RemoveRecurringItems(workItem); this.dataContext.WIRecurrencePatterns.Remove(existingPattern); workItem.RecurrencePattern = newPattern; workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } else { //if pattern hasn`t changed maybe the time span of the pattern has changed, if so, update the datetime values and remove unnecessary recurring items (or add them) if (model.StartDate != workItem.StartDateTime || model.EndDate != workItem.EndDateTime) { foreach (var recurringItem in workItem.RecurringItems .Where(o => o.Start <workItem.StartDateTime || o.End> workItem.EndDateTime) .ToList()) { this.dataContext.RecurringItems.Remove(recurringItem); } workItem.StartDateTime = model.StartDate; workItem.StartDateTime = model.EndDate; CreateOccurrences(workItem, model); } } } } else { if (workItem.RecurrencePattern != null) { var existingPattern = workItem.RecurrencePattern; RemoveRecurringItems(workItem); this.dataContext.WIRecurrencePatterns.Remove(existingPattern); workItem.RecurrenceType = null; } } var message = ""; if (workItem.Reminder != Reminder.None && workItem.isRecurring == false) { try { this.scheduler.SetWorkItemReminderData(this.scheduler, workItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } else if (workItem.Reminder != Reminder.None && workItem.isRecurring == true) { foreach (var recurringItem in workItem.RecurringItems) { try { this.scheduler.SetRecurringItemReminderData(this.scheduler, workItem, recurringItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } } if (message.Length != 0) { ModelState.AddModelError("", "Kļūda: " + message); return(View("CreateEdit", model)); } workItem.UpdatedAt = DateTime.Now; workItem.UpdatedByUserId = this.CurrentUser.UserId; this.dataContext.SaveChanges(); TempData["Message"] = "Uzdevums " + workItem.Subject + " veiksmīgi atjaunots"; TempData["Alert-Level"] = "alert-success"; return(RedirectToAction("Index")); } ViewBag.PostBackMethod = "Edit"; ModelState.AddModelError("", "Uzdevums netika atrasts!"); return(View("CreateEdit", model)); } ViewBag.PostBackMethod = "Edit"; ModelState.AddModelError("", "Lūdzu, izlabojiet kļūdas un atkārtoti nospiediet Saglabāt"); return(View("CreateEdit", model)); }
public ActionResult Create(WorkItemDataInputModel model) { PopulateDropDownLists(model); if (ModelState.IsValid) { var hasErrors = HandleLogicErrors(model); if (hasErrors == true) { return(View(model)); } var workItem = model.TransformToWorkItem(); workItem.CreatedByUserId = this.CurrentUser.UserId; workItem.UpdatedByUserId = this.CurrentUser.UserId; workItem.Categories = new List <Navigate.Models.Classifiers.Category>(); AddCategories(model, workItem); if (workItem.isRecurring == true) { workItem.RecurrencePattern = model.TransformToRecurrencePattern(); workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } this.dataContext.WorkItems.Add(workItem); var message = ""; if (workItem.Reminder != Reminder.None && workItem.isRecurring == false) { try { this.scheduler.SetWorkItemReminderData(this.scheduler, workItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } else if (workItem.Reminder != Reminder.None && workItem.isRecurring == true) { foreach (var recurringItem in workItem.RecurringItems) { try { this.scheduler.SetRecurringItemReminderData(this.scheduler, workItem, recurringItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } } if (message.Length != 0) { ModelState.AddModelError("", "Kļūda: " + message); return(View("CreateEdit", model)); } this.dataContext.SaveChanges(); TempData["Message"] = "Uzdevums " + workItem.Subject + " veiksmīgi izveidots"; TempData["Alert-Level"] = "alert-success"; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Lūdzu, izlabojiet kļūdas un atkārtoti nospiediet Saglabāt"); return(View("CreateEdit", model)); }