public async Task <UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken) { var user = await _userRepository.GetAsync(e => e.Id == request.Id); if (user != null) { _logger.LogInformation($"Got a request get user Id: {user.Id}"); var userDto = _userDxos.MapUserDto(user); return(userDto); } return(null); }
public async Task <UserDto> Handle(UpdateUserCommand request, CancellationToken cancellationToken) { var user = _userDxos.MapUpdateRequesttoUser(request); _userRepository.Update(user); if (await _userRepository.SaveChangesAsync() == 0) { throw new ApplicationException("Update in database failed"); } await _mediator.Publish(new UserUpdatedEvent(user.Id), cancellationToken); var userDto = _userDxos.MapUserDto(user); return(userDto); }
public async Task <UserDto> Handle(CreateUserCommand request, CancellationToken cancellationToken) { if (await _userRepository.EmailExistAsync(request.Email)) { throw new ArgumentException($"A user with the email {request.Email} already exist!", nameof(request.Email)); } var user = _userDxos.MapCreateRequesttoUser(request); _userRepository.Add(user); if (await _userRepository.SaveChangesAsync() == 0) { throw new ApplicationException("Insertion to database failed"); } await _mediator.Publish(new UserCreatedEvent(user.Id), cancellationToken); var userDto = _userDxos.MapUserDto(user); return(userDto); }