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); }
public async Task<ActionResult<StatusModel>> GetAllStatusAsync([FromQuery] GetAllStatusQuery request) { try { request.SetUser(User.GetUserId()); var response = await _mediator.Send(request); return Ok(response); } catch (Exception ex) { _logger.Error(ex, $"Operation failed into controller {Routes.Get_All_Status} with message: {ex.Message}"); return BadRequest(ex.Message); } }