Retructured solution
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
using LiteCharms.Extensions;
|
||||
using LiteCharms.Features.Shop.Postgres;
|
||||
using LiteCharms.Features.Shop.ShoppingCarts.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user