Make Take optional and refactor query handling logic

Updated the `Take` property in `ReadRecentReceiverSuggestionsQuery` to be nullable, allowing for unlimited results when not specified. Refactored the `Handle` method in `ReadRecentReceiverSuggestionsQueryHandler` to conditionally apply the `Take` limit only when provided. Renamed the cancellation token parameter for consistency. Improved code clarity and flexibility by removing redundant operations and dynamically applying the `Take` logic.
This commit is contained in:
2026-10-05 17:06:47 +02:00
parent fac4ec28a4
commit ece676fbcf

View File

@@ -20,9 +20,9 @@ public record ReadRecentReceiverSuggestionsQuery : IRequest<IReadOnlyList<Recent
internal string? Username { get; private init; } internal string? Username { get; private init; }
/// <summary> /// <summary>
/// Desired number of results. Default value is 5. /// Desired number of results.
/// </summary> /// </summary>
public int Take { get; init; } = 5; public int? Take { get; init; } = null;
/// <summary> /// <summary>
/// Returns a new query instance scoped to the specified sender username. /// Returns a new query instance scoped to the specified sender username.
@@ -45,9 +45,9 @@ public class ReadRecentReceiverSuggestionsQueryHandler(IRepository<EnvelopeRecei
/// Retrieves distinct recent receiver suggestions grouped by receiver email. /// Retrieves distinct recent receiver suggestions grouped by receiver email.
/// </summary> /// </summary>
/// <param name="request">Suggestion query.</param> /// <param name="request">Suggestion query.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancel">Cancellation token.</param>
/// <returns>Ordered list of recent receiver suggestions.</returns> /// <returns>Ordered list of recent receiver suggestions.</returns>
public async Task<IReadOnlyList<RecentReceiverSuggestionDto>> Handle(ReadRecentReceiverSuggestionsQuery request, CancellationToken cancellationToken) public async Task<IReadOnlyList<RecentReceiverSuggestionDto>> Handle(ReadRecentReceiverSuggestionsQuery request, CancellationToken cancel)
{ {
var query = repository.Query var query = repository.Query
.AsNoTracking() .AsNoTracking()
@@ -80,7 +80,7 @@ public class ReadRecentReceiverSuggestionsQueryHandler(IRepository<EnvelopeRecei
// 3) Tie-break: if multiple rows share the same latest timestamp for an email, // 3) Tie-break: if multiple rows share the same latest timestamp for an email,
// pick a deterministic name via Max(FullName), then order by most recent usage. // pick a deterministic name via Max(FullName), then order by most recent usage.
var suggestions = await latestRows var suggestions = latestRows
.GroupBy(x => new { x.EmailAddress, x.LastUsedAt }) .GroupBy(x => new { x.EmailAddress, x.LastUsedAt })
.Select(g => new RecentReceiverSuggestionDto .Select(g => new RecentReceiverSuggestionDto
{ {
@@ -89,10 +89,10 @@ public class ReadRecentReceiverSuggestionsQueryHandler(IRepository<EnvelopeRecei
LastUsedAt = g.Key.LastUsedAt, LastUsedAt = g.Key.LastUsedAt,
}) })
.OrderByDescending(x => x.LastUsedAt) .OrderByDescending(x => x.LastUsedAt)
.ThenBy(x => x.EmailAddress) .ThenBy(x => x.EmailAddress);
.Take(request.Take)
.ToListAsync(cancellationToken);
return suggestions; return request.Take is int take
? await suggestions.Take(take).ToListAsync(cancel)
: await suggestions.ToListAsync(cancel);
} }
} }