| | | 1 | | using System.Text; |
| | | 2 | | using RoleRadar.Application.Abstractions; |
| | | 3 | | using RoleRadar.Domain.Audit; |
| | | 4 | | using RoleRadar.Domain.Common; |
| | | 5 | | using RoleRadar.Domain.Notes; |
| | | 6 | | |
| | | 7 | | namespace RoleRadar.Application.Notes.Commands; |
| | | 8 | | |
| | | 9 | | public sealed record NoteSectionReplacedDto( |
| | | 10 | | Guid Id, |
| | | 11 | | Guid VersionToken, |
| | | 12 | | Guid RevisionId, |
| | | 13 | | int RevisionCount, |
| | | 14 | | string BodyHash, |
| | | 15 | | DateTime UpdatedAtUtc); |
| | | 16 | | |
| | | 17 | | public sealed record ReplaceNoteSectionCommand( |
| | | 18 | | Guid Id, |
| | | 19 | | string Heading, |
| | | 20 | | string NewSectionBody, |
| | | 21 | | Guid VersionToken, |
| | | 22 | | string Actor, |
| | | 23 | | string? OwnerId = null) : ICommand<NoteSectionReplacedDto>; |
| | | 24 | | |
| | | 25 | | public sealed class ReplaceNoteSectionCommandValidator : ICommandValidator<ReplaceNoteSectionCommand> |
| | | 26 | | { |
| | | 27 | | public ResultError? Validate(ReplaceNoteSectionCommand command) |
| | | 28 | | { |
| | | 29 | | ArgumentNullException.ThrowIfNull(command); |
| | | 30 | | |
| | | 31 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 32 | | if (actorError is not null) |
| | | 33 | | { |
| | | 34 | | return actorError; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | if (command.Id == Guid.Empty) |
| | | 38 | | { |
| | | 39 | | return new ResultError(ErrorCode.ValidationError, "Note ID cannot be Guid.Empty."); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | if (string.IsNullOrWhiteSpace(command.Heading)) |
| | | 43 | | { |
| | | 44 | | return new ResultError(ErrorCode.ValidationError, "Target heading is required and cannot be blank."); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | if (command.NewSectionBody is null) |
| | | 48 | | { |
| | | 49 | | return new ResultError(ErrorCode.ValidationError, "NewSectionBody is required."); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | var sectionBytes = Encoding.UTF8.GetByteCount(command.NewSectionBody); |
| | | 53 | | if (sectionBytes > Note.MaxBodyBytes) |
| | | 54 | | { |
| | | 55 | | return new ResultError( |
| | | 56 | | ErrorCode.ValidationError, |
| | | 57 | | $"NewSectionBody UTF-8 size ({sectionBytes} bytes) exceeds maximum allowed ({Note.MaxBodyBytes} bytes / |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | if (command.VersionToken == Guid.Empty) |
| | | 61 | | { |
| | | 62 | | return new ResultError(ErrorCode.ValidationError, "VersionToken cannot be Guid.Empty."); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | return null; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public sealed class ReplaceNoteSectionCommandHandler : ICommandHandler<ReplaceNoteSectionCommand, NoteSectionReplacedDto |
| | | 70 | | { |
| | 4 | 71 | | private static readonly ReplaceNoteSectionCommandValidator Validator = new(); |
| | | 72 | | private readonly INoteRepository _repository; |
| | | 73 | | private readonly IUtcClock _clock; |
| | | 74 | | |
| | 112 | 75 | | public ReplaceNoteSectionCommandHandler(INoteRepository repository, IUtcClock clock) |
| | | 76 | | { |
| | 112 | 77 | | ArgumentNullException.ThrowIfNull(repository); |
| | 112 | 78 | | ArgumentNullException.ThrowIfNull(clock); |
| | 112 | 79 | | _repository = repository; |
| | 112 | 80 | | _clock = clock; |
| | 112 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<Result<NoteSectionReplacedDto>> HandleAsync( |
| | | 84 | | ReplaceNoteSectionCommand command, |
| | | 85 | | CancellationToken cancellationToken = default) |
| | | 86 | | { |
| | 16 | 87 | | ArgumentNullException.ThrowIfNull(command); |
| | | 88 | | |
| | 16 | 89 | | var validationError = Validator.Validate(command); |
| | 16 | 90 | | if (validationError is not null) |
| | | 91 | | { |
| | 2 | 92 | | return Result<NoteSectionReplacedDto>.Failure(validationError.Value); |
| | | 93 | | } |
| | | 94 | | |
| | 14 | 95 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | 14 | 96 | | ? OwnerId.Default.Value |
| | 14 | 97 | | : new OwnerId(command.OwnerId).Value; |
| | | 98 | | |
| | 14 | 99 | | var actor = command.Actor.Trim(); |
| | 14 | 100 | | var now = _clock.UtcNow; |
| | | 101 | | |
| | 14 | 102 | | var outcome = await _repository.ExecuteInTransactionAsync( |
| | 14 | 103 | | async (tx, ct) => |
| | 14 | 104 | | { |
| | 14 | 105 | | var existingResult = await _repository.GetByIdAsync(ownerId, command.Id, ct); |
| | 14 | 106 | | if (!existingResult.IsSuccess) |
| | 14 | 107 | | { |
| | 0 | 108 | | return Result<NoteWriteOutcome>.Failure(existingResult.Error!.Value); |
| | 14 | 109 | | } |
| | 14 | 110 | | |
| | 14 | 111 | | var note = existingResult.Value!; |
| | 14 | 112 | | if (note.VersionToken != command.VersionToken) |
| | 14 | 113 | | { |
| | 4 | 114 | | return Result<NoteWriteOutcome>.Failure( |
| | 4 | 115 | | ErrorCode.StaleVersionToken, |
| | 4 | 116 | | $"Note '{command.Id}' was modified concurrently. Expected version token '{command.VersionToken}' |
| | 4 | 117 | | new Dictionary<string, object?> |
| | 4 | 118 | | { |
| | 4 | 119 | | ["currentVersionToken"] = note.VersionToken.ToString() |
| | 4 | 120 | | }); |
| | 14 | 121 | | } |
| | 14 | 122 | | |
| | 10 | 123 | | var sectionOutcome = MarkdownSectionParser.ReplaceSection( |
| | 10 | 124 | | note.Body, |
| | 10 | 125 | | command.Heading, |
| | 10 | 126 | | command.NewSectionBody); |
| | 14 | 127 | | |
| | 10 | 128 | | if (sectionOutcome.Status == SectionReplacementStatus.SectionNotFound) |
| | 14 | 129 | | { |
| | 2 | 130 | | return Result<NoteWriteOutcome>.Failure( |
| | 2 | 131 | | ErrorCode.SectionNotFound, |
| | 2 | 132 | | $"Heading '{command.Heading}' was not found in note '{command.Id}'.", |
| | 2 | 133 | | new Dictionary<string, object?> |
| | 2 | 134 | | { |
| | 2 | 135 | | ["availableHeadings"] = sectionOutcome.AvailableHeadings |
| | 2 | 136 | | }); |
| | 14 | 137 | | } |
| | 14 | 138 | | |
| | 8 | 139 | | if (sectionOutcome.Status == SectionReplacementStatus.AmbiguousHeading) |
| | 14 | 140 | | { |
| | 0 | 141 | | return Result<NoteWriteOutcome>.Failure( |
| | 0 | 142 | | ErrorCode.AmbiguousHeading, |
| | 0 | 143 | | $"Heading '{command.Heading}' matched multiple sections in note '{command.Id}'. Disambiguate usi |
| | 0 | 144 | | new Dictionary<string, object?> |
| | 0 | 145 | | { |
| | 0 | 146 | | ["matchingHeadings"] = sectionOutcome.MatchingHeadings, |
| | 0 | 147 | | ["availableHeadings"] = sectionOutcome.AvailableHeadings |
| | 0 | 148 | | }); |
| | 14 | 149 | | } |
| | 14 | 150 | | |
| | 8 | 151 | | var resultingBytes = Encoding.UTF8.GetByteCount(sectionOutcome.UpdatedBody!); |
| | 8 | 152 | | if (resultingBytes > Note.MaxBodyBytes) |
| | 14 | 153 | | { |
| | 2 | 154 | | return Result<NoteWriteOutcome>.Failure( |
| | 2 | 155 | | ErrorCode.ValidationError, |
| | 2 | 156 | | $"Resulting note body UTF-8 size ({resultingBytes} bytes) after section replacement exceeds maxi |
| | 14 | 157 | | } |
| | 14 | 158 | | |
| | 6 | 159 | | var beforeSummary = AuditContentSummary.FromNote(note); |
| | 6 | 160 | | var revision = note.ReplaceBodyWithRevision( |
| | 6 | 161 | | newBody: sectionOutcome.UpdatedBody!, |
| | 6 | 162 | | reason: NoteRevision.ReasonReplaceSection, |
| | 6 | 163 | | occurredAtUtc: now); |
| | 14 | 164 | | |
| | 6 | 165 | | var afterSummary = AuditContentSummary.FromNote(note); |
| | 6 | 166 | | var audit = AuditLogEntry.CreateForReplaceSection( |
| | 6 | 167 | | ownerId: ownerId, |
| | 6 | 168 | | actor: actor, |
| | 6 | 169 | | targetNoteId: note.Id, |
| | 6 | 170 | | noteRevisionId: revision.Id, |
| | 6 | 171 | | resultingVersionToken: note.VersionToken, |
| | 6 | 172 | | occurredAtUtc: note.UpdatedAtUtc, |
| | 6 | 173 | | beforeSummary: beforeSummary, |
| | 6 | 174 | | afterSummary: afterSummary); |
| | 14 | 175 | | |
| | 6 | 176 | | return await tx.ReplaceSectionAsync(note, command.VersionToken, revision, audit, ct); |
| | 14 | 177 | | }, |
| | 14 | 178 | | cancellationToken); |
| | | 179 | | |
| | 14 | 180 | | if (!outcome.IsSuccess) |
| | | 181 | | { |
| | 8 | 182 | | return Result<NoteSectionReplacedDto>.Failure(outcome.Error!.Value); |
| | | 183 | | } |
| | | 184 | | |
| | 6 | 185 | | var updated = outcome.Value!.Note; |
| | 6 | 186 | | var savedRevision = outcome.Value.Revision!; |
| | 6 | 187 | | return Result<NoteSectionReplacedDto>.Success(new NoteSectionReplacedDto( |
| | 6 | 188 | | Id: updated.Id, |
| | 6 | 189 | | VersionToken: updated.VersionToken, |
| | 6 | 190 | | RevisionId: savedRevision.Id, |
| | 6 | 191 | | RevisionCount: updated.RevisionCount, |
| | 6 | 192 | | BodyHash: updated.CurrentBodyHash, |
| | 6 | 193 | | UpdatedAtUtc: updated.UpdatedAtUtc)); |
| | 16 | 194 | | } |
| | | 195 | | } |