Implemented shopping carts functionality elements

This commit is contained in:
Khwezi Mngoma
2026-05-06 10:48:02 +02:00
parent 83f51c6a23
commit 4321b03735
13 changed files with 341 additions and 5 deletions
@@ -0,0 +1,32 @@
using LiteCharms.Infrastructure.Database;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class CreateShoppingCartCommandHandler(IDbContextFactory<LeadGeneratorDbContext> contextFactory) : IRequestHandler<CreateShoppingCartCommand, Result<Guid>>
{
public async ValueTask<Result<Guid>> Handle(CreateShoppingCartCommand 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<Guid>($"Customer could not be found with id {request.CustomerId}");
var cart = context.ShoppingCarts.Add(new Entities.ShoppingCart
{
CustomerId = request.CustomerId,
OrderId = request.OrderId,
QuoteId = request.QuoteId
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(cart.Entity.Id)
: Result.Fail<Guid>($"Failed to create shopping cart for customer id {request.CustomerId}");
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
}