Files
components/LiteCharms.Features/Quotes/Queries/Handlers/GetQuoteQueryHandler.cs
T
Khwezi Mngoma ff34326a53
continuous-integration/drone/pr Build is passing
Refactored database references
2026-05-09 16:58:34 +02:00

27 lines
978 B
C#

using LiteCharms.Extensions;
using LiteCharms.Infrastructure.Database;
using LiteCharms.Models;
namespace LiteCharms.Features.Quotes.Queries.Handlers;
public class GetQuoteQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetQuoteQuery, Result<Quote>>
{
public async ValueTask<Result<Quote>> Handle(GetQuoteQuery request, CancellationToken cancellationToken)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var quote = await context.Quotes.AsNoTracking().FirstOrDefaultAsync(q => q.Id == request.QuoteId, cancellationToken);
return quote is not null
? Result.Ok(quote.ToModel())
: Result.Fail<Quote>(new Error($"Quote with ID {request.QuoteId} not found."));
}
catch (Exception ex)
{
return Result.Fail<Quote>(new Error(ex.Message).CausedBy(ex));
}
}
}