| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using RoleRadar.Application.Abstractions; |
| | | 4 | | using RoleRadar.Domain.Audit; |
| | | 5 | | using RoleRadar.Domain.Common; |
| | | 6 | | using RoleRadar.Domain.Notes; |
| | | 7 | | |
| | | 8 | | namespace RoleRadar.Application.Notes.Commands; |
| | | 9 | | |
| | | 10 | | public sealed record ProvenanceDto( |
| | | 11 | | string Kind, |
| | | 12 | | string Date, |
| | | 13 | | [property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] string? Qualifier); |
| | | 14 | | |
| | | 15 | | public sealed record NoteCreatedDto( |
| | | 16 | | Guid Id, |
| | | 17 | | string Title, |
| | | 18 | | string Area, |
| | | 19 | | IReadOnlyList<string> Tags, |
| | | 20 | | ProvenanceDto Provenance, |
| | | 21 | | string Status, |
| | | 22 | | Guid VersionToken, |
| | | 23 | | string BodyHash, |
| | | 24 | | DateTime CreatedAtUtc, |
| | | 25 | | DateTime UpdatedAtUtc); |
| | | 26 | | |
| | 134 | 27 | | public sealed record CreateNoteCommand( |
| | 566 | 28 | | string Title, |
| | 564 | 29 | | string Body, |
| | 554 | 30 | | string Area, |
| | 750 | 31 | | IReadOnlyList<string>? Tags, |
| | 328 | 32 | | string ProvenanceKind, |
| | 330 | 33 | | string ProvenanceDate, |
| | 116 | 34 | | string? ProvenanceQualifier, |
| | 344 | 35 | | string Actor, |
| | 326 | 36 | | string? OwnerId = null) : ICommand<NoteCreatedDto>; |
| | | 37 | | |
| | | 38 | | public sealed class CreateNoteCommandValidator : ICommandValidator<CreateNoteCommand> |
| | | 39 | | { |
| | | 40 | | public ResultError? Validate(CreateNoteCommand command) |
| | | 41 | | { |
| | | 42 | | ArgumentNullException.ThrowIfNull(command); |
| | | 43 | | |
| | | 44 | | var actorError = CommandActor.Validate(command.Actor); |
| | | 45 | | if (actorError is not null) |
| | | 46 | | { |
| | | 47 | | return actorError; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | if (string.IsNullOrWhiteSpace(command.Title)) |
| | | 51 | | { |
| | | 52 | | return new ResultError(ErrorCode.ValidationError, "Note title is required and cannot be blank."); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | var trimmedTitleLength = command.Title.Trim().Length; |
| | | 56 | | if (trimmedTitleLength > Note.MaxTitleChars) |
| | | 57 | | { |
| | | 58 | | return new ResultError( |
| | | 59 | | ErrorCode.ValidationError, |
| | | 60 | | $"Note title length ({trimmedTitleLength} characters) exceeds maximum allowed ({Note.MaxTitleChars} char |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | if (command.Body is null) |
| | | 64 | | { |
| | | 65 | | return new ResultError(ErrorCode.ValidationError, "Note body is required."); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | var bodyByteCount = Encoding.UTF8.GetByteCount(command.Body); |
| | | 69 | | if (bodyByteCount > Note.MaxBodyBytes) |
| | | 70 | | { |
| | | 71 | | return new ResultError( |
| | | 72 | | ErrorCode.ValidationError, |
| | | 73 | | $"Note body UTF-8 size ({bodyByteCount} bytes) exceeds maximum allowed ({Note.MaxBodyBytes} bytes / 8 Mi |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | if (string.IsNullOrWhiteSpace(command.Area)) |
| | | 77 | | { |
| | | 78 | | return new ResultError(ErrorCode.ValidationError, "Note area is required and cannot be blank."); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | var trimmedAreaLength = command.Area.Trim().Length; |
| | | 82 | | if (trimmedAreaLength > Note.MaxAreaChars) |
| | | 83 | | { |
| | | 84 | | return new ResultError( |
| | | 85 | | ErrorCode.ValidationError, |
| | | 86 | | $"Note area length ({trimmedAreaLength} characters) exceeds maximum allowed ({Note.MaxAreaChars} charact |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | if (command.Tags is not null) |
| | | 90 | | { |
| | | 91 | | if (command.Tags.Count > Note.MaxTagCount) |
| | | 92 | | { |
| | | 93 | | return new ResultError( |
| | | 94 | | ErrorCode.ValidationError, |
| | | 95 | | $"Tag count ({command.Tags.Count}) exceeds maximum allowed ({Note.MaxTagCount} tags)."); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | foreach (var rawTag in command.Tags) |
| | | 99 | | { |
| | | 100 | | if (string.IsNullOrWhiteSpace(rawTag)) |
| | | 101 | | { |
| | | 102 | | return new ResultError(ErrorCode.ValidationError, "Tag values cannot be blank or whitespace."); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | var trimmedTagLength = rawTag.Trim().Length; |
| | | 106 | | if (trimmedTagLength > Note.MaxTagChars) |
| | | 107 | | { |
| | | 108 | | return new ResultError( |
| | | 109 | | ErrorCode.ValidationError, |
| | | 110 | | $"Tag length ({trimmedTagLength} characters) exceeds maximum allowed ({Note.MaxTagChars} charact |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | if (!ProvenanceKindExtensions.TryParseWireString(command.ProvenanceKind, out _)) |
| | | 116 | | { |
| | | 117 | | return new ResultError( |
| | | 118 | | ErrorCode.ValidationError, |
| | | 119 | | $"Invalid provenance kind '{command.ProvenanceKind}'. Expected 'stated', 'researched', or 'inferred'."); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | if (!ProvenanceMarker.IsValidIsoDate(command.ProvenanceDate)) |
| | | 123 | | { |
| | | 124 | | return new ResultError( |
| | | 125 | | ErrorCode.ValidationError, |
| | | 126 | | $"Invalid provenance date '{command.ProvenanceDate}'. Expected ISO-8601 date 'YYYY-MM-DD'."); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | return null; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | public sealed class CreateNoteCommandHandler : ICommandHandler<CreateNoteCommand, NoteCreatedDto> |
| | | 134 | | { |
| | | 135 | | private static readonly CreateNoteCommandValidator Validator = new(); |
| | | 136 | | private readonly INoteRepository _repository; |
| | | 137 | | private readonly IUtcClock _clock; |
| | | 138 | | |
| | | 139 | | public CreateNoteCommandHandler(INoteRepository repository, IUtcClock clock) |
| | | 140 | | { |
| | | 141 | | ArgumentNullException.ThrowIfNull(repository); |
| | | 142 | | ArgumentNullException.ThrowIfNull(clock); |
| | | 143 | | _repository = repository; |
| | | 144 | | _clock = clock; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public async Task<Result<NoteCreatedDto>> HandleAsync( |
| | | 148 | | CreateNoteCommand command, |
| | | 149 | | CancellationToken cancellationToken = default) |
| | | 150 | | { |
| | | 151 | | ArgumentNullException.ThrowIfNull(command); |
| | | 152 | | |
| | | 153 | | var validationError = Validator.Validate(command); |
| | | 154 | | if (validationError is not null) |
| | | 155 | | { |
| | | 156 | | return Result<NoteCreatedDto>.Failure(validationError.Value); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | var ownerId = string.IsNullOrWhiteSpace(command.OwnerId) |
| | | 160 | | ? OwnerId.Default.Value |
| | | 161 | | : new OwnerId(command.OwnerId).Value; |
| | | 162 | | |
| | | 163 | | var actor = command.Actor.Trim(); |
| | | 164 | | var now = _clock.UtcNow; |
| | | 165 | | |
| | | 166 | | var provKind = ProvenanceKindExtensions.FromWireString(command.ProvenanceKind); |
| | | 167 | | var provenance = new ProvenanceMarker(provKind, command.ProvenanceDate, command.ProvenanceQualifier); |
| | | 168 | | |
| | | 169 | | var note = Note.Create( |
| | | 170 | | ownerId: ownerId, |
| | | 171 | | title: command.Title, |
| | | 172 | | body: command.Body, |
| | | 173 | | tags: command.Tags, |
| | | 174 | | area: command.Area, |
| | | 175 | | provenance: provenance, |
| | | 176 | | createdAtUtc: now); |
| | | 177 | | |
| | | 178 | | var audit = AuditLogEntry.CreateForCreate( |
| | | 179 | | ownerId: ownerId, |
| | | 180 | | actor: actor, |
| | | 181 | | targetNoteId: note.Id, |
| | | 182 | | resultingVersionToken: note.VersionToken, |
| | | 183 | | occurredAtUtc: now, |
| | | 184 | | afterSummary: AuditContentSummary.FromNote(note)); |
| | | 185 | | |
| | | 186 | | var outcome = await _repository.ExecuteInTransactionAsync( |
| | | 187 | | (tx, ct) => tx.CreateAsync(note, audit, ct), |
| | | 188 | | cancellationToken); |
| | | 189 | | |
| | | 190 | | if (!outcome.IsSuccess) |
| | | 191 | | { |
| | | 192 | | return Result<NoteCreatedDto>.Failure(outcome.Error!.Value); |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | var saved = outcome.Value!.Note; |
| | | 196 | | return Result<NoteCreatedDto>.Success(new NoteCreatedDto( |
| | | 197 | | Id: saved.Id, |
| | | 198 | | Title: saved.Title, |
| | | 199 | | Area: saved.Area, |
| | | 200 | | Tags: saved.Tags, |
| | | 201 | | Provenance: new ProvenanceDto( |
| | | 202 | | saved.Provenance.Kind.ToWireString(), |
| | | 203 | | saved.Provenance.Date, |
| | | 204 | | saved.Provenance.Qualifier), |
| | | 205 | | Status: saved.Status.ToWireString(), |
| | | 206 | | VersionToken: saved.VersionToken, |
| | | 207 | | BodyHash: saved.CurrentBodyHash, |
| | | 208 | | CreatedAtUtc: saved.CreatedAtUtc, |
| | | 209 | | UpdatedAtUtc: saved.UpdatedAtUtc)); |
| | | 210 | | } |
| | | 211 | | } |