| | | 1 | | using System.Text; |
| | | 2 | | using RoleRadar.Application.Abstractions; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | using RoleRadar.Domain.Notes; |
| | | 5 | | |
| | | 6 | | namespace RoleRadar.Application.Notes.Commands; |
| | | 7 | | |
| | | 8 | | public sealed record NoteUpdatedDto( |
| | | 9 | | Guid Id, |
| | | 10 | | Guid VersionToken, |
| | | 11 | | string BodyHash, |
| | | 12 | | int ByteLength, |
| | | 13 | | DateTime UpdatedAtUtc); |
| | | 14 | | |
| | 18 | 15 | | public sealed record AppendToNoteCommand( |
| | 44 | 16 | | Guid Id, |
| | 72 | 17 | | string TextToAppend, |
| | 44 | 18 | | string Actor, |
| | 34 | 19 | | string? OwnerId = null) : ICommand<NoteUpdatedDto>; |
| | | 20 | | |
| | | 21 | | public sealed class AppendToNoteCommandValidator : ICommandValidator<AppendToNoteCommand> |
| | | 22 | | { |
| | | 23 | | public ResultError? Validate(AppendToNoteCommand command) |
| | | 24 | | { |
| | | 25 | | ArgumentNullException.ThrowIfNull(command); |
| | | 26 | | |
| | | 27 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 28 | | if (actorError is not null) |
| | | 29 | | { |
| | | 30 | | return actorError; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | if (command.Id == Guid.Empty) |
| | | 34 | | { |
| | | 35 | | return new ResultError(ErrorCode.ValidationError, "Note ID cannot be Guid.Empty."); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | if (string.IsNullOrEmpty(command.TextToAppend)) |
| | | 39 | | { |
| | | 40 | | return new ResultError(ErrorCode.ValidationError, "TextToAppend cannot be empty."); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | var appendBytes = Encoding.UTF8.GetByteCount(command.TextToAppend); |
| | | 44 | | if (appendBytes > Note.MaxBodyBytes) |
| | | 45 | | { |
| | | 46 | | return new ResultError( |
| | | 47 | | ErrorCode.ValidationError, |
| | | 48 | | $"TextToAppend UTF-8 size ({appendBytes} bytes) exceeds maximum allowed ({Note.MaxBodyBytes} bytes / 8 M |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | return null; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public sealed class AppendToNoteCommandHandler : ICommandHandler<AppendToNoteCommand, NoteUpdatedDto> |
| | | 56 | | { |
| | | 57 | | private static readonly AppendToNoteCommandValidator Validator = new(); |
| | | 58 | | private readonly INoteRepository _repository; |
| | | 59 | | private readonly IUtcClock _clock; |
| | | 60 | | |
| | | 61 | | public AppendToNoteCommandHandler(INoteRepository repository, IUtcClock clock) |
| | | 62 | | { |
| | | 63 | | ArgumentNullException.ThrowIfNull(repository); |
| | | 64 | | ArgumentNullException.ThrowIfNull(clock); |
| | | 65 | | _repository = repository; |
| | | 66 | | _clock = clock; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<Result<NoteUpdatedDto>> HandleAsync( |
| | | 70 | | AppendToNoteCommand command, |
| | | 71 | | CancellationToken cancellationToken = default) |
| | | 72 | | { |
| | | 73 | | ArgumentNullException.ThrowIfNull(command); |
| | | 74 | | |
| | | 75 | | var validationError = Validator.Validate(command); |
| | | 76 | | if (validationError is not null) |
| | | 77 | | { |
| | | 78 | | return Result<NoteUpdatedDto>.Failure(validationError.Value); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | | 82 | | ? OwnerId.Default.Value |
| | | 83 | | : new OwnerId(command.OwnerId).Value; |
| | | 84 | | |
| | | 85 | | var actor = command.Actor.Trim(); |
| | | 86 | | var now = _clock.UtcNow; |
| | | 87 | | |
| | | 88 | | var outcome = await _repository.ExecuteInTransactionAsync( |
| | | 89 | | (tx, ct) => tx.AppendAsync(ownerId, command.Id, command.TextToAppend, actor, now, ct), |
| | | 90 | | cancellationToken); |
| | | 91 | | |
| | | 92 | | if (!outcome.IsSuccess) |
| | | 93 | | { |
| | | 94 | | return Result<NoteUpdatedDto>.Failure(outcome.Error!.Value); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | var updated = outcome.Value!.Note; |
| | | 98 | | return Result<NoteUpdatedDto>.Success(new NoteUpdatedDto( |
| | | 99 | | Id: updated.Id, |
| | | 100 | | VersionToken: updated.VersionToken, |
| | | 101 | | BodyHash: updated.CurrentBodyHash, |
| | | 102 | | ByteLength: Encoding.UTF8.GetByteCount(updated.Body), |
| | | 103 | | UpdatedAtUtc: updated.UpdatedAtUtc)); |
| | | 104 | | } |
| | | 105 | | } |