public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page) { DashboardIndexVM dashboard = _dRepo.GetDashboard(User, sortOrder, currentFilter, searchString, page); return(View(dashboard)); }
public DashboardIndexVM GetDashboard(IPrincipal User, string sortOrder, string currentFilter, string searchString, int?page) { DashboardIndexVM model = null; IEnumerable <DashboardVM> dashboard = null; if (User != null) { if (HttpContext.Current.User.IsInRole(Key.ROLE_ADMIN) || HttpContext.Current.User.IsInRole(Key.ROLE_STAFF)) { // if user is internal try { IEnumerable <DashboardVM> notifications = _context.Notification .Select(s => new DashboardVM() { ThreadID = s.IncidentNumber, LevelOfImpact = s.LevelOfImpact.LevelName, ImpactValue = s.LevelOfImpact.LevelValue, ThreadHeading = s.NotificationHeading, NotificationType = s.NotificationType.NotificationTypeName, SentDateTime = s.SentDateTime, Status = s.Status.StatusName, SenderName = s.UserDetail.FirstName + " " + s.UserDetail.LastName }) .OrderBy(x => x.LevelOfImpact); dashboard = notifications .GroupBy(n => n.ThreadID) .Select( t => t.OrderByDescending(i => i.SentDateTime).FirstOrDefault() ); dashboard = from n in dashboard where n.Status == Key.STATUS_NOTIFICATION_OPEN select n; int totalNumOfNotifications = dashboard.Count(); page = searchString == null ? page : 1; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; searchString = searchString ?? currentFilter; int pageNumber = (page ?? 1); int defaultPageSize = ConstantsRepo.PAGE_SIZE; model = new DashboardIndexVM { CurrentFilter = searchString, CurrentSort = sortOrder, TotalItemCount = totalNumOfNotifications, ItemStart = currentPageIndex * defaultPageSize + 1, ItemEnd = totalNumOfNotifications - (defaultPageSize * currentPageIndex) >= defaultPageSize ? defaultPageSize * (currentPageIndex + 1) : totalNumOfNotifications, IDSort = sortOrder == ConstantsRepo.SORT_NOTIFICATION_BY_ID_ASCE ? ConstantsRepo.SORT_NOTIFICATION_BY_ID_DESC : ConstantsRepo.SORT_NOTIFICATION_BY_ID_ASCE, DateSort = sortOrder == ConstantsRepo.SORT_NOTIFICATION_BY_DATE_DESC ? ConstantsRepo.SORT_NOTIFICATION_BY_DATE_ASCE : ConstantsRepo.SORT_NOTIFICATION_BY_DATE_DESC, SubjectSort = sortOrder == ConstantsRepo.SORT_NOTIFICATION_BY_HEADING_DESC ? ConstantsRepo.SORT_NOTIFICATION_BY_HEADING_ASCE : ConstantsRepo.SORT_NOTIFICATION_BY_HEADING_DESC, LevelOfImpactSort = sortOrder == ConstantsRepo.SORT_LEVEL_OF_IMPACT_DESC ? ConstantsRepo.SORT_LEVEL_OF_IMPACT_ASCE : ConstantsRepo.SORT_LEVEL_OF_IMPACT_DESC, SenderSort = sortOrder == ConstantsRepo.SORT_NOTIFICATION_BY_SENDER_DESC ? ConstantsRepo.SORT_NOTIFICATION_BY_SENDER_ASCE : ConstantsRepo.SORT_NOTIFICATION_BY_SENDER_DESC, }; } catch { // something wrong with db, catch it by returning null model = null; } } else if (HttpContext.Current.User.IsInRole(Key.ROLE_CLIENT)) { // if it's external admin try { var username = User.Identity.Name; var clientID = _context.UserDetail .Where(u => u.User.UserName == username) .FirstOrDefault().ClientID; // get all notifications for all client apps var apps = _context.Client .Where(n => n.ClientID == clientID) .SingleOrDefault() .Applications; dashboard = GetAppNotifications(dashboard, apps); model = new DashboardIndexVM { Notifications = Sort(dashboard, sortOrder, searchString).ToPagedList(1, dashboard.Count()), }; } catch { // something wrong with db, catch it by returning null model = null; } } else { // if it's external user try { var userId = User.Identity.GetUserId(); var apps = _context.UserDetail .Where(u => u.UserID == userId) .SingleOrDefault() .Applications; dashboard = GetAppNotifications(dashboard, apps); model = new DashboardIndexVM { Notifications = Sort(dashboard, sortOrder, searchString).ToPagedList(1, dashboard.Count()), }; } catch { // something wrong with db, catch it by returning null model = null; } } } return(model); }