Exemplo n.º 1
0
        public ActionResult RemoveWatch(string ticketIdentifier)
        {
            if (string.IsNullOrWhiteSpace(ticketIdentifier))
            {
                TempData.AddDangerToast("You must provide the ticket identifier to display. Please try again.");
                return(RedirectToAction("Index", "Tickets"));
            }
            Ticket ticket = db.Tickets.ToList().FirstOrDefault(t => t.GetTicketIdentifier() == ticketIdentifier);

            if (ticket == null)
            {
                TempData.AddDangerToast("We were unable to locate this ticket. Please try again.");
                return(RedirectToAction("Index", "Tickets"));
            }

            if (!_permissionsHelper.CanViewTicket(User, ticket.Id))
            {
                TempData.AddDangerToast("You are not allowed to view this resource.");
                return(RedirectToAction("Index", "Tickets"));
            }

            try {
                var watches = ticket.Watchers.Where(w => w.WatcherId == User.Identity.GetUserId()).ToList();
                if (!watches.Any())
                {
                    throw new Exception();
                }                                             //throw error if no watch records exist for this user on this ticket

                //remove any existance of watches on this ticket for this user, and notify if changes saved successfully
                foreach (TicketNotificationWatchEntry w in watches)
                {
                    ticket.Watchers.Remove(w);
                    db.TicketNotificationWatchEntries.Remove(w);
                }
                db.SaveChanges();
                TempData.AddInfoToast($"You have successfully unsubscribed from ticket {ticket.GetTicketIdentifier()}'s notifications. You will be no longer be notified of changes.");
            } catch {
                TempData.AddDangerToast("We were unable to unsubscribe you from ticket updates. Were you originally subscribed?");
            }

            return(RedirectToAction("Show", "Tickets", new { ticketIdentifier = ticket.GetTicketIdentifier() }));
        }