Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GetAllUsersQuery.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using AutoMapper;
  2. using BlackRock.Reporting.API.Core.Interfaces;
  3. using BlackRock.Reporting.API.Core.Models;
  4. using BlackRock.Reporting.API.Mediator.UserMediator.Model;
  5. using BlackRock.Reporting.API.Mediator.UserMediator.Dto;
  6. using MediatR;
  7. namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries
  8. {
  9. public class GetAllUsersQuery : UsersFilter, IRequest<Result<PagedCollection<UserDto>>>
  10. {
  11. }
  12. public class GetAllUsersQueryHandlers : IRequestHandler<GetAllUsersQuery, Result<PagedCollection<UserDto>>>
  13. {
  14. private readonly ILogger<GetAllUsersQueryHandlers> logger;
  15. private readonly IMapper mapper;
  16. private readonly IUnitOfWork unitOfWork;
  17. public GetAllUsersQueryHandlers(ILogger<GetAllUsersQueryHandlers> logger, IMapper mapper, IUnitOfWork unitOfWork)
  18. {
  19. this.unitOfWork = unitOfWork;
  20. this.mapper = mapper;
  21. this.logger = logger;
  22. }
  23. public async Task<Result<PagedCollection<UserDto>>> Handle(GetAllUsersQuery command, CancellationToken cancellationToken)
  24. {
  25. if (command == null)
  26. throw new ArgumentNullException($"Parameter {nameof(command)} must not be null");
  27. logger.LogInformation("Getting users ...");
  28. try
  29. {
  30. var users = await unitOfWork.UsersRepository.GetAllByFilter(command);
  31. var usersDto = mapper.Map<PagedCollection<User>, PagedCollection<UserDto>>(users);
  32. logger.LogInformation($"The Users has been founded successfully");
  33. return new Result<PagedCollection<UserDto>> { Data = usersDto };
  34. }
  35. catch (Exception ex)
  36. {
  37. return new Result<PagedCollection<UserDto>> { IsSuccess = false, Error = "Faild to fetch data from DB." };
  38. }
  39. }
  40. }
  41. }