| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | using RoleRadar.Application.Abstractions; |
| | | 3 | | using RoleRadar.Application.Notes.Commands; |
| | | 4 | | using RoleRadar.Domain.Common; |
| | | 5 | | using RoleRadar.Domain.Notes; |
| | | 6 | | |
| | | 7 | | namespace RoleRadar.Application.Notes.Queries; |
| | | 8 | | |
| | | 9 | | public sealed record NoteDetailDto( |
| | | 10 | | Guid Id, |
| | | 11 | | string Title, |
| | | 12 | | string Body, |
| | | 13 | | int TotalChars, |
| | | 14 | | bool Truncated, |
| | | 15 | | [property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] int? NextOffset, |
| | | 16 | | string Area, |
| | | 17 | | IReadOnlyList<string> Tags, |
| | | 18 | | ProvenanceDto Provenance, |
| | | 19 | | string Status, |
| | | 20 | | Guid VersionToken, |
| | | 21 | | string BodyHash, |
| | | 22 | | int RevisionCount, |
| | | 23 | | DateTime CreatedAtUtc, |
| | | 24 | | DateTime UpdatedAtUtc); |
| | | 25 | | |
| | | 26 | | public sealed record GetNoteQuery( |
| | | 27 | | Guid Id, |
| | | 28 | | int Offset = 0, |
| | | 29 | | int MaxChars = Note.DefaultGetMaxChars, |
| | | 30 | | string? OwnerId = null) : IQuery<NoteDetailDto>; |
| | | 31 | | |
| | | 32 | | public sealed class GetNoteQueryHandler : IQueryHandler<GetNoteQuery, NoteDetailDto> |
| | | 33 | | { |
| | | 34 | | private readonly INoteRepository _repository; |
| | | 35 | | |
| | 116 | 36 | | public GetNoteQueryHandler(INoteRepository repository) |
| | | 37 | | { |
| | 116 | 38 | | ArgumentNullException.ThrowIfNull(repository); |
| | 116 | 39 | | _repository = repository; |
| | 116 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<Result<NoteDetailDto>> HandleAsync( |
| | | 43 | | GetNoteQuery query, |
| | | 44 | | CancellationToken cancellationToken = default) |
| | | 45 | | { |
| | 64 | 46 | | ArgumentNullException.ThrowIfNull(query); |
| | | 47 | | |
| | 64 | 48 | | if (query.Id == Guid.Empty) |
| | | 49 | | { |
| | 0 | 50 | | return Result<NoteDetailDto>.Failure( |
| | 0 | 51 | | ErrorCode.ValidationError, |
| | 0 | 52 | | "Note ID cannot be Guid.Empty."); |
| | | 53 | | } |
| | | 54 | | |
| | 64 | 55 | | if (query.Offset < 0) |
| | | 56 | | { |
| | 2 | 57 | | return Result<NoteDetailDto>.Failure( |
| | 2 | 58 | | ErrorCode.ValidationError, |
| | 2 | 59 | | $"Offset ({query.Offset}) must be greater than or equal to 0."); |
| | | 60 | | } |
| | | 61 | | |
| | 62 | 62 | | if (query.MaxChars < 1 || query.MaxChars > Note.MaxGetMaxChars) |
| | | 63 | | { |
| | 4 | 64 | | return Result<NoteDetailDto>.Failure( |
| | 4 | 65 | | ErrorCode.ValidationError, |
| | 4 | 66 | | $"MaxChars ({query.MaxChars}) must be between 1 and {Note.MaxGetMaxChars}."); |
| | | 67 | | } |
| | | 68 | | |
| | 58 | 69 | | var ownerId = string.IsNullOrWhiteSpace(query.OwnerId) |
| | 58 | 70 | | ? OwnerId.Default.Value |
| | 58 | 71 | | : new OwnerId(query.OwnerId).Value; |
| | | 72 | | |
| | 58 | 73 | | var readiness = await _repository.CheckReadinessAsync(cancellationToken); |
| | 58 | 74 | | if (!readiness.IsSuccess) |
| | | 75 | | { |
| | 2 | 76 | | return Result<NoteDetailDto>.Failure(readiness.Error!.Value); |
| | | 77 | | } |
| | | 78 | | |
| | 56 | 79 | | var noteResult = await _repository.GetByIdAsync(ownerId, query.Id, cancellationToken); |
| | 56 | 80 | | if (!noteResult.IsSuccess) |
| | | 81 | | { |
| | 6 | 82 | | return Result<NoteDetailDto>.Failure(noteResult.Error!.Value); |
| | | 83 | | } |
| | | 84 | | |
| | 50 | 85 | | var note = noteResult.Value!; |
| | 50 | 86 | | var fullBody = note.Body; |
| | 50 | 87 | | var totalChars = fullBody.Length; |
| | | 88 | | |
| | | 89 | | string slicedBody; |
| | | 90 | | bool truncated; |
| | | 91 | | int? nextOffset; |
| | | 92 | | |
| | 50 | 93 | | if (query.Offset >= totalChars) |
| | | 94 | | { |
| | 6 | 95 | | slicedBody = string.Empty; |
| | 6 | 96 | | truncated = false; |
| | 6 | 97 | | nextOffset = null; |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 44 | 101 | | var startOffset = query.Offset; |
| | 44 | 102 | | if (startOffset > 0 && |
| | 44 | 103 | | char.IsLowSurrogate(fullBody[startOffset]) && |
| | 44 | 104 | | char.IsHighSurrogate(fullBody[startOffset - 1])) |
| | | 105 | | { |
| | 2 | 106 | | startOffset--; |
| | | 107 | | } |
| | | 108 | | |
| | 44 | 109 | | var take = Math.Min(query.MaxChars, totalChars - startOffset); |
| | 44 | 110 | | var endOffset = startOffset + take; |
| | | 111 | | |
| | 44 | 112 | | if (endOffset < totalChars && |
| | 44 | 113 | | endOffset > startOffset && |
| | 44 | 114 | | char.IsHighSurrogate(fullBody[endOffset - 1]) && |
| | 44 | 115 | | char.IsLowSurrogate(fullBody[endOffset])) |
| | | 116 | | { |
| | 6 | 117 | | if (take > 1) |
| | | 118 | | { |
| | 6 | 119 | | take--; |
| | 6 | 120 | | endOffset--; |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | 0 | 124 | | take = 2; |
| | 0 | 125 | | endOffset = startOffset + 2; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 44 | 129 | | slicedBody = fullBody.Substring(startOffset, take); |
| | 44 | 130 | | truncated = endOffset < totalChars; |
| | 44 | 131 | | nextOffset = truncated ? endOffset : null; |
| | | 132 | | } |
| | | 133 | | |
| | 50 | 134 | | return Result<NoteDetailDto>.Success(new NoteDetailDto( |
| | 50 | 135 | | Id: note.Id, |
| | 50 | 136 | | Title: note.Title, |
| | 50 | 137 | | Body: slicedBody, |
| | 50 | 138 | | TotalChars: totalChars, |
| | 50 | 139 | | Truncated: truncated, |
| | 50 | 140 | | NextOffset: nextOffset, |
| | 50 | 141 | | Area: note.Area, |
| | 50 | 142 | | Tags: note.Tags, |
| | 50 | 143 | | Provenance: new ProvenanceDto( |
| | 50 | 144 | | note.Provenance.Kind.ToWireString(), |
| | 50 | 145 | | note.Provenance.Date, |
| | 50 | 146 | | note.Provenance.Qualifier), |
| | 50 | 147 | | Status: note.Status.ToWireString(), |
| | 50 | 148 | | VersionToken: note.VersionToken, |
| | 50 | 149 | | BodyHash: note.CurrentBodyHash, |
| | 50 | 150 | | RevisionCount: note.RevisionCount, |
| | 50 | 151 | | CreatedAtUtc: note.CreatedAtUtc, |
| | 50 | 152 | | UpdatedAtUtc: note.UpdatedAtUtc)); |
| | 64 | 153 | | } |
| | | 154 | | } |