public ActionResult ToggleWatchingTicket(Guid?ticketId, bool goToAllTickets = false) { if (!ticketId.HasValue) { return(RedirectToAction(nameof(Index))); } else if (!TicketRepository.DoesTicketExist(ticketId.Value)) { return(RedirectToAction(nameof(Index), new { error = "Ticket doesn't exist" })); } string userId = User.Identity.GetUserId(); bool isAdminOrProjectManager = User.IsInRole(UserRolesEnum.Admin.ToString()) || User.IsInRole(UserRolesEnum.ProjectManager.ToString()); if (!isAdminOrProjectManager) { return(RedirectToAction(nameof(Index), new { error = "You don't have the permissions to opt-in/opt-out of watching a ticket's changes" })); } try { bool isWatching = TicketNotificationRepository.IsUserSubscribedToTicket(userId, ticketId.Value); ApplicationUser foundUser = UserRepository.GetUserById(userId) ?? throw new Exception("User Not Found"); Ticket foundTicket = TicketRepository.GetTicket(ticketId.Value) ?? throw new Exception("Ticket Not Found"); if (isWatching) { TicketNotificationRepository.RemoveTicketNotificationsFromUser(foundUser, foundTicket, true); } else { TicketNotificationRepository.CreateNewTicketNotification(foundUser, foundTicket, true); } if (goToAllTickets) { return(RedirectToAction(nameof(TicketController.Index), "Ticket")); } return(RedirectToAction(nameof(Index), new { ticketId = ticketId.Value })); } catch (Exception e) { return(RedirectToAction(nameof(Index), new { error = e.Message })); } }
public ActionResult Index(string whatTickets = "", string error = "") { string userId = User.Identity.GetUserId(); ApplicationUser currentUser = UserRepository.GetUserById(userId); if (!string.IsNullOrWhiteSpace(error)) { ModelState.AddModelError("", error); } if (currentUser == null) { return(RedirectToAction(nameof(HomeController.Index), new { controller = "Home" })); } List <TicketIndexViewModel> model; if (!string.IsNullOrWhiteSpace(whatTickets)) { const string assigned = "Assigned"; const string created = "Created"; if (whatTickets.ToLower() == assigned.ToLower()) { ViewBag.whatTickets = assigned; model = TicketRepository.GetUserAssignedTickets(userId) .ToList() //.Where(ticket => TicketRepository.CanUserViewTicket(userId, ticket.Id)) // shouldn't need to check, if the user is assigned to the ticket .Select(ticket => TicketIndexViewModel.CreateNewViewModel(userId, ticket)) .ToList(); } else if (whatTickets.ToLower() == created.ToLower()) { ViewBag.whatTickets = created; model = TicketRepository.GetUserCreatedTickets(userId) .ToList() //.Where(ticket => TicketRepository.CanUserViewTicket(userId, ticket.Id)) // shouldn't need to check, if the user created the ticket .Select(ticket => TicketIndexViewModel.CreateNewViewModel(userId, ticket)) .ToList(); } else //if (whatTickets.ToLower() == "all") // defaults to this else block { ... } { model = TicketRepository.GetAllTickets() .ToList() .Where(ticket => TicketRepository.CanUserViewTicket(userId, ticket.Id)) .Select(ticket => { bool isWatching = TicketNotificationRepository.IsUserSubscribedToTicket(userId, ticket.Id); return(TicketIndexViewModel.CreateNewViewModel(userId, ticket, isWatching)); }).ToList(); } } else { model = TicketRepository.GetAllTickets() .ToList() .Where(ticket => TicketRepository.CanUserViewTicket(userId, ticket.Id)) .Select(ticket => { bool isWatching = TicketNotificationRepository.IsUserSubscribedToTicket(userId, ticket.Id); return(TicketIndexViewModel.CreateNewViewModel(userId, ticket, isWatching)); }).ToList(); } return(View(model)); }