Completed refactor

This commit is contained in:
Khwezi Mngoma
2026-05-14 01:33:21 +02:00
parent 42001998d6
commit 134d8429c0
129 changed files with 1870 additions and 3165 deletions
@@ -1,30 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class AddItemToShoppingCartCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
public Guid ProductPriceId { get; set; }
public int Quantity { get; set; }
private AddItemToShoppingCartCommand(Guid shoppingCartId, Guid productPriceId, int quantity = 1)
{
ShoppingCartId = shoppingCartId;
ProductPriceId = productPriceId;
Quantity = quantity;
}
public static AddItemToShoppingCartCommand Create(Guid shoppingCartId, Guid productPriceId, int quantity = 1)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
if (productPriceId == Guid.Empty)
throw new ArgumentException($"Product item required", nameof(productPriceId));
if(quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity));
return new(shoppingCartId, productPriceId, quantity);
}
}
@@ -1,25 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class AddPackageToShoppingCartCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
public Guid PackageId { get; set; }
private AddPackageToShoppingCartCommand(Guid shoppingCartId, Guid packageId)
{
ShoppingCartId = shoppingCartId;
PackageId = packageId;
}
public static AddPackageToShoppingCartCommand Create(Guid shoppingCartId, Guid packageId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
if (packageId == Guid.Empty)
throw new ArgumentException($"Package ID is required", nameof(packageId));
return new(shoppingCartId, packageId);
}
}
@@ -1,25 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class CreateShoppingCartCommand : IRequest<Result<Guid>>
{
public Guid? CustomerId { get; set; }
public Guid? OrderId { get; set; }
public Guid? QuoteId { get; set; }
private CreateShoppingCartCommand(Guid customerId, Guid? orderId = null, Guid? quoteId = null)
{
CustomerId = customerId;
OrderId = orderId;
QuoteId = quoteId;
}
public static CreateShoppingCartCommand Create(Guid customerId, Guid? orderId = null, Guid? quoteId = null)
{
if (customerId == Guid.Empty)
throw new ArgumentException($"Customer ID is required", nameof(customerId));
return new(customerId, orderId, quoteId);
}
}
@@ -1,16 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class EmptyShoppingCartCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
private EmptyShoppingCartCommand(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
public static EmptyShoppingCartCommand Create(Guid shoppingCartId)
{
if(shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required.", nameof(shoppingCartId));
return new(shoppingCartId);
}
}
@@ -1,40 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class AddItemToShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AddItemToShoppingCartCommand, Result>
{
public async ValueTask<Result> Handle(AddItemToShoppingCartCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ProductPriceId, cancellationToken))
return Result.Fail($"Product item could not be found with id {request.ProductPriceId}");
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
if (cart is null)
return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}");
if (cart.ShoppingCartItems?.Any(i => i.ProductPriceId == request.ProductPriceId) == true)
return Result.Fail($"Item already in shopping cart with id {request.ShoppingCartId}");
context.ShoppingCartItems.Add(new Entities.ShoppingCartItem
{
ShoppingCartId = request.ShoppingCartId,
ProductPriceId = request.ProductPriceId,
Quantity = request.Quantity
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to add cart item with id {request.ProductPriceId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,39 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class AddPackageToShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<AddPackageToShoppingCartCommand, Result>
{
public async ValueTask<Result> Handle(AddPackageToShoppingCartCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Packages.AnyAsync(p => p.Id == request.PackageId, cancellationToken))
return Result.Fail($"Package cold not be found by ID {request.PackageId}");
var shoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
if (shoppingCart is null)
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
if (!await context.ShoppingCartPackages.AnyAsync(cp => cp.ShoppingCartId == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Package {request.PackageId} is already in the cart");
var newShoppingCartPackage = context.ShoppingCartPackages.Add(new Entities.ShoppingCartPackage
{
ShoppingCartId = request.ShoppingCartId,
PackageId = request.PackageId
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could not add package of id {request.PackageId} to shopping cart {request.ShoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,32 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class CreateShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> 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));
}
}
}
@@ -1,32 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class EmptyShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<EmptyShoppingCartCommand, Result>
{
public async ValueTask<Result> Handle(EmptyShoppingCartCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}");
if (await context.ShoppingCartItems.CountAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken) == 0)
return Result.Ok();
var cartItems = await context.ShoppingCartItems.Where(i => i.ShoppingCartId == request.ShoppingCartId).ToListAsync(cancellationToken);
context.RemoveRange(cartItems);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could not empty cart with id {request.ShoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,35 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class RemovePackageFromShoppingCartCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<RemovePackageFromShoppingCartCommand, Result>
{
public async ValueTask<Result> Handle(RemovePackageFromShoppingCartCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
if (!await context.ShoppingCartPackages.AnyAsync(p => p.Id == request.ShoppingCartPackageId, cancellationToken))
return Result.Fail($"Shopping cart package {request.ShoppingCartPackageId} is not in the shopping cart {request.ShoppingCartId}");
var shoppingCartPackage = await context.ShoppingCartPackages.FirstOrDefaultAsync(cp => cp.Id == request.ShoppingCartPackageId, cancellationToken);
if (shoppingCartPackage is null)
return Result.Ok();
context.ShoppingCartPackages.Remove(shoppingCartPackage!);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could remove package of id {request.ShoppingCartPackageId} from shopping cart {request.ShoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,36 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class RemoveShoppingCartItemCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<RemoveShoppingCartItemCommand, Result>
{
public async ValueTask<Result> Handle(RemoveShoppingCartItemCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ProductPrices.AnyAsync(c => c.Id == request.ShoppingCartItemId, cancellationToken))
return Result.Fail($"Product item could not be found with id {request.ShoppingCartItemId}");
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == request.ShoppingCartId, cancellationToken);
if (cart is null)
return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartId}");
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.Id == request.ShoppingCartItemId, cancellationToken);
if (item is null) return Result.Ok();
context.ShoppingCartItems.Remove(item);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to remove shopping cart item with id {request.ShoppingCartItemId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,32 +0,0 @@
using LiteCharms.Features.Shop.Postgres;
namespace LiteCharms.Features.ShoppingCarts.Commands.Handlers;
public class UpdateShoppingCartItemCommandHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<UpdateShoppingCartItemCommand, Result>
{
public async ValueTask<Result> Handle(UpdateShoppingCartItemCommand request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Shopping could not be found with id {request.ShoppingCartId}");
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.ShoppingCartId == request.ShoppingCartId, cancellationToken);
if(item is null)
return Result.Fail($"Shopping cart item could not be found with id {request.ShoppingCartItemId}");
item.Quantity = request.Quantity;
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to update cart item quntity");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,25 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class RemovePackageFromShoppingCartCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
public Guid ShoppingCartPackageId { get; set; }
private RemovePackageFromShoppingCartCommand(Guid shoppingCartId, Guid shoppingCartPackageId)
{
ShoppingCartId = shoppingCartId;
ShoppingCartPackageId = shoppingCartPackageId;
}
public static RemovePackageFromShoppingCartCommand Create(Guid shoppingCartId, Guid shoppingCartPackageId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
if (shoppingCartPackageId == Guid.Empty)
throw new ArgumentException($"Shopping cart Package ID is required", nameof(shoppingCartPackageId));
return new(shoppingCartId, shoppingCartPackageId);
}
}
@@ -1,25 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class RemoveShoppingCartItemCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
public Guid ShoppingCartItemId { get; set; }
private RemoveShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId)
{
ShoppingCartId = shoppingCartId;
ShoppingCartItemId = shoppingCartItemId;
}
public static RemoveShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
if (shoppingCartItemId == Guid.Empty)
throw new ArgumentException($"Shopping cart item required", nameof(shoppingCartItemId));
return new(shoppingCartId, shoppingCartItemId);
}
}
@@ -1,30 +0,0 @@
namespace LiteCharms.Features.ShoppingCarts.Commands;
public class UpdateShoppingCartItemCommand : IRequest<Result>
{
public Guid ShoppingCartId { get; set; }
public Guid ShoppingCartItemId { get; set; }
public int Quantity { get; set; }
private UpdateShoppingCartItemCommand(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1)
{
ShoppingCartId = shoppingCartId;
ShoppingCartItemId = shoppingCartItemId;
Quantity = quantity;
}
public static UpdateShoppingCartItemCommand Create(Guid shoppingCartId, Guid shoppingCartItemId, int quantity = 1)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart ID is required", nameof(shoppingCartId));
if (shoppingCartItemId == Guid.Empty)
throw new ArgumentException($"Shopping cart item is required", nameof(shoppingCartItemId));
if (quantity <= 0) throw new ArgumentException($"Quantity must be at least 1", nameof(quantity));
return new(shoppingCartId, shoppingCartItemId, quantity);
}
}
@@ -1,18 +0,0 @@
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries;
public class GetCustomerShoppingCartsQuery : IRequest<Result<ShoppingCart[]>>
{
public Guid CustomerId { get; set; }
private GetCustomerShoppingCartsQuery(Guid customerId) => CustomerId = customerId;
public static GetCustomerShoppingCartsQuery Create(Guid customerId)
{
if(customerId == Guid.Empty)
throw new ArgumentException("Customer ID is required.", nameof(customerId));
return new(customerId);
}
}
@@ -1,18 +0,0 @@
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries;
public class GetShoppingCartItemsQuery : IRequest<Result<ShoppingCartItem[]>>
{
public Guid ShoppingCartId { get; set; }
private GetShoppingCartItemsQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
public static GetShoppingCartItemsQuery Create(Guid shoppingCartId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId));
return new(shoppingCartId);
}
}
@@ -1,18 +0,0 @@
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries;
public class GetShoppingCartPackagesQuery : IRequest<Result<ShoppingCartPackage[]>>
{
public Guid ShoppingCartId { get; set; }
private GetShoppingCartPackagesQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
public static GetShoppingCartPackagesQuery Create(Guid shoppingCartId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException("Shopping cart id is required", nameof(shoppingCartId));
return new(shoppingCartId);
}
}
@@ -1,18 +0,0 @@
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries;
public class GetShoppingCartQuery : IRequest<Result<ShoppingCart>>
{
public Guid ShoppingCartId { get; set; }
private GetShoppingCartQuery(Guid shoppingCartId) => ShoppingCartId = shoppingCartId;
public static GetShoppingCartQuery Create(Guid shoppingCartId)
{
if (shoppingCartId == Guid.Empty)
throw new ArgumentException($"Shopping cart id is required", nameof(shoppingCartId));
return new(shoppingCartId);
}
}
@@ -1,30 +0,0 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Postgres;
using LiteCharms.Features.Shop.ShoppingCarts.Models;
using LiteCharms.Features.ShoppingCarts.Queries;
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
public class GetCustomerShoppingCartsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetCustomerShoppingCartsQuery, Result<ShoppingCart[]>>
{
public async ValueTask<Result<ShoppingCart[]>> Handle(GetCustomerShoppingCartsQuery 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<ShoppingCart[]>(new Error($"Customer with Id {request.CustomerId} does not exist."));
var shoppingCarts = await context.ShoppingCarts.Where(sc => sc.CustomerId == request.CustomerId).ToArrayAsync(cancellationToken);
return shoppingCarts?.Length > 0
? Result.Ok(shoppingCarts.Select(c => c.ToModel()).ToArray())
: Result.Fail<ShoppingCart[]>(new Error($"No shopping carts found for customer with Id {request.CustomerId}."));
}
catch (Exception ex)
{
return Result.Fail<ShoppingCart[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,30 +0,0 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Postgres;
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
public class GetShoppingCartItemsQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartItemsQuery, Result<ShoppingCartItem[]>>
{
public async ValueTask<Result<ShoppingCartItem[]>> Handle(GetShoppingCartItemsQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(i => i.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found with id {request.ShoppingCartId}");
var items = await context.ShoppingCartItems.AsNoTracking()
.Where(i => i.ShoppingCartId == request.ShoppingCartId).ToArrayAsync(cancellationToken);
return items?.Length > 0
? Result.Ok(items.Select(i => i.ToModel()).ToArray())
: Result.Fail<ShoppingCartItem[]>($"Failed to retrieve shopping cart items with id {request.ShoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail<ShoppingCartItem[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,32 +0,0 @@
using LiteCharms.Extensions;
using LiteCharms.Features.Shop.Postgres;
using LiteCharms.Features.Shop.ShoppingCarts.Models;
namespace LiteCharms.Features.ShoppingCarts.Queries.Handlers;
public class GetShoppingCartPackagesQueryHandler(IDbContextFactory<ShopDbContext> contextFactory) : IRequestHandler<GetShoppingCartPackagesQuery, Result<ShoppingCartPackage[]>>
{
public async ValueTask<Result<ShoppingCartPackage[]>> Handle(GetShoppingCartPackagesQuery request, CancellationToken cancellationToken)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == request.ShoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found by ID {request.ShoppingCartId}");
var packages = await context.ShoppingCartPackages.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(cp => cp.ShoppingCartId == request.ShoppingCartId)
.ToArrayAsync(cancellationToken);
return packages?.Length > 0
? Result.Ok(packages.Select(p => p.ToModel()).ToArray())
: Result.Fail($"Could not find packaged in shopping cart by ID {request.ShoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail<ShoppingCartPackage[]>(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -1,26 +0,0 @@
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));
}
}
}
@@ -0,0 +1,298 @@
using LiteCharms.Features.Extensions;
using LiteCharms.Features.Shop.Postgres;
using LiteCharms.Features.Shop.ShoppingCarts.Models;
using static LiteCharms.Features.Extensions.Timezones;
namespace LiteCharms.Features.Shop.ShoppingCarts;
public class ShoppingCartService(IDbContextFactory<ShopDbContext> contextFactory)
{
public async ValueTask<Result> AddItemToShoppingCartAsync(Guid shoppingCartId, Guid productPriceId, int quantity, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ProductPrices.AnyAsync(c => c.Id == productPriceId, cancellationToken))
return Result.Fail($"Product item could not be found with id {productPriceId}");
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken);
if (cart is null)
return Result.Fail($"Shopping cart could not be found with id {shoppingCartId}");
if (cart.ShoppingCartItems?.Any(i => i.ProductPriceId == productPriceId) == true)
return Result.Fail($"Item already in shopping cart with id {shoppingCartId}");
context.ShoppingCartItems.Add(new Entities.ShoppingCartItem
{
ShoppingCartId = shoppingCartId,
ProductPriceId = productPriceId,
Quantity = quantity
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to add cart item with id {productPriceId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> AddPackageToShoppingCartAsync(Guid shoppingCartId, Guid packageId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Packages.AnyAsync(p => p.Id == packageId, cancellationToken))
return Result.Fail($"Package cold not be found by ID {packageId}");
var shoppingCart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken);
if (shoppingCart is null)
return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}");
if (!await context.ShoppingCartPackages.AnyAsync(cp => cp.ShoppingCartId == shoppingCartId, cancellationToken))
return Result.Fail($"Package {packageId} is already in the cart");
var newShoppingCartPackage = context.ShoppingCartPackages.Add(new Entities.ShoppingCartPackage
{
ShoppingCartId = shoppingCartId,
PackageId = packageId
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could not add package of id {packageId} to shopping cart {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<Guid>> CreateShoppingCartAsync(Guid customerId, Guid orderId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken))
return Result.Fail<Guid>($"Customer could not be found with id {customerId}");
var cart = context.ShoppingCarts.Add(new Entities.ShoppingCart
{
CustomerId = customerId,
OrderId = orderId
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok(cart.Entity.Id)
: Result.Fail<Guid>($"Failed to create shopping cart for customer id {customerId}");
}
catch (Exception ex)
{
return Result.Fail<Guid>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> EmptyShoppingCartAsync(Guid shoppingCartId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken))
return Result.Fail($"Shopping could not be found with id {shoppingCartId}");
if (await context.ShoppingCartItems.CountAsync(i => i.ShoppingCartId == shoppingCartId, cancellationToken) == 0)
return Result.Ok();
var cartItems = await context.ShoppingCartItems.Where(i => i.ShoppingCartId == shoppingCartId).ToListAsync(cancellationToken);
context.RemoveRange(cartItems);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could not empty cart with id {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<ShoppingCart[]>> GetCustomerShoppingCartsAsync(Guid customerId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Customers.AnyAsync(c => c.Id == customerId, cancellationToken))
return Result.Fail<ShoppingCart[]>(new Error($"Customer with Id {customerId} does not exist."));
var shoppingCarts = await context.ShoppingCarts.Where(sc => sc.CustomerId == customerId).ToArrayAsync(cancellationToken);
return shoppingCarts?.Length > 0
? Result.Ok(shoppingCarts.Select(c => c.ToModel()).ToArray())
: Result.Fail<ShoppingCart[]>(new Error($"No shopping carts found for customer with Id {customerId}."));
}
catch (Exception ex)
{
return Result.Fail<ShoppingCart[]>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<ShoppingCart>> GetShoppingCartAsync(Guid shoppingCartId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var cart = await context.ShoppingCarts.AsNoTracking().FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken);
return cart is not null
? Result.Ok(cart.ToModel())
: Result.Fail<ShoppingCart>($"Failed to find shopping cart with id {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail<ShoppingCart>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<ShoppingCartItem[]>> GetShoppingCartItemsAsync(Guid shoppingCartId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(i => i.Id == shoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found with id {shoppingCartId}");
var items = await context.ShoppingCartItems.AsNoTracking()
.Where(i => i.ShoppingCartId == shoppingCartId).ToArrayAsync(cancellationToken);
return items?.Length > 0
? Result.Ok(items.Select(i => i.ToModel()).ToArray())
: Result.Fail<ShoppingCartItem[]>($"Failed to retrieve shopping cart items with id {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail<ShoppingCartItem[]>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<ShoppingCartPackage[]>> GetShoppingCartPackagesAsync(Guid shoppingCartId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}");
var packages = await context.ShoppingCartPackages.AsNoTracking()
.OrderByDescending(o => o.CreatedAt)
.Where(cp => cp.ShoppingCartId == shoppingCartId)
.ToArrayAsync(cancellationToken);
return packages?.Length > 0
? Result.Ok(packages.Select(p => p.ToModel()).ToArray())
: Result.Fail($"Could not find packaged in shopping cart by ID {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail<ShoppingCartPackage[]>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> RemovePackageFromShoppingCartAsync(Guid shoppingCartId, Guid shoppingCartPackageId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken))
return Result.Fail($"Shopping cart could not be found by ID {shoppingCartId}");
if (!await context.ShoppingCartPackages.AnyAsync(p => p.Id == shoppingCartPackageId, cancellationToken))
return Result.Fail($"Shopping cart package {shoppingCartPackageId} is not in the shopping cart {shoppingCartId}");
var shoppingCartPackage = await context.ShoppingCartPackages.FirstOrDefaultAsync(cp => cp.Id == shoppingCartPackageId, cancellationToken);
if (shoppingCartPackage is null)
return Result.Ok();
context.ShoppingCartPackages.Remove(shoppingCartPackage!);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Could remove package of id {shoppingCartPackageId} from shopping cart {shoppingCartId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> RemoveShoppingCartItemAsync(Guid shoppingCartId, Guid shoppingCartItemId, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ProductPrices.AnyAsync(c => c.Id == shoppingCartItemId, cancellationToken))
return Result.Fail($"Product item could not be found with id {shoppingCartItemId}");
var cart = await context.ShoppingCarts.FirstOrDefaultAsync(c => c.Id == shoppingCartId, cancellationToken);
if (cart is null)
return Result.Fail($"Shopping cart item could not be found with id {shoppingCartId}");
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.Id == shoppingCartItemId, cancellationToken);
if (item is null) return Result.Ok();
context.ShoppingCartItems.Remove(item);
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to remove shopping cart item with id {shoppingCartItemId}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> UpdateShoppingCartItemAsync(Guid shoppingCartId, Guid shoppingCartItemId, int quantity, CancellationToken cancellationToken = default)
{
try
{
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.ShoppingCarts.AnyAsync(c => c.Id == shoppingCartId, cancellationToken))
return Result.Fail($"Shopping could not be found with id {shoppingCartId}");
var item = await context.ShoppingCartItems.FirstOrDefaultAsync(i => i.ShoppingCartId == shoppingCartId, cancellationToken);
if (item is null)
return Result.Fail($"Shopping cart item could not be found with id {shoppingCartItemId}");
item.Quantity = quantity;
item.UpdatedAt = SouthAfricanTimeZone.UtcNow();
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail($"Failed to update cart item quntity");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}