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:
@@ -20,9 +20,9 @@ public record ReadRecentReceiverSuggestionsQuery : IRequest<IReadOnlyList<Recent
|
||||
internal string? Username { get; private init; }
|
||||
|
||||
/// <summary>
|
||||
/// Desired number of results. Default value is 5.
|
||||
/// Desired number of results.
|
||||
/// </summary>
|
||||
public int Take { get; init; } = 5;
|
||||
public int? Take { get; init; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public async Task<IReadOnlyList<RecentReceiverSuggestionDto>> Handle(ReadRecentReceiverSuggestionsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IReadOnlyList<RecentReceiverSuggestionDto>> Handle(ReadRecentReceiverSuggestionsQuery request, CancellationToken cancel)
|
||||
{
|
||||
var query = repository.Query
|
||||
.AsNoTracking()
|
||||
@@ -80,7 +80,7 @@ public class ReadRecentReceiverSuggestionsQueryHandler(IRepository<EnvelopeRecei
|
||||
|
||||
// 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.
|
||||
var suggestions = await latestRows
|
||||
var suggestions = latestRows
|
||||
.GroupBy(x => new { x.EmailAddress, x.LastUsedAt })
|
||||
.Select(g => new RecentReceiverSuggestionDto
|
||||
{
|
||||
@@ -89,10 +89,10 @@ public class ReadRecentReceiverSuggestionsQueryHandler(IRepository<EnvelopeRecei
|
||||
LastUsedAt = g.Key.LastUsedAt,
|
||||
})
|
||||
.OrderByDescending(x => x.LastUsedAt)
|
||||
.ThenBy(x => x.EmailAddress)
|
||||
.Take(request.Take)
|
||||
.ToListAsync(cancellationToken);
|
||||
.ThenBy(x => x.EmailAddress);
|
||||
|
||||
return suggestions;
|
||||
return request.Take is int take
|
||||
? await suggestions.Take(take).ToListAsync(cancel)
|
||||
: await suggestions.ToListAsync(cancel);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user