Split Features to create space for more projects
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-05-24 13:19:09 +02:00
parent 032b9e1818
commit 70c6e0bfbc
95 changed files with 621 additions and 314 deletions
@@ -0,0 +1,19 @@
using LiteCharms.Features.TechShop.Customers.Entities;
using LiteCharms.Features.TechShop.Orders.Entities;
using LiteCharms.Features.TechShop.Quotes.Entities;
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
[EntityTypeConfiguration<ShoppingCartConfiguration, ShoppingCart>]
public class ShoppingCart : Models.ShoppingCart
{
public virtual Customer? Customer { get; set; }
public virtual Order? Order { get; set; }
public virtual Quote? Quote { get; set; }
public virtual ICollection<ShoppingCartItem>? ShoppingCartItems { get; set; }
public virtual ICollection<ShoppingCartPackage>? ShoppingCartPackages { get; set; }
}
@@ -0,0 +1,26 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
public class ShoppingCartConfiguration : IEntityTypeConfiguration<ShoppingCart>
{
public void Configure(EntityTypeBuilder<ShoppingCart> builder)
{
builder.ToTable("ShoppingCarts");
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
builder.Property(f => f.UpdatedAt).IsRequired(false).HasDefaultValueSql(null);
builder.Property(f => f.CustomerId).IsRequired();
builder.Property(f => f.OrderId);
builder.HasOne(s => s.Customer)
.WithMany(c => c.ShoppingCarts)
.HasForeignKey(s => s.CustomerId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(s => s.Order)
.WithOne(o => o.ShoppingCart)
.HasForeignKey<ShoppingCart>(s => s.OrderId)
.OnDelete(DeleteBehavior.SetNull);
}
}
@@ -0,0 +1,11 @@
using LiteCharms.Features.TechShop.Products.Entities;
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
[EntityTypeConfiguration<ShoppingCartItemConfiguration, ShoppingCartItem>]
public class ShoppingCartItem : Models.ShoppingCartItem
{
public virtual ShoppingCart? ShoppingCart { get; set; }
public virtual ProductPrice? ProductPrice { get; set; }
}
@@ -0,0 +1,28 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
public class ShoppingCartItemConfiguration : IEntityTypeConfiguration<ShoppingCartItem>
{
public void Configure(EntityTypeBuilder<ShoppingCartItem> builder)
{
builder.ToTable("ShoppingCartItems");
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
builder.Property(f => f.UpdatedAt).IsRequired(false).HasDefaultValueSql(null);
builder.Property(f => f.Quantity).IsRequired().HasDefaultValue(1);
builder.Property(f => f.ShoppingCartId).IsRequired();
builder.Property(f => f.ProductPriceId).IsRequired();
builder.HasOne(f => f.ShoppingCart)
.WithMany(s => s.ShoppingCartItems)
.HasForeignKey(f => f.ShoppingCartId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(f => f.ProductPrice)
.WithMany()
.HasForeignKey(f => f.ProductPriceId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
}
}
@@ -0,0 +1,11 @@
using LiteCharms.Features.TechShop.CartPackages.Entities;
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
[EntityTypeConfiguration<ShoppingCartPackageConfiguration, ShoppingCartPackage>]
public class ShoppingCartPackage : Models.ShoppingCartPackage
{
public virtual ShoppingCart? ShoppingCart { get; set; }
public virtual Package? Package { get; set; }
}
@@ -0,0 +1,26 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Entities;
public class ShoppingCartPackageConfiguration : IEntityTypeConfiguration<ShoppingCartPackage>
{
public void Configure(EntityTypeBuilder<ShoppingCartPackage> builder)
{
builder.ToTable("ShoppingCartPackages");
builder.HasKey(f => f.Id);
builder.Property(f => f.CreatedAt).IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("now()");
builder.Property(f => f.ShoppingCartId).IsRequired();
builder.Property(f => f.PackageId).IsRequired();
builder.HasOne(f => f.ShoppingCart)
.WithMany(s => s.ShoppingCartPackages)
.HasForeignKey(scp => scp.ShoppingCartId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(f => f.Package)
.WithMany()
.HasForeignKey(scp => scp.PackageId)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
}
}
@@ -0,0 +1,14 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Models;
public class ShoppingCart
{
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public Guid CustomerId { get; set; }
public Guid? OrderId { get; set; }
}
@@ -0,0 +1,16 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Models;
public class ShoppingCartItem
{
public Guid Id { get; set; }
public Guid ShoppingCartId { get; set; }
public Guid ProductPriceId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public int Quantity { get; set; }
}
@@ -0,0 +1,12 @@
namespace LiteCharms.Features.TechShop.ShoppingCarts.Models;
public class ShoppingCartPackage
{
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
public Guid ShoppingCartId { get; set; }
public Guid PackageId { get; set; }
}
@@ -0,0 +1,297 @@
using LiteCharms.Features.TechShop.Extensions;
using LiteCharms.Features.TechShop.Postgres;
using LiteCharms.Features.TechShop.ShoppingCarts.Models;
namespace LiteCharms.Features.TechShop.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 = DateTime.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));
}
}
}