| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using AutoMapper;
- using BlackRock.Reporting.API.Core.Interfaces;
- using BlackRock.Reporting.API.Core.Models;
- using BlackRock.Reporting.API.Mediator.UserMediator.Model;
- using BlackRock.Reporting.API.Mediator.UserMediator.Dto;
- using MediatR;
-
- namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries
- {
- public class GetAllUsersQuery : UsersFilter, IRequest<Result<PagedCollection<UserDto>>>
- {
- }
-
- public class GetAllUsersQueryHandlers : IRequestHandler<GetAllUsersQuery, Result<PagedCollection<UserDto>>>
- {
- private readonly ILogger<GetAllUsersQueryHandlers> logger;
- private readonly IMapper mapper;
- private readonly IUnitOfWork unitOfWork;
- public GetAllUsersQueryHandlers(ILogger<GetAllUsersQueryHandlers> logger, IMapper mapper, IUnitOfWork unitOfWork)
- {
- this.unitOfWork = unitOfWork;
- this.mapper = mapper;
- this.logger = logger;
- }
- public async Task<Result<PagedCollection<UserDto>>> Handle(GetAllUsersQuery command, CancellationToken cancellationToken)
- {
- if (command == null)
- throw new ArgumentNullException($"Parameter {nameof(command)} must not be null");
- logger.LogInformation("Getting users ...");
- try
- {
- var users = await unitOfWork.UsersRepository.GetAllByFilter(command);
- var usersDto = mapper.Map<PagedCollection<User>, PagedCollection<UserDto>>(users);
- logger.LogInformation($"The Users has been founded successfully");
- return new Result<PagedCollection<UserDto>> { Data = usersDto };
- }
- catch (Exception ex)
- {
- return new Result<PagedCollection<UserDto>> { IsSuccess = false, Error = "Faild to fetch data from DB." };
- }
- }
- }
- }
|