public static ProjectDetailsViewModel CreateNewViewModel(string currentUserId, Domain.Project project, ApplicationDbContext dbContext) { if (project == null) { throw new ArgumentNullException(nameof(project)); } try { return(new ProjectDetailsViewModel() { Id = project.Id == null ? throw new ArgumentNullException() : project.Id, Name = string.IsNullOrWhiteSpace(project.Name) ? throw new ArgumentNullException() : project.Name, Users = project.Users?.Select(user => HelperUserViewModel.CreateNewViewModel(user, dbContext)).ToList() ?? throw new ArgumentNullException(), TicketCount = project.Tickets?.Count ?? 0, Tickets = project.Tickets?.Select(ticket => TicketIndexViewModel.CreateNewViewModel(currentUserId, ticket)).ToList() ?? new List <TicketIndexViewModel>(), DateCreated = project.DateCreated == null ? throw new ArgumentNullException() : project.DateCreated, DateUpdated = project.DateUpdated, CurrentUserId = currentUserId, }); }
public static HomeIndexViewModel CreateNewViewModel( ApplicationUser applicationUser, ApplicationDbContext dbContext, int latestProjectIntakeLimit = 3, int latestCreatedTicketIntakeLimit = 3, int latestAssignedTicketIntakeLimit = 3) { if (applicationUser == null) { throw new ArgumentNullException(nameof(applicationUser)); } List <IdentityRole> roles = new UserRoleRepository(dbContext).GetUserRoles(applicationUser.Id); ProjectRepository projectRepository = new ProjectRepository(dbContext); List <ProjectIndexViewModel> latestProjects = projectRepository .GetUserProjects(applicationUser.Id)? .ToList() .OrderByDescending(project => project?.DateUpdated ?? project.DateCreated) .Take(latestProjectIntakeLimit) .Select(project => ProjectIndexViewModel.CreateNewViewModel(project)) .ToList() ?? new List <ProjectIndexViewModel>(); int numberOfCreatedTickets = 0; int numberOfAssignedTickets = 0; List <TicketIndexViewModel> latestCreatedTickets; List <TicketIndexViewModel> latestAssignedTickets; if (roles.Any(role => role.Name == nameof(UserRolesEnum.Submitter))) { //! get created tickets (Submitter) numberOfCreatedTickets = applicationUser.CreatedTickets?.Count ?? 0; latestCreatedTickets = applicationUser.CreatedTickets? .OrderByDescending(ticket => ticket?.DateUpdated ?? ticket.DateCreated) .Take(latestCreatedTicketIntakeLimit) .Select(ticket => TicketIndexViewModel.CreateNewViewModel(applicationUser.Id, ticket)) .ToList() ?? new List <TicketIndexViewModel>(); } else { //! if not (Submitter) latestCreatedTickets = new List <TicketIndexViewModel>(); } if (roles.Any(role => role.Name == nameof(UserRolesEnum.Developer))) { //! get assigned tickets (Developer) numberOfAssignedTickets = applicationUser.AssignedTickets?.Count ?? 0; latestAssignedTickets = applicationUser.AssignedTickets? .OrderByDescending(ticket => ticket?.DateUpdated ?? ticket.DateCreated) .Take(latestAssignedTicketIntakeLimit) .Select(ticket => TicketIndexViewModel.CreateNewViewModel(applicationUser.Id, ticket)) .ToList() ?? new List <TicketIndexViewModel>(); } else { //! if not (Developer) latestAssignedTickets = new List <TicketIndexViewModel>(); } TicketRepository ticketRepository = new TicketRepository(dbContext); try { return(new HomeIndexViewModel() { UserId = string.IsNullOrWhiteSpace(applicationUser.Id) ? throw new ArgumentNullException() : applicationUser.Id, DisplayName = string.IsNullOrWhiteSpace(applicationUser.DisplayName) ? throw new ArgumentNullException() : applicationUser.DisplayName, Email = string.IsNullOrWhiteSpace(applicationUser.Email) ? throw new ArgumentNullException() : applicationUser.Email, TotalProjectCount = projectRepository.GetUserProjects(applicationUser.Id)?.Count() ?? 0, LatestProjects = latestProjects?.Any() ?? false ? latestProjects : new List <ProjectIndexViewModel>(), Roles = (roles?.Any() ?? false) ? roles : new List <IdentityRole>(), TotalCreatedTicketCount = numberOfCreatedTickets, TotalAssignedTicketCount = numberOfAssignedTickets, LatestCreatedTickets = latestCreatedTickets, LatestAssignedTickets = latestAssignedTickets, AllProjectCount = projectRepository.GetAllProjects().Count(), //! NOTE: This depends on the table primary keys matching with the enum int value AllOpenTicketsCount = ticketRepository.GetAllTickets().Count(ticket => ticket.StatusId == (int)TicketStatusesEnum.Open), AllResovledTicketsCount = ticketRepository.GetAllTickets().Count(ticket => ticket.StatusId == (int)TicketStatusesEnum.Resolved), AllRejectedTicketsCount = ticketRepository.GetAllTickets().Count(ticket => ticket.StatusId == (int)TicketStatusesEnum.Rejected), }); }
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)); }