27 lines
1023 B
C#
27 lines
1023 B
C#
using LiteCharms.Extensions;
|
|
using LiteCharms.Infrastructure.Database;
|
|
using LiteCharms.Models;
|
|
|
|
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
|
|
|
|
public class GetShoppingCartQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartQuery, Result<ShoppingCart>>
|
|
{
|
|
public async ValueTask<Result<ShoppingCart>> Handle(GetShoppingCartQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var cart = await context.ShoppingCarts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
|
|
|
|
return cart is not null
|
|
? Result.Ok(cart.ToModel())
|
|
: Result.Fail<ShoppingCart>($"Failed to find shopping cart with id {request.ShoppingCartId}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Fail<ShoppingCart>(new Error(ex.Message).CausedBy(ex));
|
|
}
|
|
}
|
|
}
|