diff --git a/VichVich.BuildingBlocks.Infrastructure/Vizhvizh.BuildingBlocks.Infrastructure.csproj b/VichVich.BuildingBlocks.Infrastructure/Vizhvizh.BuildingBlocks.Infrastructure.csproj index e01a0d4..0badb5d 100644 --- a/VichVich.BuildingBlocks.Infrastructure/Vizhvizh.BuildingBlocks.Infrastructure.csproj +++ b/VichVich.BuildingBlocks.Infrastructure/Vizhvizh.BuildingBlocks.Infrastructure.csproj @@ -6,6 +6,11 @@ enable + + + + + diff --git a/Vichvich.Shop.Infrastructure/EF/Configurations/CategoryConfiguration.cs b/Vichvich.Shop.Infrastructure/EF/Configurations/CategoryConfiguration.cs index af981e0..5d0a129 100644 --- a/Vichvich.Shop.Infrastructure/EF/Configurations/CategoryConfiguration.cs +++ b/Vichvich.Shop.Infrastructure/EF/Configurations/CategoryConfiguration.cs @@ -35,7 +35,11 @@ internal class CategoryConfiguration : BaseEntityConfiguration x.Path) .HasField("_path") .IsRequired() - .HasConversion(x => x.Value, x => Domain.Products.ValuesObjects.CategoryPath.CreateNew(x)) + .HasConversion( + // Convert CategoryPath → HierarchyId + categoryPath => HierarchyId.Parse(categoryPath.Value), + // Convert HierarchyId → CategoryPath + hierarchyId => CategoryPath.CreateNew(hierarchyId.ToString())) .HasColumnType("HIERARCHYID"); diff --git a/Vichvich.Shop.Infrastructure/ShopStartup.cs b/Vichvich.Shop.Infrastructure/ShopStartup.cs index 95f4d4e..22662d2 100644 --- a/Vichvich.Shop.Infrastructure/ShopStartup.cs +++ b/Vichvich.Shop.Infrastructure/ShopStartup.cs @@ -26,7 +26,7 @@ public class ShopStartup { services.AddDbContext(op => { - op.UseSqlServer(connectionString); + op.UseSqlServer(connectionString, sqlOption=> sqlOption.UseHierarchyId()); }); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(); diff --git a/Vizhvizh.GoldGallery.Application/CommandHandlers/Categories/CreateCategoryCommandHandler.cs b/Vizhvizh.GoldGallery.Application/CommandHandlers/Categories/CreateCategoryCommandHandler.cs index d6b50f7..e0865f0 100644 --- a/Vizhvizh.GoldGallery.Application/CommandHandlers/Categories/CreateCategoryCommandHandler.cs +++ b/Vizhvizh.GoldGallery.Application/CommandHandlers/Categories/CreateCategoryCommandHandler.cs @@ -1,4 +1,5 @@ -using Vizhvizh.BuildingBlocks.Application.Configuration.Commnad; +using Microsoft.EntityFrameworkCore; +using Vizhvizh.BuildingBlocks.Application.Configuration.Commnad; using Vizhvizh.GoldGallery.ApplicationContract.Commands; using Vizhvizh.GoldGallery.Domain.Products.Entities; using Vizhvizh.GoldGallery.Domain.Products.Repositories; @@ -10,10 +11,24 @@ public class CreateCategoryCommandHandler(ICategoryRepository categoryRepository { public async Task Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { + CategoryPath? parentPath = null; + + if(request.ParentId != null) + { + parentPath = await categoryRepository.GetQueryAsNoTracking() + .Where(x => x.Id == request.ParentId) + .Select(x => x.Path) + .FirstOrDefaultAsync(cancellationToken); + } + var category = Category.CreateNew(request.Title, request.Description, - request.Image, CategoryPath.CreateNew("/1/")); + request.Image, + request.ParentId); await categoryRepository.AddAsync(category, cancellationToken); + + category.SetPath(parentPath); + } } diff --git a/Vizhvizh.GoldGallery.Domain/Products/Entities/Category.cs b/Vizhvizh.GoldGallery.Domain/Products/Entities/Category.cs index bea94b4..c1f0bde 100644 --- a/Vizhvizh.GoldGallery.Domain/Products/Entities/Category.cs +++ b/Vizhvizh.GoldGallery.Domain/Products/Entities/Category.cs @@ -28,24 +28,20 @@ public class Category : Entity private Category(TitleVO title, DescriptionVO description, CategoryImage image, - CategoryPath path, CategoryId? parentId = null) { _title = title; _description = description; _image = image; _parentId = parentId; - _path = path; - } public static Category CreateNew(TitleVO title, DescriptionVO description, CategoryImage image, - CategoryPath path, CategoryId? parentId = null) { - return new Category(title, description, image, path, parentId); + return new Category(title, description, image, parentId); } public TitleVO Title => _title; @@ -55,5 +51,9 @@ public class Category : Entity public CategoryPath Path => _path; public IReadOnlyList Products => _products; + public void SetPath(CategoryPath? parentPath) + { + _path = CategoryPath.CreatePath(parentPath, Id); + } } diff --git a/Vizhvizh.GoldGallery.Domain/Products/ValuesObjects/CategoryPath.cs b/Vizhvizh.GoldGallery.Domain/Products/ValuesObjects/CategoryPath.cs index 219a61d..1843872 100644 --- a/Vizhvizh.GoldGallery.Domain/Products/ValuesObjects/CategoryPath.cs +++ b/Vizhvizh.GoldGallery.Domain/Products/ValuesObjects/CategoryPath.cs @@ -1,4 +1,5 @@ using Vizhvizh.BuildingBlocks.Domain; +using Vizhvizh.GoldGallery.Domain.Products.TypeIds; namespace Vizhvizh.GoldGallery.Domain.Products.ValuesObjects; @@ -10,15 +11,15 @@ public class CategoryPath : ValueObject Value = value; } public string Value { get; private set; } + public static CategoryPath CreateNew(string value) - => new CategoryPath(value); + => new(value); - - public static CategoryPath CreatePath(CategoryPath? parentPath, string childId) + internal static CategoryPath CreatePath(CategoryPath? parentPath, CategoryId childId) { if (parentPath == null) - return new CategoryPath(childId); + return new CategoryPath($"/{childId.Value}/"); - return new CategoryPath($"{parentPath.Value}/{childId}"); + return new CategoryPath($"{parentPath.Value}{childId.Value}/"); } } diff --git a/Vizhvizh.GoldGallery.Infrastructure/EF/Configurations/CategoryConfiguration.cs b/Vizhvizh.GoldGallery.Infrastructure/EF/Configurations/CategoryConfiguration.cs index e8f8d3e..aef00d1 100644 --- a/Vizhvizh.GoldGallery.Infrastructure/EF/Configurations/CategoryConfiguration.cs +++ b/Vizhvizh.GoldGallery.Infrastructure/EF/Configurations/CategoryConfiguration.cs @@ -37,7 +37,11 @@ internal class CategoryConfiguration : BaseEntityConfiguration x.Path) .HasField("_path") .IsRequired() - .HasConversion(x => x.Value, x => Domain.Products.ValuesObjects.CategoryPath.CreateNew(x)) + .HasConversion( + // Convert CategoryPath → HierarchyId + categoryPath => HierarchyId.Parse(categoryPath.Value), + // Convert HierarchyId → CategoryPath + hierarchyId => CategoryPath.CreateNew(hierarchyId.ToString())) .HasColumnType("HIERARCHYID"); // ایندکس برای Path با نوع HIERARCHYID diff --git a/Vizhvizh.GoldGallery.Infrastructure/GoldGalleryStartup.cs b/Vizhvizh.GoldGallery.Infrastructure/GoldGalleryStartup.cs index 6ee9290..668761f 100644 --- a/Vizhvizh.GoldGallery.Infrastructure/GoldGalleryStartup.cs +++ b/Vizhvizh.GoldGallery.Infrastructure/GoldGalleryStartup.cs @@ -28,7 +28,7 @@ public class GoldGalleryStartup { services.AddDbContext(op => { - op.UseSqlServer(connectionString); + op.UseSqlServer(connectionString, sqlOptions => sqlOptions.UseHierarchyId()); }); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped();