| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using AutoMapper;
- using BlackRock.Reporting.API.Core;
- using BlackRock.Reporting.API.Models;
- using MediatR;
-
- namespace BlackRock.Reporting.API.Mediator
- {
- public class GetUsersQuery : IRequest<Result<UserDto>>
- {
- public Guid Id { get; }
- public GetUsersQuery(Guid id)
- {
- this.Id = id;
- }
- }
-
- public class GetUsersQueryHandlers : IRequestHandler<GetUsersQuery, Result<UserDto>>
- {
- private readonly ILogger<GetUsersQueryHandlers> logger;
- private readonly IMapper mapper;
- private readonly IUsersRepository repository;
- private readonly IUnitOfWork unitOfWork;
- public GetUsersQueryHandlers(ILogger<GetUsersQueryHandlers> logger, IMapper mapper, IUsersRepository repository, IUnitOfWork unitOfWork)
- {
- this.unitOfWork = unitOfWork;
- this.repository = repository;
- this.mapper = mapper;
- this.logger = logger;
- }
- public async Task<Result<UserDto>> Handle(GetUsersQuery command, CancellationToken cancellationToken)
- {
- if (command.Id == Guid.Empty)
- throw new ArgumentException($"Parameter {nameof(command.Id)} must not be null or empty");
- logger.LogInformation("Getting user ...");
- try
- {
- var user = await repository.GetByIdAsync(command.Id);
- var userDto = mapper.Map<User, UserDto>(user);
- logger.LogInformation($"The user with id {user.Id} has been founded successfully");
- return new Result<UserDto> { Data = userDto };
- }
- catch (Exception ex)
- {
- return new Result<UserDto> { IsSuccess = false, Error = "Faild to fetch data from DB." };
- }
- }
- }
- }
|