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