This commit is contained in:
@@ -62,7 +62,7 @@ public class CategoryServiceFeatureTests(Fixture fixture) : IClassFixture<Fixtur
|
|||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task CreateCategoryAsync_ShouldReturn_ResultWithCategoryId()
|
public async Task CreateCategoryAsync_ShouldReturn_ResultWithCategoryId()
|
||||||
{
|
{
|
||||||
var result = await categoryService.CreateCategoryAsync("Test", false, fixture.CancellationToken);
|
var result = await categoryService.CreateCategoryAsync("Test", true, fixture.CancellationToken);
|
||||||
|
|
||||||
Assert.True(result.IsSuccess);
|
Assert.True(result.IsSuccess);
|
||||||
Assert.True(result.Value > 0);
|
Assert.True(result.Value > 0);
|
||||||
|
|||||||
@@ -10,7 +10,40 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
private readonly ProductService productService = fixture.Services.GetRequiredService<ProductService>();
|
private readonly ProductService productService = fixture.Services.GetRequiredService<ProductService>();
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task GetProductPriceAsync_ShouldReturn_RetultOneProductPrice()
|
public async Task AddProductCategoryAsync_ShouldReturn_ResultWithId()
|
||||||
|
{
|
||||||
|
var result = await productService.AddProductCategoryAsync(1, 2, fixture.CancellationToken);
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
[IntegrationFact]
|
||||||
|
public async Task GetProductCategoriesAsync_ShouldReturn_ResultWithCategoryList()
|
||||||
|
{
|
||||||
|
var result = await productService.GetProductCategoriesAsync(1, fixture.CancellationToken);
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.NotEmpty(result.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[IntegrationFact]
|
||||||
|
public async Task DeleteProductCategoryAsync_ShouldReturn_ResultWithSuccess()
|
||||||
|
{
|
||||||
|
var result = await productService.DeleteProductCategoryAsync(1, 1, fixture.CancellationToken);
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
[IntegrationFact]
|
||||||
|
public async Task DeleteAllProductCategoriesAsync_ShouldReturn_ResultWithSuccess()
|
||||||
|
{
|
||||||
|
var result = await productService.DeleteAllProductCategoriesAsync(1, fixture.CancellationToken);
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
[IntegrationFact]
|
||||||
|
public async Task GetProductPriceAsync_ShouldReturn_ResultOneProductPrice()
|
||||||
{
|
{
|
||||||
var result = await productService.GetProductPriceAsync(2, fixture.CancellationToken);
|
var result = await productService.GetProductPriceAsync(2, fixture.CancellationToken);
|
||||||
|
|
||||||
@@ -21,7 +54,7 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task GetProductPricesAsync_ShouldReturn_RetultProductPriceList()
|
public async Task GetProductPricesAsync_ShouldReturn_ResultProductPriceList()
|
||||||
{
|
{
|
||||||
var result = await productService.GetProductPricesAsync(2, fixture.CancellationToken);
|
var result = await productService.GetProductPricesAsync(2, fixture.CancellationToken);
|
||||||
|
|
||||||
@@ -32,7 +65,7 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task SearchProductsAsync_ShouldReturn_RetultMatchingProducts()
|
public async Task SearchProductsAsync_ShouldReturn_ResultMatchingProducts()
|
||||||
{
|
{
|
||||||
var filter = new ProductFilter
|
var filter = new ProductFilter
|
||||||
{
|
{
|
||||||
@@ -52,7 +85,7 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task GetProductAsync_ShouldReturn_RetultOneProduct()
|
public async Task GetProductAsync_ShouldReturn_ResultOneProduct()
|
||||||
{
|
{
|
||||||
var result = await productService.GetProductAsync(2, fixture.CancellationToken);
|
var result = await productService.GetProductAsync(2, fixture.CancellationToken);
|
||||||
|
|
||||||
@@ -63,7 +96,7 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task GetProductsAsync_ShouldReturn_RetultProducts()
|
public async Task GetProductsAsync_ShouldReturn_ResultProducts()
|
||||||
{
|
{
|
||||||
var range = new DateRange
|
var range = new DateRange
|
||||||
{
|
{
|
||||||
@@ -81,7 +114,7 @@ public class ProductServiceFeatureTests(Fixture fixture, ITestOutputHelper outpu
|
|||||||
}
|
}
|
||||||
|
|
||||||
[IntegrationFact]
|
[IntegrationFact]
|
||||||
public async Task UpdateProductStatusAsync_ShouldReturn_ResultTrue()
|
public async Task UpdateProductStatusAsync_ShouldResurn_ResultTrue()
|
||||||
{
|
{
|
||||||
var result = await productService.UpdateProductStatusAsync(2, true, fixture.CancellationToken);
|
var result = await productService.UpdateProductStatusAsync(2, true, fixture.CancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public sealed class CategoryService(IDbContextFactory<MidrandBooksDbContext> con
|
|||||||
var query = context.Categories.AsNoTracking()
|
var query = context.Categories.AsNoTracking()
|
||||||
.OrderByDescending(o => o.IsMain)
|
.OrderByDescending(o => o.IsMain)
|
||||||
.ThenByDescending(o => o.Id)
|
.ThenByDescending(o => o.Id)
|
||||||
.ThenBy(o => o.IsMain)
|
.ThenBy(o => o.Name)
|
||||||
.AsQueryable();
|
.AsQueryable();
|
||||||
|
|
||||||
query = isMain is null
|
query = isMain is null
|
||||||
|
|||||||
@@ -147,9 +147,7 @@ public static class Mappers
|
|||||||
Enabled = entity.Enabled
|
Enabled = entity.Enabled
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Product ToModel(this Products.Entities.Product entity)
|
public static Product ToModel(this Products.Entities.Product entity) => new Product
|
||||||
{
|
|
||||||
return new Product
|
|
||||||
{
|
{
|
||||||
Id = entity.Id,
|
Id = entity.Id,
|
||||||
CreatedAt = entity.CreatedAt,
|
CreatedAt = entity.CreatedAt,
|
||||||
@@ -161,11 +159,9 @@ public static class Mappers
|
|||||||
ImageUrl = entity.ImageUrl,
|
ImageUrl = entity.ImageUrl,
|
||||||
ThumbnailUrls = entity.ThumbnailUrls,
|
ThumbnailUrls = entity.ThumbnailUrls,
|
||||||
Metadata = entity.Metadata,
|
Metadata = entity.Metadata,
|
||||||
Categories = entity.Categories,
|
|
||||||
Enabled = entity.Enabled,
|
Enabled = entity.Enabled,
|
||||||
Price = entity.Prices?.FirstOrDefault(p => p.Enabled)?.ToModel() ?? null,
|
Price = entity.Prices?.FirstOrDefault(p => p.Enabled)?.ToModel() ?? null,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
public static Author ToModel(this Authors.Entities.Author entity) => new()
|
public static Author ToModel(this Authors.Entities.Author entity) => new()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,4 +38,6 @@ public sealed class MidrandBooksDbContext(DbContextOptions<MidrandBooksDbContext
|
|||||||
public DbSet<ShippingProvider> ShippingProviders => Set<ShippingProvider>();
|
public DbSet<ShippingProvider> ShippingProviders => Set<ShippingProvider>();
|
||||||
|
|
||||||
public DbSet<Category> Categories => Set<Category>();
|
public DbSet<Category> Categories => Set<Category>();
|
||||||
|
|
||||||
|
public DbSet<ProductCategory> ProductCategories => Set<ProductCategory>();
|
||||||
}
|
}
|
||||||
|
|||||||
+1007
File diff suppressed because it is too large
Load Diff
+68
@@ -0,0 +1,68 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace LiteCharms.Features.MidrandBooks.Postgres.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddedProductCategories : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Categories",
|
||||||
|
table: "Products");
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "ProductCategories",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<long>(type: "bigint", nullable: false)
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
ProductId = table.Column<long>(type: "bigint", nullable: false),
|
||||||
|
CategoryId = table.Column<long>(type: "bigint", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_ProductCategories", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_ProductCategories_Categories_CategoryId",
|
||||||
|
column: x => x.CategoryId,
|
||||||
|
principalTable: "Categories",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_ProductCategories_Products_ProductId",
|
||||||
|
column: x => x.ProductId,
|
||||||
|
principalTable: "Products",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_ProductCategories_CategoryId",
|
||||||
|
table: "ProductCategories",
|
||||||
|
column: "CategoryId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_ProductCategories_ProductId",
|
||||||
|
table: "ProductCategories",
|
||||||
|
column: "ProductId");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "ProductCategories");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string[]>(
|
||||||
|
name: "Categories",
|
||||||
|
table: "Products",
|
||||||
|
type: "text[]",
|
||||||
|
nullable: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+44
-3
@@ -586,9 +586,6 @@ namespace LiteCharms.Features.MidrandBooks.Postgres.Migrations
|
|||||||
|
|
||||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
b.PrimitiveCollection<string[]>("Categories")
|
|
||||||
.HasColumnType("text[]");
|
|
||||||
|
|
||||||
b.Property<DateTime>("CreatedAt")
|
b.Property<DateTime>("CreatedAt")
|
||||||
.ValueGeneratedOnAdd()
|
.ValueGeneratedOnAdd()
|
||||||
.HasColumnType("timestamp with time zone")
|
.HasColumnType("timestamp with time zone")
|
||||||
@@ -633,6 +630,29 @@ namespace LiteCharms.Features.MidrandBooks.Postgres.Migrations
|
|||||||
b.ToTable("Products", (string)null);
|
b.ToTable("Products", (string)null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductCategory", b =>
|
||||||
|
{
|
||||||
|
b.Property<long>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
|
b.Property<long>("CategoryId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.Property<long>("ProductId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("CategoryId");
|
||||||
|
|
||||||
|
b.HasIndex("ProductId");
|
||||||
|
|
||||||
|
b.ToTable("ProductCategories", (string)null);
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductPrice", b =>
|
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductPrice", b =>
|
||||||
{
|
{
|
||||||
b.Property<long>("Id")
|
b.Property<long>("Id")
|
||||||
@@ -911,6 +931,25 @@ namespace LiteCharms.Features.MidrandBooks.Postgres.Migrations
|
|||||||
b.Navigation("Metadata");
|
b.Navigation("Metadata");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductCategory", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("LiteCharms.Features.MidrandBooks.Categories.Entities.Category", "Category")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("CategoryId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("LiteCharms.Features.MidrandBooks.Products.Entities.Product", "Product")
|
||||||
|
.WithMany("Categories")
|
||||||
|
.HasForeignKey("ProductId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Category");
|
||||||
|
|
||||||
|
b.Navigation("Product");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductPrice", b =>
|
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.ProductPrice", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("LiteCharms.Features.MidrandBooks.Products.Entities.Product", "Product")
|
b.HasOne("LiteCharms.Features.MidrandBooks.Products.Entities.Product", "Product")
|
||||||
@@ -955,6 +994,8 @@ namespace LiteCharms.Features.MidrandBooks.Postgres.Migrations
|
|||||||
|
|
||||||
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.Product", b =>
|
modelBuilder.Entity("LiteCharms.Features.MidrandBooks.Products.Entities.Product", b =>
|
||||||
{
|
{
|
||||||
|
b.Navigation("Categories");
|
||||||
|
|
||||||
b.Navigation("Prices");
|
b.Navigation("Prices");
|
||||||
});
|
});
|
||||||
#pragma warning restore 612, 618
|
#pragma warning restore 612, 618
|
||||||
|
|||||||
@@ -3,5 +3,7 @@
|
|||||||
[EntityTypeConfiguration<ProductConfiguration, Product>]
|
[EntityTypeConfiguration<ProductConfiguration, Product>]
|
||||||
public class Product : Models.Product
|
public class Product : Models.Product
|
||||||
{
|
{
|
||||||
|
public virtual ICollection<ProductCategory> Categories { get; set; } = [];
|
||||||
|
|
||||||
public virtual ICollection<ProductPrice> Prices { 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.Description).HasMaxLength(1024);
|
||||||
builder.Property(f => f.ImageUrl).HasMaxLength(1024);
|
builder.Property(f => f.ImageUrl).HasMaxLength(1024);
|
||||||
builder.Property(f => f.Enabled).HasDefaultValue(false);
|
builder.Property(f => f.Enabled).HasDefaultValue(false);
|
||||||
builder.Property(f => f.Categories).IsRequired(false);
|
|
||||||
builder.Property(f => f.ThumbnailUrls).IsRequired(false);
|
builder.Property(f => f.ThumbnailUrls).IsRequired(false);
|
||||||
|
|
||||||
builder.OwnsOne(f => f.Metadata, b => { b.ToJson(); });
|
builder.OwnsOne(f => f.Metadata, b => { b.ToJson(); });
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ public class Product
|
|||||||
|
|
||||||
public string[]? ThumbnailUrls { get; set; }
|
public string[]? ThumbnailUrls { get; set; }
|
||||||
|
|
||||||
public string[]? Categories { get; set; }
|
|
||||||
|
|
||||||
public ProductMetadata? Metadata { get; set; }
|
public ProductMetadata? Metadata { get; set; }
|
||||||
|
|
||||||
public ProductPrice? Price { 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.Abstractions;
|
||||||
|
using LiteCharms.Features.MidrandBooks.Categories.Models;
|
||||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||||
using LiteCharms.Features.MidrandBooks.Postgres;
|
using LiteCharms.Features.MidrandBooks.Postgres;
|
||||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||||
@@ -8,6 +9,100 @@ namespace LiteCharms.Features.MidrandBooks.Products;
|
|||||||
|
|
||||||
public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
|
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)
|
public async ValueTask<Result> UpdateProductPriceStatusAsync(long productPriceId, bool isEnabled, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -68,9 +163,6 @@ public sealed class ProductService(IDbContextFactory<MidrandBooksDbContext> cont
|
|||||||
if (!string.IsNullOrWhiteSpace(filter.Title))
|
if (!string.IsNullOrWhiteSpace(filter.Title))
|
||||||
query = query.Where(p => EF.Functions.ILike(p.Name!, $"%{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))
|
if (!string.IsNullOrWhiteSpace(filter.Manufacturer))
|
||||||
query = query.Where(p => EF.Functions.ILike(p.Metadata!.Manufacturer!, $"%{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,
|
ImageUrl = request.ImageUrl,
|
||||||
ThumbnailUrls = request.ThumbnailUrls,
|
ThumbnailUrls = request.ThumbnailUrls,
|
||||||
Metadata = request.Metadata,
|
Metadata = request.Metadata,
|
||||||
Categories = request.Categories,
|
|
||||||
Enabled = true
|
Enabled = true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ public sealed class ProductFilter
|
|||||||
|
|
||||||
public string? Title { get; set; }
|
public string? Title { get; set; }
|
||||||
|
|
||||||
public string? Category { get; set; }
|
|
||||||
|
|
||||||
public string? Manufacturer { get; set; }
|
public string? Manufacturer { get; set; }
|
||||||
|
|
||||||
public string? SerialNumber { get; set; }
|
public string? SerialNumber { get; set; }
|
||||||
|
|||||||
Reference in New Issue
Block a user