| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Application.Notes.Commands; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | using RoleRadar.Domain.Notes; |
| | | 5 | | |
| | | 6 | | namespace RoleRadar.Application.Notes.Queries; |
| | | 7 | | |
| | | 8 | | public sealed record NoteSearchItemDto( |
| | | 9 | | Guid Id, |
| | | 10 | | string Title, |
| | | 11 | | string Area, |
| | | 12 | | IReadOnlyList<string> Tags, |
| | | 13 | | ProvenanceDto Provenance, |
| | | 14 | | string Status, |
| | | 15 | | Guid VersionToken, |
| | | 16 | | string Snippet, |
| | | 17 | | double Score, |
| | | 18 | | DateTime UpdatedAtUtc); |
| | | 19 | | |
| | | 20 | | public sealed record NoteSearchResponseDto( |
| | | 21 | | IReadOnlyList<NoteSearchItemDto> Results); |
| | | 22 | | |
| | 104 | 23 | | public sealed record SearchNotesQuery( |
| | 366 | 24 | | string? Query = null, |
| | 182 | 25 | | IReadOnlyList<string>? Tags = null, |
| | 268 | 26 | | string? Area = null, |
| | 184 | 27 | | bool IncludeArchived = false, |
| | 364 | 28 | | int Limit = 20, |
| | 286 | 29 | | string? OwnerId = null) : IQuery<NoteSearchResponseDto>; |
| | | 30 | | |
| | | 31 | | public sealed class SearchNotesQueryHandler : IQueryHandler<SearchNotesQuery, NoteSearchResponseDto> |
| | | 32 | | { |
| | | 33 | | private readonly INoteRepository _repository; |
| | | 34 | | |
| | | 35 | | public SearchNotesQueryHandler(INoteRepository repository) |
| | | 36 | | { |
| | | 37 | | ArgumentNullException.ThrowIfNull(repository); |
| | | 38 | | _repository = repository; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public async Task<Result<NoteSearchResponseDto>> HandleAsync( |
| | | 42 | | SearchNotesQuery query, |
| | | 43 | | CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | | 45 | | ArgumentNullException.ThrowIfNull(query); |
| | | 46 | | |
| | | 47 | | var ownerId = string.IsNullOrWhiteSpace(query.OwnerId) |
| | | 48 | | ? OwnerId.Default.Value |
| | | 49 | | : new OwnerId(query.OwnerId).Value; |
| | | 50 | | |
| | | 51 | | var readiness = await _repository.CheckReadinessAsync(cancellationToken); |
| | | 52 | | if (!readiness.IsSuccess) |
| | | 53 | | { |
| | | 54 | | return Result<NoteSearchResponseDto>.Failure(readiness.Error!.Value); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | var clampedLimit = Math.Clamp(query.Limit <= 0 ? 20 : query.Limit, 1, 100); |
| | | 58 | | var criteria = new NoteSearchCriteria( |
| | | 59 | | OwnerId: ownerId, |
| | | 60 | | Query: string.IsNullOrWhiteSpace(query.Query) ? null : query.Query.Trim(), |
| | | 61 | | Tags: query.Tags, |
| | | 62 | | Area: string.IsNullOrWhiteSpace(query.Area) ? null : query.Area.Trim(), |
| | | 63 | | IncludeArchived: query.IncludeArchived, |
| | | 64 | | Limit: clampedLimit); |
| | | 65 | | |
| | | 66 | | var hitsResult = await _repository.SearchAsync(criteria, cancellationToken); |
| | | 67 | | if (!hitsResult.IsSuccess) |
| | | 68 | | { |
| | | 69 | | return Result<NoteSearchResponseDto>.Failure(hitsResult.Error!.Value); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | var items = hitsResult.Value! |
| | | 73 | | .Select(h => new NoteSearchItemDto( |
| | | 74 | | Id: h.Note.Id, |
| | | 75 | | Title: h.Note.Title, |
| | | 76 | | Area: h.Note.Area, |
| | | 77 | | Tags: h.Note.Tags, |
| | | 78 | | Provenance: new ProvenanceDto( |
| | | 79 | | h.Note.Provenance.Kind.ToWireString(), |
| | | 80 | | h.Note.Provenance.Date, |
| | | 81 | | h.Note.Provenance.Qualifier), |
| | | 82 | | Status: h.Note.Status.ToWireString(), |
| | | 83 | | VersionToken: h.Note.VersionToken, |
| | | 84 | | Snippet: h.Snippet, |
| | | 85 | | Score: h.Score, |
| | | 86 | | UpdatedAtUtc: h.Note.UpdatedAtUtc)) |
| | | 87 | | .ToArray(); |
| | | 88 | | |
| | | 89 | | return Result<NoteSearchResponseDto>.Success(new NoteSearchResponseDto(items)); |
| | | 90 | | } |
| | | 91 | | } |