using AutoMapper; using BlackRock.Reporting.API.Core; using BlackRock.Reporting.API.Mediator.Model; using BlackRock.Reporting.API.Models; using MediatR; namespace BlackRock.Reporting.API.Mediator { public class CreateUsersCommand : UserCommand, IRequest> { public CreateUsersCommand(string Name, string Email) : base(Name, Email) { } } public class CreateUsersCommandHandlers : IRequestHandler> { private readonly ILogger logger; private readonly IMapper mapper; private readonly IUnitOfWork unitOfWork; public CreateUsersCommandHandlers(ILogger logger, IMapper mapper, IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; this.mapper = mapper; this.logger = logger; } public async Task> Handle(CreateUsersCommand command, CancellationToken cancellationToken) { if (command is null) throw new ArgumentException($"Parameter {nameof(command)} must not be null"); try { logger.LogInformation("Creating new user ..."); var user = mapper.Map(command); await unitOfWork.UsersRepository.AddAsync(user); await unitOfWork.SaveChangesAsync(); logger.LogInformation($"User with id {user.Id} has been created successfully"); return new Result { Data = user.Id}; } catch (Exception ex) { logger.LogError(ex,"Faild to add data to DB."); return new Result { IsSuccess = false, Error = "Faild to add data to DB." }; } } } }