83f51c6a23
Added shopping cart and items Added quotes Refactored relatinoships Migrated changes Refactored cqrs commands and queries Refactored mappings
33 lines
1.4 KiB
C#
33 lines
1.4 KiB
C#
using LiteCharms.Extensions;
|
|
using LiteCharms.Features.Refunds.Queries;
|
|
using LiteCharms.Infrastructure.Database;
|
|
using LiteCharms.Models;
|
|
|
|
namespace LiteCharms.Features.Refunds.Queries.Handlers;
|
|
|
|
public class GetCustomerRefundsQueryHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<GetCustomerRefundsQuery, Result<OrderRefund[]>>
|
|
{
|
|
public async ValueTask<Result<OrderRefund[]>> Handle(GetCustomerRefundsQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
if(!await context.Customers.AnyAsync(c => c.Id == request.CustomerId, cancellationToken))
|
|
return Result.Fail<OrderRefund[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
|
|
|
|
var refunds = await context.OrderRefunds.AsNoTracking().AsSplitQuery()
|
|
.OrderByDescending(o => o.CreatedAt)
|
|
.Where(r => r.Order!.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
|
|
|
|
return refunds?.Length > 0
|
|
? Result.Ok(refunds.Select(r => r.ToModel()).ToArray())
|
|
: Result.Fail<OrderRefund[]>(new Error($"No refunds found for customer with Id {request.CustomerId}."));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<OrderRefund[]>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|