private async Task NotificationsMapper(User user, ListNotificationsViewModel listVM) { foreach (var notification in user.Notifications) { User username = null; if (User.IsInRole("admin")) { string usernameFromNotif = notification.Message.Substring(0, notification.Message.IndexOf(" ")); username = await _accountManager.GetUserByUsernameAsync(usernameFromNotif); } var vm = new NotificationsViewModel(); vm.Id = notification.Id.ToString(); vm.Message = notification.Message; vm.UserId = notification.User.Id.ToString(); vm.IsSeen = notification.IsSeen; vm.DateSent = notification.SentOn; if (User.IsInRole("admin")) { vm.User = username; } listVM.NotificationsList.Add(vm); } }
public async Task <IActionResult> Index() { var user = await _accountManager.GetUserByUsernameAsync(User.Identity.Name); var listVM = new ListNotificationsViewModel(); if (User.IsInRole("admin")) { await NotificationsMapper(user, listVM); } else if (User.IsInRole("user")) { await NotificationsMapper(user, listVM); } listVM.NotificationsList = listVM.NotificationsList.OrderByDescending(n => n.DateSent).ToList(); return(View(listVM)); }