public ActionResult DeleteThread(ThreadDetailVM model) { string result = ""; if (ModelState.IsValid) { bool success = _nRepo.DeleteThread(model.IncidentNumber, out result); if (success) { TempData["SuccessMsg"] = result; return(RedirectToAction("Index")); } } else { ViewBag.ErrorMsg = "Cannot add Notification, model not valid."; } // recreate the model for the view model = _nRepo.CreateDetailModel(model.IncidentNumber); // if notification returns null, redirect to notification index if (model == null) { TempData["ErrorMsg"] = "Cannot delete this thread at the moment"; return(RedirectToAction("DetailsThread", new { id = model.IncidentNumber })); } TempData["ErrorMsg"] = result; return(View(model)); }
// create model for Detail view public ThreadDetailVM CreateDetailModel(string incidentNumber) { try { IEnumerable <Notification> notifications = _context.Notification.Where(n => n.IncidentNumber == incidentNumber) .OrderBy(n => n.SentDateTime).ToList(); IEnumerable <NotificationDetailVM> thread = notifications.Select( n => new NotificationDetailVM { ReferenceID = n.ReferenceID, NotificationHeading = n.NotificationHeading, NotificationDescription = n.NotificationDescription, SentDateTime = n.SentDateTime, Status = n.Status.StatusName }); Notification latestNotification = notifications.LastOrDefault(); IEnumerable <NotificationServerVM> servers = latestNotification.Servers.Select( s => new NotificationServerVM { ServerName = s.ServerName, ServerType = s.ServerType.ServerTypeName, ServerStatus = s.Status.StatusName, ReferenceID = s.ReferenceID }); // check and get all application associated to the server if latest notification is not app specific IEnumerable <Application> associatedNotificationApplications = latestNotification.Applications; if (associatedNotificationApplications.Count() == 0) { associatedNotificationApplications = latestNotification.Servers.SelectMany(s => s.Applications).Distinct(); } // Get Applications that are associated to this notification if (HttpContext.Current.User.IsInRole(Key.ROLE_USER)) { string userId = HttpContext.Current.User.Identity.GetUserId(); var userApps = _context.UserDetail .Where(u => u.UserID == userId) .FirstOrDefault().Applications; associatedNotificationApplications = associatedNotificationApplications .Where(a => userApps.Contains(a)); } else if (HttpContext.Current.User.IsInRole(Key.ROLE_CLIENT)) { string userId = HttpContext.Current.User.Identity.GetUserId(); var userApps = _context.UserDetail .Where(u => u.UserID == userId) .FirstOrDefault().Client.Applications; associatedNotificationApplications = associatedNotificationApplications .Where(a => userApps.Contains(a)); } IEnumerable <NotificationApplicationVM> apps = associatedNotificationApplications.Select( a => new NotificationApplicationVM { ApplicationName = a.ApplicationName, ApplicationURL = a.URL, ApplicationStatus = a.Status.StatusName, ReferenceID = a.ReferenceID }).OrderByDescending(a => a.ApplicationName); ThreadDetailVM model = new ThreadDetailVM() { IncidentNumber = incidentNumber, NotificationType = latestNotification.NotificationType.NotificationTypeName, LevelOfImpact = latestNotification.LevelOfImpact.LevelName, Status = latestNotification.Status.StatusName, StartDateTime = latestNotification.StartDateTime, EndDateTime = latestNotification.EndDateTime, Thread = thread, Servers = servers, Applications = apps, Subject = notifications.FirstOrDefault().NotificationHeading, SenderName = notifications.FirstOrDefault().UserDetail.FirstName + " " + notifications.FirstOrDefault().UserDetail.LastName }; return(model); } catch (Exception) { // something went while quering the database // handle null values in controller return(null); } }