public ActionResult Edit(NotificationEditVM model) { string result = ""; if (ModelState.IsValid) { bool success = _nRepo.EditNotification(model, out result); if (success) { TempData["SuccessMsg"] = result; return(RedirectToAction("DetailsThread", new { id = model.IncidentNumber })); } } else { TempData["ErrorMsg"] = "Cannot edit Notification, model not valid."; } // recreate model for the view model = _nRepo.CreateEditModel(model.NotificationReferenceID, model); // if notification returns null, redirect to thread view if (model == null) { TempData["ErrorMsg"] = "Cannot create new notification at the moment"; return(RedirectToAction("DetailsThread", new { id = model.IncidentNumber })); } TempData["ErrorMsg"] = result; return(View(model)); }
// create model for Edit view public NotificationEditVM CreateEditModel(string notificationReferenceID, NotificationEditVM model = null) { var editingNotification = _context.Notification.Where(n => n.ReferenceID == notificationReferenceID) .FirstOrDefault(); if (model == null) { model = new NotificationEditVM() { NotificationReferenceID = notificationReferenceID, IncidentNumber = editingNotification.IncidentNumber, StartDateTime = editingNotification.StartDateTime, EndDateTime = editingNotification.EndDateTime, LevelOfImpactID = editingNotification.LevelOfImpactID, NotificationTypeID = editingNotification.NotificationTypeID, PriorityID = editingNotification.PriorityID, StatusID = editingNotification.StatusID, NotificationDescription = editingNotification.NotificationDescription, NotificationHeading = editingNotification.NotificationHeading }; model.ServerReferenceIDs = editingNotification.Servers.Select(s => s.ReferenceID).ToArray(); model.ApplicationReferenceIDs = editingNotification.Applications.Select(a => a.ReferenceID).ToArray(); } model.ApplicationList = _slRepo.GetApplicationListByServer(model.ServerReferenceIDs); model.ServerList = _slRepo.GetServerList(); model.NotificationTypeList = _slRepo.GetTypeList(); model.LevelOfImpactList = _slRepo.GetImpactLevelList(); model.PriorityList = _slRepo.GetPriorityList(); model.StatusList = _slRepo.GetStatusList(Key.STATUS_TYPE_NOTIFICATION); return(model); }
// edit a created notification public bool EditNotification(NotificationEditVM notification, out string msg) { try { //TO DO: check if it's by server or by Application if (notification.ServerReferenceIDs == null) { msg = "Must choose a Server"; return(false); } if (notification.ApplicationReferenceIDs == null) { notification.ApplicationReferenceIDs = new string[0]; } var servers = _context.Server.Where(s => notification.ServerReferenceIDs.Contains(s.ReferenceID)); var apps = _context.Application.Where(a => notification.ApplicationReferenceIDs.Contains(a.ReferenceID)); var editingNotification = _context.Notification .Where(n => n.ReferenceID == notification.NotificationReferenceID) .FirstOrDefault(); int headingLength = notification.NotificationHeading.IndexOf(" (Edit"); editingNotification.NotificationHeading = headingLength == -1 ? notification.NotificationHeading + " (Edited " + DateTime.Now.ToString() + ")" : notification.NotificationHeading.Substring(0, headingLength) + " (Edited " + DateTime.Now.ToString() + ")"; editingNotification.LevelOfImpactID = notification.LevelOfImpactID; editingNotification.NotificationTypeID = notification.NotificationTypeID; editingNotification.NotificationDescription = notification.NotificationDescription; editingNotification.StatusID = notification.StatusID; editingNotification.StartDateTime = notification.StartDateTime; editingNotification.EndDateTime = notification.EndDateTime; // drop associated servers and apps first editingNotification.Servers.Clear(); editingNotification.Applications.Clear(); // then set associated servers and apps editingNotification.Servers = servers.ToList(); editingNotification.Applications = apps.ToList(); _context.SaveChanges(); msg = "Notification Edited"; return(true); } catch (Exception ex) { if (ex is SqlException) { } msg = "Notification not edited"; return(false); } }