| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using AutoMapper;
- using BlackRock.Reporting.API.Models;
- using MediatR;
-
- namespace BlackRock.Reporting.API.Mediator
- {
- public class UpdateAllUsersCommand : IRequest<Result<UserDto>>
- {
- public UserForm User { get; }
- public Guid Id { get; }
- public UpdateAllUsersCommand(Guid id, UserForm user)
- {
- this.Id = id;
- this.User = user;
- }
- }
-
- public class UpdateAllUsersCommandHandlers : IRequestHandler<UpdateAllUsersCommand, Result<UserDto>>
- {
- private readonly ILogger<UpdateAllUsersCommandHandlers> logger;
- private readonly IMapper mapper;
- public UpdateAllUsersCommandHandlers(ILogger<UpdateAllUsersCommandHandlers> logger, IMapper mapper)
- {
- this.mapper = mapper;
- this.logger = logger;
- }
- public async Task<Result<UserDto>> Handle(UpdateAllUsersCommand command, CancellationToken cancellationToken)
- {
- if (command.Id == Guid.Empty)
- throw new ArgumentException($"Parameter {nameof(command.Id)} must not be null or empty");
- logger.LogInformation("Updating user ...");
- try
- {
- var user = new User();
- // var user = repository.GetUserByIdAsync(id);
- // mapper.Map<UserForm, User>(command.User,user)
- // await unitOfWork.SaveChangesAsync();
- var updatedUser = mapper.Map<User, UserDto>(user);
- return new Result<UserDto> { Data = updatedUser };
- }
- catch (Exception ex)
- {
- return new Result<UserDto> { IsSuccess = false, Error = "Faild to update data in DB." };
- }
- }
- }
- }
|