public async Task Invoke(HttpContext context) { var correlationID = _guidGenerator.GetNextId().Value; using (LogContext.PushProperty(CorrelationIDPropertyName, correlationID)) { await _next.Invoke(context); } }
public async Task <Result <int> > Handle(RegisterUserCommand request, CancellationToken cancellationToken) { var nicknameValidationResult = Nickname.Create(request.Nickname); var eMailValidationResult = Email.Create(request.Email); var passwordValidationResult = Password.Create(request.Password); var fullNameValidationResult = FullName.Create(request.Name, request.Surname); var validationResult = Result.Combine(nicknameValidationResult, eMailValidationResult, passwordValidationResult, fullNameValidationResult); if (validationResult.IsFailure) { _logger.Warning("Error during registration of user {Nickname}: Error message: {ErrorMessage}", request.Nickname, validationResult.Error); return(Result.Failure <int>(validationResult.Error)); } var userToRegister = new User( nicknameValidationResult.Value, fullNameValidationResult.Value, passwordValidationResult.Value, eMailValidationResult.Value, request.Biography); if (request.ProfilePicture != null) { var profilePicture = Picture.Create(request.ProfilePicture, _idGenerator.GetNextId()).Value; userToRegister.SetProfilePicture(profilePicture); } using (var tx = _session.BeginTransaction()) { try { await _session.SaveAsync(userToRegister, cancellationToken); await tx.CommitAsync(cancellationToken); _logger.Information("User [{Nickname}({UserID})] has just registered.", request.Nickname, userToRegister.ID); return(Result.Success(userToRegister.ID)); } catch (ADOException ex) { await tx.RollbackAsync(cancellationToken); _logger.Error("Error during registration of user {Nickname}. Error message: {ErrorMessage}", request.Nickname, ex.Message); return(Result.Failure <int>(ex.Message)); } } }
public async Task <Result <int> > Handle(PublishPostCommand request, CancellationToken cancellationToken) { using (var tx = _session.BeginTransaction()) { User author = null; try { author = await _session.LoadAsync <User>(request.UserID, cancellationToken); var postPicture = Picture.Create(request.PictureRawBytes, _idGenerator.GetNextId()).Value; var post = new Post(author, postPicture, (PostText)request.Text); await _session.SaveAsync(post, cancellationToken); await tx.CommitAsync(cancellationToken); _logger.Information("User [{Nickname}({UserID})] has just shared a new post.", author.Nickname, request.UserID); return(Result.Success(post.ID)); } catch (ADOException ex) { await tx.RollbackAsync(cancellationToken); _logger.Error("Failed to publish a post for user [{Nickname}({UserID})]. Error message: {ErrorMessage}", author?.Nickname, request.UserID, ex.Message); return(Result.Failure <int>(ex.Message)); } } }