< Summary

Information
Class: RoleRadar.Application.Notes.Queries.GetNoteQuery
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Queries/GetNoteQuery.cs
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 154
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Id()100%11100%
get_Offset()100%11100%
get_MaxChars()100%11100%
get_OwnerId()100%11100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Queries/GetNoteQuery.cs

#LineLine coverage
 1using System.Text.Json.Serialization;
 2using RoleRadar.Application.Abstractions;
 3using RoleRadar.Application.Notes.Commands;
 4using RoleRadar.Domain.Common;
 5using RoleRadar.Domain.Notes;
 6
 7namespace RoleRadar.Application.Notes.Queries;
 8
 9public 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
 6426public sealed record GetNoteQuery(
 12027    Guid Id,
 16028    int Offset = 0,
 17029    int MaxChars = Note.DefaultGetMaxChars,
 13030    string? OwnerId = null) : IQuery<NoteDetailDto>;
 31
 32public 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}