Added product categories
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-05-30 15:35:35 +02:00
parent e40c958066
commit 18d1640808
16 changed files with 1318 additions and 39 deletions
@@ -3,5 +3,7 @@
[EntityTypeConfiguration<ProductConfiguration, Product>]
public class Product : Models.Product
{
public virtual ICollection<ProductCategory> Categories { get; set; } = [];
public virtual ICollection<ProductPrice> Prices { get; set; } = [];
}
@@ -0,0 +1,11 @@
using LiteCharms.Features.MidrandBooks.Categories.Entities;
namespace LiteCharms.Features.MidrandBooks.Products.Entities;
[EntityTypeConfiguration<ProductCategoryConfiguration, ProductCategory>]
public class ProductCategory : Models.ProductCategory
{
public virtual Product? Product { get; set; }
public virtual Category? Category { get; set; }
}
@@ -0,0 +1,23 @@
namespace LiteCharms.Features.MidrandBooks.Products.Entities;
public sealed class ProductCategoryConfiguration : IEntityTypeConfiguration<ProductCategory>
{
public void Configure(EntityTypeBuilder<ProductCategory> builder)
{
builder.ToTable("ProductCategories");
builder.HasKey(p => p.Id);
builder.Property(p => p.ProductId).IsRequired();
builder.Property(p => p.CategoryId).IsRequired();
builder.HasOne(p => p.Product)
.WithMany(p => p.Categories)
.HasForeignKey(p => p.ProductId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne(c => c.Category)
.WithMany()
.HasForeignKey(c => c.CategoryId)
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -17,7 +17,6 @@ public sealed class ProductConfiguration : IEntityTypeConfiguration<Product>
builder.Property(f => f.Description).HasMaxLength(1024);
builder.Property(f => f.ImageUrl).HasMaxLength(1024);
builder.Property(f => f.Enabled).HasDefaultValue(false);
builder.Property(f => f.Categories).IsRequired(false);
builder.Property(f => f.ThumbnailUrls).IsRequired(false);
builder.OwnsOne(f => f.Metadata, b => { b.ToJson(); });
@@ -22,8 +22,6 @@ public class Product
public string[]? ThumbnailUrls { get; set; }
public string[]? Categories { get; set; }
public ProductMetadata? Metadata { get; set; }
public ProductPrice? Price { get; set; }
@@ -0,0 +1,10 @@
namespace LiteCharms.Features.MidrandBooks.Products.Models;
public class ProductCategory
{
public long Id { get; set; }
public long ProductId { get; set; }
public long CategoryId { get; set; }
}
@@ -1,4 +1,5 @@
using LiteCharms.Features.MidrandBooks.Abstractions;
using LiteCharms.Features.MidrandBooks.Categories.Models;
using LiteCharms.Features.MidrandBooks.Extensions;
using LiteCharms.Features.MidrandBooks.Postgres;
using LiteCharms.Features.MidrandBooks.Products.Models;
@@ -8,6 +9,100 @@ namespace LiteCharms.Features.MidrandBooks.Products;
public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
{
public async ValueTask<Result> AddProductCategoryAsync(long productId, long categoryId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!await context.Products.AnyAsync(p => p.Id == productId && p.Enabled, cancellationToken))
return Result.Fail("Product does not exist");
if (!await context.Categories.AnyAsync(c => c.Id == categoryId && c.Enabled, cancellationToken))
return Result.Fail("Category does not exist");
if (await context.ProductCategories.AnyAsync(c => c.ProductId == productId && c.CategoryId == categoryId, cancellationToken))
return Result.Fail("Category already assigned to product");
context.ProductCategories.Add(new Entities.ProductCategory
{
ProductId = productId,
CategoryId = categoryId,
});
return await context.SaveChangesAsync(cancellationToken) > 0
? Result.Ok()
: Result.Fail("Could not add category to product");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<Category[]>> GetProductCategoriesAsync(long productId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var categories = await context.ProductCategories.AsNoTracking()
.Where(p => p.ProductId == productId)
.OrderByDescending(o => o.Id)
.Select(p => p.Category)
.ToArrayAsync(cancellationToken);
return categories?.Length > 0
? Result.Ok(categories.Select(c => c!.ToModel()).ToArray())
: Result.Fail<Category[]>("Failed to get product categories");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> DeleteProductCategoryAsync(long productId, long categoryId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var rowsDeleted = await context.ProductCategories
.Where(p => p.ProductId == productId && p.CategoryId == categoryId)
.ExecuteDeleteAsync(cancellationToken);
return rowsDeleted > 0
? Result.Ok()
: Result.Fail("No product categories were deleted");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> DeleteAllProductCategoriesAsync(long productId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var rowsDeleted = await context.ProductCategories
.Where(p => p.ProductId == productId)
.ExecuteDeleteAsync(cancellationToken);
return rowsDeleted > 0
? Result.Ok()
: Result.Fail("No product categories were deleted");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> UpdateProductPriceStatusAsync(long productPriceId, bool isEnabled, CancellationToken cancellationToken = default)
{
try
@@ -68,9 +163,6 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
if (!string.IsNullOrWhiteSpace(filter.Title))
query = query.Where(p => EF.Functions.ILike(p.Name!, $"%{filter.Title}%"));
if (!string.IsNullOrWhiteSpace(filter.Category))
query = query.Where(p => p.Categories.Contains(filter.Category));
if (!string.IsNullOrWhiteSpace(filter.Manufacturer))
query = query.Where(p => EF.Functions.ILike(p.Metadata!.Manufacturer!, $"%{filter.Manufacturer}%"));
@@ -152,7 +244,6 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
ImageUrl = request.ImageUrl,
ThumbnailUrls = request.ThumbnailUrls,
Metadata = request.Metadata,
Categories = request.Categories,
Enabled = true
});