public async Task <Tuple <IEnumerable <WorkerModel>, int> > Handle(GetAllWorkersQuery request, CancellationToken cancellationToken) { var user = await userStore.FindByIdAsync(request.GetUser(), cancellationToken); if (user == null) { throw new UserDoesNotExistException(); } var workersByTeamDb = await _workersByTeamRepository.GetAllWorkersWithTeamInfo(); var response = workersByTeamDb .Select(x => x.Worker) .Distinct() .Select(item => new WorkerModel { WorkerId = item.WorkerId, FirstName = item.FirstName, LastName = item.LastName, Email = item.Email, Birthday = item.Birthday, PhotoUrl = item.PhotoUrl, StatusId = item.StatusId, Status = item.Status.Name, RoleId = item.RoleId, Role = item.Role.Name, LastModificationTime = item.LastModificationTime, IsBirthdayToday = VerifyBirthday(DateTime.Now, item.Birthday), Teams = workersByTeamDb.Where(x => x.WorkerId == item.WorkerId) .Select(x => new TeamModel(x.Team.TeamId, x.Team.Name)) .ToList() }) .ToList(); if (NeedToFilter(request)) { response = FilterResults(request, response); } return(new Tuple <IEnumerable <WorkerModel>, int>(response .Skip(PaginationHelper.GetSkipRows(request)) .Take(request.PageSize), response.Count)); }
public async Task <ActionResult <WorkerModel> > GetAllWorkersAsync([FromQuery] GetAllWorkersQuery request) { try { request.SetUser(User.GetUserId()); var response = await _mediator.Send(request); return(Ok(new PagedResponse <WorkerModel>( data: response.Item1, pageNumber: request.PageNumber, totalResults: response.Item2, pageSize: request.PageSize))); } catch (Exception ex) { _logger.Error(ex, $"Operation failed into controller {Routes.Get_All_Workers} with message: {ex.Message}"); return(BadRequest(ex.Message)); } }
private List <WorkerModel> FilterResults(GetAllWorkersQuery request, List <WorkerModel> response) { if (request.StatusId.HasValue) { response = response.Where(x => x.StatusId == request.StatusId).ToList(); } if (request.RoleId.HasValue) { response = response.Where(x => x.RoleId == request.RoleId).ToList(); } if (request.TeamId.HasValue) { response = response.Where(x => x.Teams.Any(y => y.TeamId == request.TeamId.Value)).ToList(); } if (!string.IsNullOrWhiteSpace(request.NameToSearch)) { var searchWords = request.NameToSearch.Split(' ') .Select(tag => tag.Trim()) .Where(tag => !string.IsNullOrEmpty(tag)); if (searchWords.Count() > 1) { var fullKeyword = string.Concat(searchWords); response = response.Where(x => EF.Functions.Like(string.Concat(x.FirstName, x.LastName), $"%{fullKeyword}%")) .ToList(); } else { response = response.Where(x => EF.Functions.Like(x.FirstName, $"%{request.NameToSearch.Trim()}%") || EF.Functions.Like(x.LastName, $"%{request.NameToSearch.Trim()}%")) .ToList(); } } return(response); }
private static bool NeedToFilter(GetAllWorkersQuery request) => request.StatusId.HasValue || request.RoleId.HasValue || request.TeamId.HasValue || !string.IsNullOrWhiteSpace(request.NameToSearch);
public static int GetSkipRows(GetAllWorkersQuery request) { return((request.PageNumber - 1) * request.PageSize); }