using LiteCharms.Extensions; using LiteCharms.Features.Shop.Orders.Models; using LiteCharms.Features.Shop.Postgres; namespace LiteCharms.Features.Orders.Queries.Handlers; public class GetOrderRefundQueryHandler(IDbContextFactory contextFactory) : IRequestHandler> { public async ValueTask> Handle(GetOrderRefundQuery request, CancellationToken cancellationToken) { try { using var context = await contextFactory.CreateDbContextAsync(cancellationToken); var refund = await context.OrderRefunds.AsNoTracking() .FirstOrDefaultAsync(r => r.OrderId == request.OrderId && r.Id == request.OrderRefundId, cancellationToken); return refund is not null ? Result.Ok(refund.ToModel()) : Result.Fail(new Error($"Refund {request.OrderRefundId} not found for the given OrderId: {request.OrderId}")); } catch (Exception ex) { return Result.Fail(new Error(ex.Message).CausedBy(ex)); } } }