Files
components/LiteCharms.Features/Shop/Orders/Refunds/Queries/Handlers/GetRefundQueryHandler.cs
T
2026-05-13 20:06:24 +02:00

28 lines
1.1 KiB
C#

using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Orders.Models;
using LiteCharms.Features.Shop.Orders.Refunds.Queries;
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.Shop.Orders.Refunds.Queries.Handlers;
public class GetRefundQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetRefundQuery, Result<OrderRefund>>
{
public async ValueTask<Result<OrderRefund>> Handle(GetRefundQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var refund = await context.OrderRefunds.AsNoTracking().FirstOrDefaultAsync(r => r.Id == request.OrderRefundId, cancellationToken);
return refund is not null
? Result.Ok(refund.ToModel())
: Result.Fail<OrderRefund>($"Order refund could not be found with id {request.OrderRefundId}");
}
catch (Exception ex)
{
return Result.Fail<OrderRefund>(new Error(ex.Message).CausedBy(ex));
}
}
}