27 lines
978 B
C#
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));
|
|
}
|
|
}
|
|
}
|