Exemple #1
0
        public static HelperUserViewModel CreateNewViewModel(ApplicationUser applicationUser, ApplicationDbContext dbContext)
        {
            if (applicationUser == null)
            {
                throw new ArgumentNullException(nameof(applicationUser));
            }
            List <IdentityRole> roles = new UserRoleRepository(dbContext).GetUserRoles(applicationUser.Id);

            try
            {
                return(new HelperUserViewModel()
                {
                    Id = 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,
                    Roles = (roles?.Any() ?? false) ? roles : new List <IdentityRole>(),
                });
            }
        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),
                });
            }