選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UpdateAllUsersCommand.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using AutoMapper;
  2. using BlackRock.Reporting.API.Models;
  3. using MediatR;
  4. namespace BlackRock.Reporting.API.Mediator
  5. {
  6. public class UpdateAllUsersCommand : IRequest<Result<UserDto>>
  7. {
  8. public UserForm User { get; }
  9. public Guid Id { get; }
  10. public UpdateAllUsersCommand(Guid id, UserForm user)
  11. {
  12. this.Id = id;
  13. this.User = user;
  14. }
  15. }
  16. public class UpdateAllUsersCommandHandlers : IRequestHandler<UpdateAllUsersCommand, Result<UserDto>>
  17. {
  18. private readonly ILogger<UpdateAllUsersCommandHandlers> logger;
  19. private readonly IMapper mapper;
  20. public UpdateAllUsersCommandHandlers(ILogger<UpdateAllUsersCommandHandlers> logger, IMapper mapper)
  21. {
  22. this.mapper = mapper;
  23. this.logger = logger;
  24. }
  25. public async Task<Result<UserDto>> Handle(UpdateAllUsersCommand command, CancellationToken cancellationToken)
  26. {
  27. if (command.Id == Guid.Empty)
  28. throw new ArgumentException($"Parameter {nameof(command.Id)} must not be null or empty");
  29. logger.LogInformation("Updating user ...");
  30. try
  31. {
  32. var user = new User();
  33. // var user = repository.GetUserByIdAsync(id);
  34. // mapper.Map<UserForm, User>(command.User,user)
  35. // await unitOfWork.SaveChangesAsync();
  36. var updatedUser = mapper.Map<User, UserDto>(user);
  37. return new Result<UserDto> { Data = updatedUser };
  38. }
  39. catch (Exception ex)
  40. {
  41. return new Result<UserDto> { IsSuccess = false, Error = "Faild to update data in DB." };
  42. }
  43. }
  44. }
  45. }