| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using AutoMapper;
- using BlackRock.Reporting.API.Mediator.UserMediator.Dto;
- using BlackRock.Reporting.API.Mediator.UserMediator.Model;
- using BlackRock.Reporting.API.Core.Interfaces;
- using BlackRock.Reporting.API.Core.Models;
- using MediatR;
-
- namespace BlackRock.Reporting.API.Mediator.UserMediator.Commands
- {
- public class UpdateUserEmailCommand : IRequest<Result<UserDto>>
- {
- public UserCommand User { set; get; }
- public int Id { get; set; }
-
- }
-
- public class UpdateUserEmailCommandHandlers : IRequestHandler<UpdateUserEmailCommand, Result<UserDto>>
- {
- private readonly ILogger<UpdateUserEmailCommandHandlers> logger;
- private readonly IMapper mapper;
- private readonly IUnitOfWork unitOfWork;
- public UpdateUserEmailCommandHandlers(ILogger<UpdateUserEmailCommandHandlers> logger, IMapper mapper, IUnitOfWork unitOfWork)
- {
- this.unitOfWork = unitOfWork;
- this.mapper = mapper;
- this.logger = logger;
- }
- public async Task<Result<UserDto>> Handle(UpdateUserEmailCommand command, CancellationToken cancellationToken)
- {
- if (command.Id <= 0)
- throw new ArgumentException($"Parameter {nameof(command.Id)} must not be grater than 0");
- logger.LogInformation("Updating user email ...");
- try
- {
- var user = await unitOfWork.UsersRepository.GetByIdAsync(command.Id);
- unitOfWork.UsersRepository.UpdateEmail(user, command.User.Email);
- await unitOfWork.SaveChangesAsync();
- var updatedUser = mapper.Map<User, UserDto>(user);
- logger.LogInformation($"Email of the user with id {user.Id} has been updated successfully");
- return new Result<UserDto> { Data = updatedUser };
- }
- catch (Exception ex)
- {
- return new Result<UserDto> { IsSuccess = false, Error = "Faild to update data in DB." };
- }
- }
- }
- }
|