Beispiel #1
0
        public async Task <List <StatusModel> > Handle(GetAllStatusQuery request, CancellationToken cancellationToken)
        {
            var user = await userStore.FindByIdAsync(request.GetUser(), cancellationToken);

            if (user == null)
            {
                throw new UserDoesNotExistException();
            }

            var statusDb = await _statusRepository.GetAllStatusAsync();

            var workersDb = await _workerRepository.GetAllWorkersAsync();

            var groupByStatusId = workersDb
                                  .GroupBy(x => x.Status)
                                  .Select(w => new StatusModel(w.Key.StatusId, w.Count())
                                          );

            var response = statusDb.Select(x =>
                                           new StatusModel(statusId: x.StatusId,
                                                           statusName: x.Name)
            {
                TotalWorkers = groupByStatusId.Any(y => y.StatusId == x.StatusId)
                                                        ? groupByStatusId.Where(y => y.StatusId == x.StatusId).Select(y => y.TotalWorkers).FirstOrDefault()
                                                        : 0
            })
                           .ToList();

            return(response);
        }
Beispiel #2
0
        public async Task <List <RoleModel> > Handle(GetAllRolesQuery request, CancellationToken cancellationToken)
        {
            var user = await userStore.FindByIdAsync(request.GetUser(), cancellationToken);

            if (user == null)
            {
                throw new Exceptions.UserDoesNotExistException();
            }

            var roleDb = await _roleRepository.GetAllRolesAsync();

            var workersDb = await _workerRepository.GetAllWorkersAsync();


            var groupByRoleId = workersDb
                                .GroupBy(x => x.Role)
                                .Select(w => new RoleModel(w.Key.RoleId, w.Count())
                                        );

            var response = roleDb.Select(x =>
                                         new RoleModel(roleId: x.RoleId,
                                                       roleName: x.Name,
                                                       abbreviation: x.Abbreviation)
            {
                TotalWorkers = groupByRoleId.Any(y => y.RoleId == x.RoleId)
                                                        ? groupByRoleId.Where(y => y.RoleId == x.RoleId).Select(y => y.TotalWorkers).FirstOrDefault()
                                                        : 0
            })
                           .ToList();

            return(response);
        }