Files
components/LiteCharms.Features/Quotes/Queries/Handlers/GetQuotesHandler.cs
T
Khwezi Mngoma 83f51c6a23 Added notifications
Added shopping cart and items
Added quotes
Refactored relatinoships
Migrated changes
Refactored cqrs commands and queries
Refactored mappings
2026-05-05 23:59:31 +02:00

34 lines
1.3 KiB
C#

using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Quotes.Queries.Handlers;
public class GetQuotesHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetQuotesQuery, Result<Quote[]>>
{
public async ValueTask<Result<Quote[]>> Handle(GetQuotesQuery request, CancellationToken cancellationToken)
{
try
{
var fromDate = request.From.ToDateTime(TimeOnly.MinValue);
var toDate = request.To.ToDateTime(TimeOnly.MaxValue);
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var quotes = await context.Quotes.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(o => o.CreatedAt >= fromDate && o.CreatedAt <= toDate)
.Take(request.MaxRecords)
.ToArrayAsync(cancellationToken);
return quotes?.Length > 0
? Result.Ok(quotes.Select(o => o.ToModel()).ToArray())
: Result.Fail<Quote[]>(new Error($"No quotes found for the specified date range {request.From} - {request.To}."));
}
catch (Exception ex)
{
return Result.Fail<Quote[]>(new Error(ex.Message).CausedBy(ex));
}
}
}