Пример #1
0
        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));
        }