< Summary

Information
Class: RoleRadar.Application.Notes.Queries.GetNoteQueryHandler
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Notes/Queries/GetNoteQuery.cs
Line coverage
93%
Covered lines: 67
Uncovered lines: 5
Coverable lines: 72
Total lines: 154
Line coverage: 93%
Branch coverage
94%
Covered branches: 32
Total branches: 34
Branch coverage: 94.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync()91.17%353491.17%

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
 26public sealed record GetNoteQuery(
 27    Guid Id,
 28    int Offset = 0,
 29    int MaxChars = Note.DefaultGetMaxChars,
 30    string? OwnerId = null) : IQuery<NoteDetailDto>;
 31
 32public sealed class GetNoteQueryHandler : IQueryHandler<GetNoteQuery, NoteDetailDto>
 33{
 34    private readonly INoteRepository _repository;
 35
 11636    public GetNoteQueryHandler(INoteRepository repository)
 37    {
 11638        ArgumentNullException.ThrowIfNull(repository);
 11639        _repository = repository;
 11640    }
 41
 42    public async Task<Result<NoteDetailDto>> HandleAsync(
 43        GetNoteQuery query,
 44        CancellationToken cancellationToken = default)
 45    {
 6446        ArgumentNullException.ThrowIfNull(query);
 47
 6448        if (query.Id == Guid.Empty)
 49        {
 050            return Result<NoteDetailDto>.Failure(
 051                ErrorCode.ValidationError,
 052                "Note ID cannot be Guid.Empty.");
 53        }
 54
 6455        if (query.Offset < 0)
 56        {
 257            return Result<NoteDetailDto>.Failure(
 258                ErrorCode.ValidationError,
 259                $"Offset ({query.Offset}) must be greater than or equal to 0.");
 60        }
 61
 6262        if (query.MaxChars < 1 || query.MaxChars > Note.MaxGetMaxChars)
 63        {
 464            return Result<NoteDetailDto>.Failure(
 465                ErrorCode.ValidationError,
 466                $"MaxChars ({query.MaxChars}) must be between 1 and {Note.MaxGetMaxChars}.");
 67        }
 68
 5869        var ownerId = string.IsNullOrWhiteSpace(query.OwnerId)
 5870            ? OwnerId.Default.Value
 5871            : new OwnerId(query.OwnerId).Value;
 72
 5873        var readiness = await _repository.CheckReadinessAsync(cancellationToken);
 5874        if (!readiness.IsSuccess)
 75        {
 276            return Result<NoteDetailDto>.Failure(readiness.Error!.Value);
 77        }
 78
 5679        var noteResult = await _repository.GetByIdAsync(ownerId, query.Id, cancellationToken);
 5680        if (!noteResult.IsSuccess)
 81        {
 682            return Result<NoteDetailDto>.Failure(noteResult.Error!.Value);
 83        }
 84
 5085        var note = noteResult.Value!;
 5086        var fullBody = note.Body;
 5087        var totalChars = fullBody.Length;
 88
 89        string slicedBody;
 90        bool truncated;
 91        int? nextOffset;
 92
 5093        if (query.Offset >= totalChars)
 94        {
 695            slicedBody = string.Empty;
 696            truncated = false;
 697            nextOffset = null;
 98        }
 99        else
 100        {
 44101            var startOffset = query.Offset;
 44102            if (startOffset > 0 &&
 44103                char.IsLowSurrogate(fullBody[startOffset]) &&
 44104                char.IsHighSurrogate(fullBody[startOffset - 1]))
 105            {
 2106                startOffset--;
 107            }
 108
 44109            var take = Math.Min(query.MaxChars, totalChars - startOffset);
 44110            var endOffset = startOffset + take;
 111
 44112            if (endOffset < totalChars &&
 44113                endOffset > startOffset &&
 44114                char.IsHighSurrogate(fullBody[endOffset - 1]) &&
 44115                char.IsLowSurrogate(fullBody[endOffset]))
 116            {
 6117                if (take > 1)
 118                {
 6119                    take--;
 6120                    endOffset--;
 121                }
 122                else
 123                {
 0124                    take = 2;
 0125                    endOffset = startOffset + 2;
 126                }
 127            }
 128
 44129            slicedBody = fullBody.Substring(startOffset, take);
 44130            truncated = endOffset < totalChars;
 44131            nextOffset = truncated ? endOffset : null;
 132        }
 133
 50134        return Result<NoteDetailDto>.Success(new NoteDetailDto(
 50135            Id: note.Id,
 50136            Title: note.Title,
 50137            Body: slicedBody,
 50138            TotalChars: totalChars,
 50139            Truncated: truncated,
 50140            NextOffset: nextOffset,
 50141            Area: note.Area,
 50142            Tags: note.Tags,
 50143            Provenance: new ProvenanceDto(
 50144                note.Provenance.Kind.ToWireString(),
 50145                note.Provenance.Date,
 50146                note.Provenance.Qualifier),
 50147            Status: note.Status.ToWireString(),
 50148            VersionToken: note.VersionToken,
 50149            BodyHash: note.CurrentBodyHash,
 50150            RevisionCount: note.RevisionCount,
 50151            CreatedAtUtc: note.CreatedAtUtc,
 50152            UpdatedAtUtc: note.UpdatedAtUtc));
 64153    }
 154}