Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add ISlugNameValidator interface The ISlugNameValidator interface is used for validating the SEO name (seName) of entities that implement the ISlugEntity interface. The interface includes a method, ValidateSeName, which takes an entity, seName, name, and ensureNotEmpty parameter, and returns a validated seName. This change improves the SEO functionality of the application.
KrzysztofPajakc46e0363ed51
diff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/AddBrandCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/AddBrandCommandHandler.csindex a04db90aa..f70d4e176 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/AddBrandCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/AddBrandCommandHandler.cs@@ -1,11 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Brands;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;-using Grand.Domain.Seo;+using Grand.Domain.Catalog;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -13,28 +11,23 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class AddBrandCommandHandler : IRequestHandler<AddBrandCommand, BrandDto>{private readonly IBrandService _brandService;- private readonly ILanguageService _languageService;- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;-+ private readonly ISlugNameValidator _slugNameValidator;public AddBrandCommandHandler(IBrandService brandService,ISlugService slugService,- ILanguageService languageService,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_brandService = brandService;_slugService = slugService;- _languageService = languageService;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<BrandDto> Handle(AddBrandCommand request, CancellationToken cancellationToken){var brand = request.Model.ToEntity();await _brandService.InsertBrand(brand);- request.Model.SeName = await brand.ValidateSeName(request.Model.SeName, brand.Name, true, _seoSettings,- _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(brand, request.Model.SeName, brand.Name, true);brand.SeName = request.Model.SeName;await _brandService.UpdateBrand(brand);await _slugService.SaveSlug(brand, request.Model.SeName, "");diff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/AddCategoryCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/AddCategoryCommandHandler.csindex 77e565e61..2bbd8f355 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/AddCategoryCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/AddCategoryCommandHandler.cs@@ -1,11 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Categories;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;-using Grand.Domain.Seo;+using Grand.Domain.Catalog;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -13,28 +11,24 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class AddCategoryCommandHandler : IRequestHandler<AddCategoryCommand, CategoryDto>{private readonly ICategoryService _categoryService;- private readonly ILanguageService _languageService;- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;+ private readonly ISlugNameValidator _slugNameValidator;public AddCategoryCommandHandler(ICategoryService categoryService,ISlugService slugService,- ILanguageService languageService,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_categoryService = categoryService;_slugService = slugService;- _languageService = languageService;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<CategoryDto> Handle(AddCategoryCommand request, CancellationToken cancellationToken){var category = request.Model.ToEntity();await _categoryService.InsertCategory(category);- request.Model.SeName = await category.ValidateSeName(request.Model.SeName,- category.Name, true, _seoSettings, _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(category, request.Model.SeName, category.Name, true);category.SeName = request.Model.SeName;await _categoryService.UpdateCategory(category);await _slugService.SaveSlug(category, request.Model.SeName, "");diff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/AddCollectionCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/AddCollectionCommandHandler.csindex dae7c096a..39b4304d2 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/AddCollectionCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/AddCollectionCommandHandler.cs@@ -1,12 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Collections;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;-using Grand.Domain.Seo;-using Grand.Infrastructure;+using Grand.Domain.Catalog;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -14,34 +11,23 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class AddCollectionCommandHandler : IRequestHandler<AddCollectionCommand, CollectionDto>{private readonly ICollectionService _collectionService;- private readonly ILanguageService _languageService;- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public AddCollectionCommandHandler(ICollectionService collectionService,ISlugService slugService,- ILanguageService languageService,- ITranslationService translationService,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_collectionService = collectionService;_slugService = slugService;- _languageService = languageService;- _translationService = translationService;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<CollectionDto> Handle(AddCollectionCommand request, CancellationToken cancellationToken){var collection = request.Model.ToEntity();await _collectionService.InsertCollection(collection);- request.Model.SeName = await collection.ValidateSeName(request.Model.SeName, collection.Name, true,- _seoSettings, _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(collection, request.Model.SeName, collection.Name, true);collection.SeName = request.Model.SeName;await _collectionService.UpdateCollection(collection);await _slugService.SaveSlug(collection, request.Model.SeName, "");diff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/AddProductCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/AddProductCommandHandler.csindex eae1c4e40..f6d8024f1 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/AddProductCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/AddProductCommandHandler.cs@@ -5,6 +5,7 @@ using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Domain.Catalog;using Grand.Domain.Seo;using Grand.Infrastructure;using MediatR;@@ -13,28 +14,17 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class AddProductCommandHandler : IRequestHandler<AddProductCommand, ProductDto>{- private readonly ILanguageService _languageService;private readonly IProductService _productService;-- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public AddProductCommandHandler(IProductService productService,ISlugService slugService,- ITranslationService translationService,- ILanguageService languageService,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_productService = productService;_slugService = slugService;- _translationService = translationService;- _languageService = languageService;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<ProductDto> Handle(AddProductCommand request, CancellationToken cancellationToken)@@ -42,8 +32,7 @@ public class AddProductCommandHandler : IRequestHandler<AddProductCommand, Produvar product = request.Model.ToEntity();await _productService.InsertProduct(product);- request.Model.SeName = await product.ValidateSeName(request.Model.SeName, product.Name, true, _seoSettings,- _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(product, request.Model.SeName, product.Name, true);product.SeName = request.Model.SeName;//search engine nameawait _slugService.SaveSlug(product, request.Model.SeName, "");diff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateBrandCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateBrandCommandHandler.csindex 7c2278082..b6c6f590d 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateBrandCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateBrandCommandHandler.cs@@ -1,13 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Brands;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.Storage;-using Grand.Domain.Seo;-using Grand.Infrastructure;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -15,30 +11,19 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class UpdateBrandCommandHandler : IRequestHandler<UpdateBrandCommand, BrandDto>{private readonly IBrandService _brandService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public UpdateBrandCommandHandler(IBrandService brandService,ISlugService slugService,- ILanguageService languageService,- ITranslationService translationService,IPictureService pictureService,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_brandService = brandService;_slugService = slugService;- _languageService = languageService;- _translationService = translationService;_pictureService = pictureService;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<BrandDto> Handle(UpdateBrandCommand request, CancellationToken cancellationToken)@@ -46,8 +31,7 @@ public class UpdateBrandCommandHandler : IRequestHandler<UpdateBrandCommand, Bravar brand = await _brandService.GetBrandById(request.Model.Id);var prevPictureId = brand.PictureId;brand = request.Model.ToEntity(brand);- request.Model.SeName = await brand.ValidateSeName(request.Model.SeName, brand.Name, true, _seoSettings,- _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(brand, request.Model.SeName, brand.Name, true);brand.SeName = request.Model.SeName;await _brandService.UpdateBrand(brand);//search engine namediff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCategoryCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCategoryCommandHandler.csindex af4df615c..5294c9aed 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCategoryCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCategoryCommandHandler.cs@@ -1,13 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Categories;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.Storage;-using Grand.Domain.Seo;-using Grand.Infrastructure;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -15,30 +11,19 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class UpdateCategoryCommandHandler : IRequestHandler<UpdateCategoryCommand, CategoryDto>{private readonly ICategoryService _categoryService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public UpdateCategoryCommandHandler(ICategoryService categoryService,ISlugService slugService,- ILanguageService languageService,- ITranslationService translationService,IPictureService pictureService,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_categoryService = categoryService;_slugService = slugService;- _languageService = languageService;- _translationService = translationService;_pictureService = pictureService;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<CategoryDto> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)@@ -46,8 +31,7 @@ public class UpdateCategoryCommandHandler : IRequestHandler<UpdateCategoryCommanvar category = await _categoryService.GetCategoryById(request.Model.Id);var prevPictureId = category.PictureId;category = request.Model.ToEntity(category);- request.Model.SeName = await category.ValidateSeName(request.Model.SeName, category.Name, true, _seoSettings,- _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(category, request.Model.SeName, category.Name, true);category.SeName = request.Model.SeName;await _categoryService.UpdateCategory(category);//search engine namediff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCollectionCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCollectionCommandHandler.csindex 5f10cc235..fb5a620d1 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCollectionCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateCollectionCommandHandler.cs@@ -1,13 +1,9 @@using Grand.Api.Commands.Models.Catalog;using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Collections;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.Storage;-using Grand.Domain.Seo;-using Grand.Infrastructure;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;@@ -15,30 +11,19 @@ namespace Grand.Api.Commands.Handlers.Catalog;public class UpdateCollectionCommandHandler : IRequestHandler<UpdateCollectionCommand, CollectionDto>{private readonly ICollectionService _collectionService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public UpdateCollectionCommandHandler(ICollectionService collectionService,ISlugService slugService,- ILanguageService languageService,- ITranslationService translationService,IPictureService pictureService,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_collectionService = collectionService;_slugService = slugService;- _languageService = languageService;- _translationService = translationService;_pictureService = pictureService;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<CollectionDto> Handle(UpdateCollectionCommand request, CancellationToken cancellationToken)@@ -46,8 +31,7 @@ public class UpdateCollectionCommandHandler : IRequestHandler<UpdateCollectionCovar collection = await _collectionService.GetCollectionById(request.Model.Id);var prevPictureId = collection.PictureId;collection = request.Model.ToEntity(collection);- request.Model.SeName = await collection.ValidateSeName(request.Model.SeName, collection.Name, true,- _seoSettings, _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(collection, request.Model.SeName, collection.Name, true);collection.SeName = request.Model.SeName;await _collectionService.UpdateCollection(collection);//search engine namediff --git a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateProductCommandHandler.cs b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateProductCommandHandler.csindex f5e600cf7..8f22215ff 100644--- a/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateProductCommandHandler.cs+++ b/src/API/Grand.Api/Commands/Handlers/Catalog/UpdateProductCommandHandler.cs@@ -2,50 +2,35 @@using Grand.Api.DTOs.Catalog;using Grand.Api.Extensions;using Grand.Business.Core.Events.Catalog;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Products;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Domain.Catalog;-using Grand.Domain.Seo;-using Grand.Infrastructure;using MediatR;namespace Grand.Api.Commands.Handlers.Catalog;public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand, ProductDto>{- private readonly ILanguageService _languageService;private readonly IMediator _mediator;private readonly IOutOfStockSubscriptionService _outOfStockSubscriptionService;private readonly IProductService _productService;-- private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;private readonly IStockQuantityService _stockQuantityService;- private readonly ITranslationService _translationService;- private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public UpdateProductCommandHandler(IProductService productService,ISlugService slugService,- ITranslationService translationService,- ILanguageService languageService,IOutOfStockSubscriptionService outOfStockSubscriptionService,IStockQuantityService stockQuantityService,IMediator mediator,- IWorkContext workContext,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_productService = productService;_slugService = slugService;- _translationService = translationService;- _languageService = languageService;_outOfStockSubscriptionService = outOfStockSubscriptionService;_stockQuantityService = stockQuantityService;_mediator = mediator;- _workContext = workContext;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public async Task<ProductDto> Handle(UpdateProductCommand request, CancellationToken cancellationToken)@@ -56,8 +41,7 @@ public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand,var prevPublished = product.Published;product = request.Model.ToEntity(product);- request.Model.SeName = await product.ValidateSeName(request.Model.SeName, product.Name, true, _seoSettings,- _slugService, _languageService);+ request.Model.SeName = await _slugNameValidator.ValidateSeName(product, request.Model.SeName, product.Name, true);product.SeName = request.Model.SeName;//search engine nameawait _slugService.SaveSlug(product, request.Model.SeName, "");diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.csindex ae03c45b7..77c13963b 100644--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/BrandImportDataObject.cs@@ -1,14 +1,11 @@using Grand.Business.Catalog.Extensions;using Grand.Business.Core.Dto;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Brands;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.ExportImport;using Grand.Business.Core.Interfaces.Storage;using Grand.Domain.Catalog;using Grand.Domain.Media;-using Grand.Domain.Seo;using Grand.Infrastructure.Mapper;namespace Grand.Business.Catalog.Services.ExportImport;@@ -17,27 +14,22 @@ public class BrandImportDataObject : IImportDataObject<BrandDto>{private readonly IBrandLayoutService _brandLayoutService;private readonly IBrandService _brandService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSetting;private readonly ISlugService _slugService;-+ private readonly ISlugNameValidator _slugNameValidator;+public BrandImportDataObject(IBrandService brandService,IPictureService pictureService,IBrandLayoutService brandLayoutService,ISlugService slugService,- ILanguageService languageService,- SeoSettings seoSetting)+ ISlugNameValidator slugNameValidator){_brandService = brandService;_pictureService = pictureService;_brandLayoutService = brandLayoutService;_slugService = slugService;- _languageService = languageService;-- _seoSetting = seoSetting;+ _slugNameValidator = slugNameValidator;}public async Task Execute(IEnumerable<BrandDto> data)@@ -81,12 +73,12 @@ public class BrandImportDataObject : IImportDataObject<BrandDto>brand.PictureId = picture.Id;}- var sename = brand.SeName ?? brand.Name;- sename = await brand.ValidateSeName(sename, brand.Name, true, _seoSetting, _slugService, _languageService);- brand.SeName = sename;+ var seName = brand.SeName ?? brand.Name;+ seName = await _slugNameValidator.ValidateSeName(brand, seName, brand.Name, true);+ brand.SeName = seName;await _brandService.UpdateBrand(brand);- await _slugService.SaveSlug(brand, sename, "");+ await _slugService.SaveSlug(brand, seName, "");}private bool ValidBrand(Brand brand)diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.csindex 787eeacfa..d546cebb3 100644--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/CategoryImportDataObject.cs@@ -1,14 +1,11 @@using Grand.Business.Catalog.Extensions;using Grand.Business.Core.Dto;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Categories;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.ExportImport;using Grand.Business.Core.Interfaces.Storage;using Grand.Domain.Catalog;using Grand.Domain.Media;-using Grand.Domain.Seo;using Grand.Infrastructure.Mapper;namespace Grand.Business.Catalog.Services.ExportImport;@@ -17,27 +14,22 @@ public class CategoryImportDataObject : IImportDataObject<CategoryDto>{private readonly ICategoryLayoutService _categoryLayoutService;private readonly ICategoryService _categoryService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSetting;private readonly ISlugService _slugService;-+ private readonly ISlugNameValidator _slugNameValidator;+public CategoryImportDataObject(ICategoryService categoryService,IPictureService pictureService,ICategoryLayoutService categoryLayoutService,ISlugService slugService,- ILanguageService languageService,- SeoSettings seoSetting)+ ISlugNameValidator slugNameValidator){_categoryService = categoryService;_pictureService = pictureService;_categoryLayoutService = categoryLayoutService;_slugService = slugService;- _languageService = languageService;-- _seoSetting = seoSetting;+ _slugNameValidator = slugNameValidator;}public async Task Execute(IEnumerable<CategoryDto> data)@@ -87,12 +79,11 @@ public class CategoryImportDataObject : IImportDataObject<CategoryDto>category.PictureId = picture.Id;}- var sename = category.SeName ?? category.Name;- sename = await category.ValidateSeName(sename, category.Name, true, _seoSetting, _slugService,- _languageService);- category.SeName = sename;+ var seName = category.SeName ?? category.Name;+ seName = await _slugNameValidator.ValidateSeName(category, seName, category.Name, true);+ category.SeName = seName;await _categoryService.UpdateCategory(category);- await _slugService.SaveSlug(category, sename, "");+ await _slugService.SaveSlug(category, seName, "");}private bool ValidCategory(Category category)diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.csindex 93e46327a..1426e28c3 100644--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/CollectionImportDataObject.cs@@ -1,14 +1,11 @@using Grand.Business.Catalog.Extensions;using Grand.Business.Core.Dto;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Collections;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.ExportImport;using Grand.Business.Core.Interfaces.Storage;using Grand.Domain.Catalog;using Grand.Domain.Media;-using Grand.Domain.Seo;using Grand.Infrastructure.Mapper;namespace Grand.Business.Catalog.Services.ExportImport;@@ -17,27 +14,22 @@ public class CollectionImportDataObject : IImportDataObject<CollectionDto>{private readonly ICollectionLayoutService _collectionLayoutService;private readonly ICollectionService _collectionService;- private readonly ILanguageService _languageService;private readonly IPictureService _pictureService;-- private readonly SeoSettings _seoSetting;private readonly ISlugService _slugService;-+ private readonly ISlugNameValidator _slugNameValidator;public CollectionImportDataObject(ICollectionService collectionService,IPictureService pictureService,ICollectionLayoutService collectionLayoutService,ISlugService slugService,- ILanguageService languageService,- SeoSettings seoSetting)+ ISlugNameValidator slugNameValidator){_collectionService = collectionService;_pictureService = pictureService;_collectionLayoutService = collectionLayoutService;_slugService = slugService;- _languageService = languageService;- _seoSetting = seoSetting;+ _slugNameValidator = slugNameValidator;}public async Task Execute(IEnumerable<CollectionDto> data)@@ -84,8 +76,7 @@ public class CollectionImportDataObject : IImportDataObject<CollectionDto>}var sename = collection.SeName ?? collection.Name;- sename = await collection.ValidateSeName(sename, collection.Name, true, _seoSetting, _slugService,- _languageService);+ sename = await _slugNameValidator.ValidateSeName(collection, sename, collection.Name, true);collection.SeName = sename;await _collectionService.UpdateCollection(collection);diff --git a/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs b/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.csindex d87323f06..b4d419bba 100644--- a/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs+++ b/src/Business/Grand.Business.Catalog/Services/ExportImport/ProductImportDataObject.cs@@ -1,6 +1,5 @@using Grand.Business.Catalog.Extensions;using Grand.Business.Core.Dto;-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Brands;using Grand.Business.Core.Interfaces.Catalog.Categories;using Grand.Business.Core.Interfaces.Catalog.Collections;@@ -8,14 +7,12 @@ using Grand.Business.Core.Interfaces.Catalog.Directory;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Catalog.Tax;using Grand.Business.Core.Interfaces.Checkout.Shipping;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.ExportImport;using Grand.Business.Core.Interfaces.Storage;using Grand.Business.Core.Utilities.System;using Grand.Domain.Catalog;using Grand.Domain.Common;-using Grand.Domain.Seo;using Grand.Infrastructure.Mapper;namespace Grand.Business.Catalog.Services.ExportImport;@@ -26,19 +23,16 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>private readonly ICategoryService _categoryService;private readonly ICollectionService _collectionService;private readonly IDeliveryDateService _deliveryDateService;- private readonly ILanguageService _languageService;private readonly IMeasureService _measureService;private readonly IPictureService _pictureService;private readonly IProductCategoryService _productCategoryService;private readonly IProductCollectionService _productCollectionService;private readonly IProductLayoutService _productLayoutService;private readonly IProductService _productService;-- private readonly SeoSettings _seoSetting;private readonly ISlugService _slugService;private readonly ITaxCategoryService _taxService;private readonly IWarehouseService _warehouseService;-+ private readonly ISlugNameValidator _slugNameValidator;public ProductImportDataObject(IProductService productService,IPictureService pictureService,@@ -48,13 +42,12 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>IWarehouseService warehouseService,IMeasureService measureService,ISlugService slugService,- ILanguageService languageService,ICategoryService categoryService,IProductCategoryService productCategoryService,IBrandService brandService,ICollectionService collectionService,IProductCollectionService productCollectionService,- SeoSettings seoSetting)+ ISlugNameValidator slugNameValidator){_productService = productService;_pictureService = pictureService;@@ -64,13 +57,12 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>_warehouseService = warehouseService;_measureService = measureService;_slugService = slugService;- _languageService = languageService;_categoryService = categoryService;_productCategoryService = productCategoryService;_brandService = brandService;_collectionService = collectionService;_productCollectionService = productCollectionService;- _seoSetting = seoSetting;+ _slugNameValidator = slugNameValidator;}public async Task Execute(IEnumerable<ProductDto> data)@@ -106,10 +98,10 @@ public class ProductImportDataObject : IImportDataObject<ProductDto>await UpdateProductDataBrand(product);//search engine name- var sename = product.SeName ?? product.Name;- sename = await product.ValidateSeName(sename, product.Name, true, _seoSetting, _slugService, _languageService);- await _slugService.SaveSlug(product, sename, "");- product.SeName = sename;+ var seName = product.SeName ?? product.Name;+ seName = await _slugNameValidator.ValidateSeName(product, seName, product.Name, true);+ await _slugService.SaveSlug(product, seName, "");+ product.SeName = seName;await _productService.UpdateProduct(product);product.LowStock = product.MinStockQuantity > 0 && product.MinStockQuantity >= product.StockQuantity;diff --git a/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs b/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.csindex de203cfdd..b983c2cbd 100644--- a/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs+++ b/src/Business/Grand.Business.Catalog/Services/Products/CopyProductService.cs@@ -1,9 +1,6 @@-using Grand.Business.Core.Extensions;using Grand.Business.Core.Interfaces.Catalog.Products;-using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Domain.Catalog;-using Grand.Domain.Seo;namespace Grand.Business.Catalog.Services.Products;@@ -12,17 +9,23 @@ namespace Grand.Business.Catalog.Services.Products;/// </summary>public class CopyProductService : ICopyProductService{++ #region Fields++ private readonly IProductService _productService;+ private readonly ISlugService _slugService;+ private readonly ISlugNameValidator _slugNameValidator;++ #endregion#region Ctorpublic CopyProductService(IProductService productService,- ILanguageService languageService,ISlugService slugService,- SeoSettings seoSettings)+ ISlugNameValidator slugNameValidator){_productService = productService;- _languageService = languageService;_slugService = slugService;- _seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}#endregion@@ -184,8 +187,7 @@ public class CopyProductService : ICopyProductServiceawait _productService.InsertProduct(productCopy);//search engine name- var seName =- await productCopy.ValidateSeName("", productCopy.Name, true, _seoSettings, _slugService, _languageService);+ var seName = await _slugNameValidator.ValidateSeName(productCopy, "", productCopy.Name, true);productCopy.SeName = seName;await _productService.UpdateProduct(productCopy);await _slugService.SaveSlug(productCopy, seName, "");@@ -194,13 +196,4 @@ public class CopyProductService : ICopyProductService}#endregion-- #region Fields-- private readonly IProductService _productService;- private readonly ILanguageService _languageService;- private readonly ISlugService _slugService;- private readonly SeoSettings _seoSettings;-- #endregion}\ No newline at end of filediff --git a/src/Business/Grand.Business.Common/Services/Seo/SlugNameValidator.cs b/src/Business/Grand.Business.Common/Services/Seo/SlugNameValidator.csnew file mode 100644index 000000000..9b1d5d11c--- /dev/null+++ b/src/Business/Grand.Business.Common/Services/Seo/SlugNameValidator.cs@@ -0,0 +1,66 @@+using Grand.Business.Core.Extensions;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Domain;+using Grand.Domain.Seo;+using Grand.SharedKernel.Extensions;++namespace Grand.Business.Common.Services.Seo;++public class SlugNameValidator(ISlugService slugService, ILanguageService languageService, SeoSettings seoSettings) : ISlugNameValidator+{+ public async Task<string> ValidateSeName<T>(T entity, string seName, string name, bool ensureNotEmpty) where T : BaseEntity, ISlugEntity+ {+ ArgumentNullException.ThrowIfNull(entity);++ if (string.IsNullOrWhiteSpace(seName) && !string.IsNullOrWhiteSpace(name))+ seName = name;++ seName = GetSeName(seName);++ seName = CommonHelper.EnsureMaximumLength(seName, 200);++ if (string.IsNullOrWhiteSpace(seName))+ {+ if (ensureNotEmpty)+ seName = entity.Id;+ else+ return seName;+ }++ var entityName = typeof(T).Name;+ var i = 1;+ var tempSeName = seName;++ while (true)+ {+ var entityUrl = await slugService.GetBySlug(tempSeName);+ var reserved1 = entityUrl != null && !(entityUrl.EntityId == entity.Id &&+ entityUrl.EntityName.Equals(entityName,+ StringComparison.OrdinalIgnoreCase));++ var reserved2 = seoSettings.ReservedEntityUrlSlugs.Contains(tempSeName, StringComparer.OrdinalIgnoreCase);+ var reserved3 = (await languageService.GetAllLanguages(true)).Any(language =>+ language.UniqueSeoCode.Equals(tempSeName, StringComparison.OrdinalIgnoreCase));++ if (!reserved1 && !reserved2 && !reserved3)+ break;++ tempSeName = $"{seName}-{i}";+ i++;+ if (i <= 4) continue;+ tempSeName = $"{seName}-{Guid.NewGuid()}";+ break;+ }++ seName = tempSeName;++ return seName;+ }++ private string GetSeName(string name)+ {+ return SeoExtensions.GenerateSlug(name, seoSettings.ConvertNonWesternChars, seoSettings.AllowUnicodeCharsInUrls,+ seoSettings.AllowSlashChar, seoSettings.SeoCharConversion);+ }+}\ No newline at end of filediff --git a/src/Business/Grand.Business.Common/Startup/StartupApplication.cs b/src/Business/Grand.Business.Common/Startup/StartupApplication.csindex 6a884c43f..1a03790d2 100644--- a/src/Business/Grand.Business.Common/Startup/StartupApplication.cs+++ b/src/Business/Grand.Business.Common/Startup/StartupApplication.cs@@ -86,6 +86,7 @@ public class StartupApplication : IStartupApplicationprivate void RegisterSeoService(IServiceCollection serviceCollection){serviceCollection.AddScoped<ISlugService, SlugService>();+ serviceCollection.AddScoped<ISlugNameValidator, SlugNameValidator>();}diff --git a/src/Business/Grand.Business.Core/Extensions/SeoExtensions.cs b/src/Business/Grand.Business.Core/Extensions/SeoExtensions.csindex 1fc7935d8..0dfc67cce 100644--- a/src/Business/Grand.Business.Core/Extensions/SeoExtensions.cs+++ b/src/Business/Grand.Business.Core/Extensions/SeoExtensions.cs@@ -136,20 +136,13 @@ public static class SeoExtensionsreturn seName;}--- /// <summary>- /// Get SE name- /// </summary>- /// <param name="name">Name</param>- /// <param name="seoSettings">SeoSettings</param>- /// <returns>Result</returns>- public static string GetSeName(string name, SeoSettings seoSettings)++ private static string GetSeName(string name, SeoSettings seoSettings){- return GenerateSlug(name, seoSettings.ConvertNonWesternChars, seoSettings.AllowUnicodeCharsInUrls,+ return SeoExtensions.GenerateSlug(name, seoSettings.ConvertNonWesternChars, seoSettings.AllowUnicodeCharsInUrls,seoSettings.AllowSlashChar, seoSettings.SeoCharConversion);}-+/// <summary>/// Get SE name/// </summary>diff --git a/src/Business/Grand.Business.Core/Interfaces/Common/Seo/ISlugNameValidator.cs b/src/Business/Grand.Business.Core/Interfaces/Common/Seo/ISlugNameValidator.csnew file mode 100644index 000000000..2412aff72--- /dev/null+++ b/src/Business/Grand.Business.Core/Interfaces/Common/Seo/ISlugNameValidator.cs@@ -0,0 +1,9 @@+using Grand.Domain;+using Grand.Domain.Seo;++namespace Grand.Business.Core.Interfaces.Common.Seo;++public interface ISlugNameValidator+{+ Task<string> ValidateSeName<T>(T entity, string seName, string name, bool ensureNotEmpty) where T : BaseEntity, ISlugEntity;+}\ No newline at end of filediff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.csindex e094276c0..d7566dd92 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/BrandImportDataObjectTests.cs@@ -1,6 +1,7 @@using AutoMapper;using Grand.Business.Catalog.Services.Brands;using Grand.Business.Catalog.Services.ExportImport;+using Grand.Business.Common.Services.Seo;using Grand.Business.Core.Dto;using Grand.Business.Core.Interfaces.Catalog.Brands;using Grand.Business.Core.Interfaces.Common.Localization;@@ -40,7 +41,7 @@ public class BrandImportDataObjectTestsprivate IRepository<Brand> _repository;private Mock<ISlugService> _slugServiceMock;private Mock<IWorkContext> _workContextMock;-+ private ISlugNameValidator _slugNameValidator;[TestInitialize]public void Init(){@@ -61,9 +62,9 @@ public class BrandImportDataObjectTestsnew CacheConfig { DefaultCacheTimeMinutes = 1 });_brandService = new BrandService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object,new AccessControlConfig());-+ _slugNameValidator = new SlugNameValidator(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());_brandImportDataObject = new BrandImportDataObject(_brandService, _pictureServiceMock.Object,- _brandLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());+ _brandLayoutServiceMock.Object, _slugServiceMock.Object, _slugNameValidator);}[TestMethod]diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.csindex a7cef7ebf..f95842583 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CategoryImportDataObjectTests.cs@@ -2,6 +2,7 @@using Grand.Business.Catalog.Services.Categories;using Grand.Business.Catalog.Services.ExportImport;using Grand.Business.Common.Services.Security;+using Grand.Business.Common.Services.Seo;using Grand.Business.Core.Dto;using Grand.Business.Core.Interfaces.Catalog.Categories;using Grand.Business.Core.Interfaces.Common.Localization;@@ -41,7 +42,7 @@ public class CategoryImportDataObjectTestsprivate IRepository<Category> _repository;private Mock<ISlugService> _slugServiceMock;private Mock<IWorkContext> _workContextMock;-+ private ISlugNameValidator _slugNameValidator;[TestInitialize]public void Init(){@@ -62,9 +63,9 @@ public class CategoryImportDataObjectTestsnew CacheConfig { DefaultCacheTimeMinutes = 1 });_categoryService = new CategoryService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object,new AclService(new AccessControlConfig()), new AccessControlConfig());-+ _slugNameValidator = new SlugNameValidator(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());_categoryImportDataObject = new CategoryImportDataObject(_categoryService, _pictureServiceMock.Object,- _categoryLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());+ _categoryLayoutServiceMock.Object, _slugServiceMock.Object, _slugNameValidator);}[TestMethod]diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.csindex 5d71388a2..402be5135 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/CollectionImportDataObjectTests.cs@@ -2,6 +2,7 @@using Grand.Business.Catalog.Services.Collections;using Grand.Business.Catalog.Services.ExportImport;using Grand.Business.Common.Services.Security;+using Grand.Business.Common.Services.Seo;using Grand.Business.Core.Dto;using Grand.Business.Core.Interfaces.Catalog.Collections;using Grand.Business.Core.Interfaces.Common.Localization;@@ -41,7 +42,7 @@ public class CollectionImportDataObjectTestsprivate IRepository<Collection> _repository;private Mock<ISlugService> _slugServiceMock;private Mock<IWorkContext> _workContextMock;-+ private ISlugNameValidator _slugNameValidator;[TestInitialize]public void Init(){@@ -62,10 +63,9 @@ public class CollectionImportDataObjectTestsnew CacheConfig { DefaultCacheTimeMinutes = 1 });_collectionService = new CollectionService(_cacheBase, _repository, _workContextMock.Object,_mediatorMock.Object, new AclService(new AccessControlConfig()), new AccessControlConfig());-+ _slugNameValidator = new SlugNameValidator(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());_collectionImportDataObject = new CollectionImportDataObject(_collectionService, _pictureServiceMock.Object,- _collectionLayoutServiceMock.Object, _slugServiceMock.Object, _languageServiceMock.Object,- new SeoSettings());+ _collectionLayoutServiceMock.Object, _slugServiceMock.Object, _slugNameValidator);}[TestMethod]diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.csindex 120378b96..06f660020 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/ExportImport/ProductImportDataObjectTests.cs@@ -2,6 +2,7 @@using Grand.Business.Catalog.Services.ExportImport;using Grand.Business.Catalog.Services.Products;using Grand.Business.Common.Services.Security;+using Grand.Business.Common.Services.Seo;using Grand.Business.Core.Dto;using Grand.Business.Core.Interfaces.Catalog.Brands;using Grand.Business.Core.Interfaces.Catalog.Categories;@@ -59,7 +60,7 @@ public class ProductImportDataObjectTestsprivate Mock<ITaxCategoryService> _taxServiceMock;private Mock<IWarehouseService> _warehouseServiceMock;private Mock<IWorkContext> _workContextMock;-+ private ISlugNameValidator _slugNameValidator;[TestInitialize]public void Init(){@@ -90,15 +91,14 @@ public class ProductImportDataObjectTestsnew CacheConfig { DefaultCacheTimeMinutes = 1 });_productService = new ProductService(_cacheBase, _repository, _workContextMock.Object, _mediatorMock.Object,new AclService(new AccessControlConfig()));-+ _slugNameValidator = new SlugNameValidator(_slugServiceMock.Object, _languageServiceMock.Object, new SeoSettings());_productImportDataObject = new ProductImportDataObject(_productService, _pictureServiceMock.Object, _productLayoutServiceMock.Object, _deliveryDateServiceMock.Object,_taxServiceMock.Object, _warehouseServiceMock.Object, _measureServiceMock.Object, _slugServiceMock.Object,- _languageServiceMock.Object,_categoryServiceMock.Object, _productCategoryServiceMock.Object, _brandServiceMock.Object,_collectionServiceMock.Object,_productCollectionServiceMock.Object,- new SeoSettings());+ _slugNameValidator);}[TestMethod]diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/CopyProductServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/CopyProductServiceTests.csindex c4e37e1dc..46c17597e 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/CopyProductServiceTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/CopyProductServiceTests.cs@@ -1,4 +1,5 @@using Grand.Business.Catalog.Services.Products;+using Grand.Business.Common.Services.Seo;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;@@ -18,7 +19,8 @@ public class CopyProductServiceTestsprivate Mock<IProductService> _productServiceMock;private SeoSettings _settings;private Mock<ISlugService> _slugServiceMock;-+ private ISlugNameValidator _slugNameValidator;+[TestInitialize]public void Init(){@@ -26,8 +28,8 @@ public class CopyProductServiceTests_langServiceMock = new Mock<ILanguageService>();_slugServiceMock = new Mock<ISlugService>();_settings = new SeoSettings();- _copyProductService = new CopyProductService(_productServiceMock.Object, _langServiceMock.Object,- _slugServiceMock.Object, _settings);+ _slugNameValidator = new SlugNameValidator(_slugServiceMock.Object, _langServiceMock.Object, new SeoSettings());+ _copyProductService = new CopyProductService(_productServiceMock.Object, _slugServiceMock.Object, _slugNameValidator);}diff --git a/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugNameValidatorTests.cs b/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugNameValidatorTests.csnew file mode 100644index 000000000..a66262059--- /dev/null+++ b/src/Tests/Grand.Business.Common.Tests/Services/Seo/SlugNameValidatorTests.cs@@ -0,0 +1,172 @@+using Moq;+using NUnit.Framework;+using Grand.Business.Common.Services.Seo;+using Grand.Business.Core.Interfaces.Common.Localization;+using Grand.Business.Core.Interfaces.Common.Seo;+using Grand.Domain;+using Grand.Domain.Localization;+using Grand.Domain.Seo;+using NUnit.Framework.Legacy;++namespace Grand.Business.Common.Tests.Services.Seo+{+ public class SlugNameValidatorTests+ {+ private Mock<ISlugService> _mockSlugService;+ private Mock<ILanguageService> _mockLanguageService;+ private SeoSettings _seoSettings;+ private SlugNameValidator _slugNameValidator;++ [SetUp]+ public void Setup()+ {+ _mockSlugService = new Mock<ISlugService>();+ _mockLanguageService = new Mock<ILanguageService>();+ _mockLanguageService.Setup(l => l.GetAllLanguages(true, "")).ReturnsAsync(new List<Language>());+ _seoSettings = new SeoSettings {+ ReservedEntityUrlSlugs = new List<string> { "reserved-slug" },+ ConvertNonWesternChars = false,+ AllowUnicodeCharsInUrls = false,+ AllowSlashChar = false,+ SeoCharConversion = null+ };+ _slugNameValidator = new SlugNameValidator(_mockSlugService.Object, _mockLanguageService.Object, _seoSettings);+ }++ [Test]+ public async Task ValidateSeName_ShouldReturnName_WhenSeNameIsEmpty()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ string seName = "";+ string name = "test-name";+ bool ensureNotEmpty = false;++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, ensureNotEmpty);++ // Assert+ ClassicAssert.AreEqual("test-name", result);+ }++ [Test]+ public async Task ValidateSeName_ShouldReturnEntityId_WhenSeNameIsEmptyAndEnsureNotEmptyTrue()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ string seName = "";+ string name = "";+ bool ensureNotEmpty = true;++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, ensureNotEmpty);++ // Assert+ ClassicAssert.AreEqual("123", result);+ }++ [Test]+ public async Task ValidateSeName_ShouldAppendNumber_WhenSlugIsReserved()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ string seName = "reserved-slug";+ string name = "test-name";+ bool ensureNotEmpty = false;++ _mockSlugService.Setup(s => s.GetBySlug(It.IsAny<string>())).ReturnsAsync(new EntityUrl() { Slug = seName, EntityId = "123", EntityName = "TestEntity" });+ _mockLanguageService.Setup(l => l.GetAllLanguages(true, "")).ReturnsAsync(new List<Language>());++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, ensureNotEmpty);++ // Assert+ ClassicAssert.AreEqual("reserved-slug-1", result);+ }+ [Test]+ public async Task ValidateSeName_SeNameInLanguageSeoCode_ReturnsUniqueSlug()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ var name = "Entity Name";+ var seName = "en"; // Assuming this is a reserved SEO code in languages++ _mockSlugService.Setup(s => s.GetBySlug(It.IsAny<string>())).ReturnsAsync(new EntityUrl() { Slug = seName, EntityId = "123", EntityName = "TestEntity" });++ _mockLanguageService.Setup(l => l.GetAllLanguages(true, ""))+ .ReturnsAsync(new[] { new Language { UniqueSeoCode = "en" } });++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, true);++ // Assert+ ClassicAssert.IsTrue(result.StartsWith("en-"));+ }++ [Test]+ public async Task ValidateSeName_SeNameTooLong_TruncatesToMaxLength()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ var name = "Entity Name";+ var seName = new string('a', 250); // Longer than the max length of 200++ _mockSlugService.Setup(s => s.GetBySlug(It.IsAny<string>())).ReturnsAsync(new EntityUrl() { Slug = seName, EntityId = "123", EntityName = "TestEntity" });++ _mockLanguageService.Setup(l => l.GetAllLanguages(true, ""))+ .ReturnsAsync(new[] { new Language { UniqueSeoCode = "en" } });++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, true);++ // Assert+ ClassicAssert.AreEqual(seName.Substring(0, 200), result);+ }+ [Test]+ public async Task ValidateSeName_NoReservedSlug_ReturnsOriginalSlug()+ {+ // Arrange+ var entity = new TestEntity { Id = "123" };+ var name = "Entity Name";+ var seName = "unique-slug";++ _mockSlugService.Setup(s => s.GetBySlug(It.IsAny<string>())).ReturnsAsync(new EntityUrl() { Slug = seName, EntityId = "123", EntityName = "TestEntity" });++ _mockLanguageService.Setup(l => l.GetAllLanguages(true, ""))+ .ReturnsAsync(new[] { new Language { UniqueSeoCode = "en" } });++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, true);++ // Assert+ ClassicAssert.AreEqual("unique-slug", result);+ }+ [Test]+ public async Task ValidateSeName_ReservedSlug_OverLimit_ReturnsSlugWithGuid()+ {+ // Arrange+ var entity = new TestEntity { Id = "1" };+ var name = "Entity Name";+ var seName = "reserved-slug";++ _mockSlugService.SetupSequence(s => s.GetBySlug(It.IsAny<string>()))+ .ReturnsAsync(new EntityUrl { EntityId = "2", EntityName = "OtherEntity" })+ .ReturnsAsync(new EntityUrl { EntityId = "2", EntityName = "OtherEntity" })+ .ReturnsAsync(new EntityUrl { EntityId = "2", EntityName = "OtherEntity" })+ .ReturnsAsync(new EntityUrl { EntityId = "2", EntityName = "OtherEntity" })+ .ReturnsAsync(new EntityUrl { EntityId = "2", EntityName = "OtherEntity" });++ // Act+ var result = await _slugNameValidator.ValidateSeName(entity, seName, name, true);++ // Assert+ ClassicAssert.IsTrue(result.StartsWith("reserved-slug-") && result.Length > "reserved-slug-4".Length);+ }++ class TestEntity : BaseEntity, ISlugEntity+ {+ // Implement necessary members of ISlugEntity if needed+ public string SeName { get; set; }+ }+ }+}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Services/BlogViewModelService.cs b/src/Web/Grand.Web.Admin/Services/BlogViewModelService.csindex 1522f67de..bae1701c0 100644--- a/src/Web/Grand.Web.Admin/Services/BlogViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/BlogViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Catalog.Products;+using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Cms;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;@@ -36,13 +35,14 @@ public class BlogViewModelService : IBlogViewModelServiceprivate readonly ITranslationService _translationService;private readonly IVendorService _vendorService;private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;+public BlogViewModelService(IBlogService blogService, IDateTimeService dateTimeService, IStoreService storeService,ISlugService slugService,IPictureService pictureService, ICustomerService customerService, ITranslationService translationService,IProductService productService,IVendorService vendorService, ILanguageService languageService, IWorkContext workContext,- SeoSettings seoSettings)+ SeoSettings seoSettings, ISlugNameValidator slugNameValidator){_blogService = blogService;_dateTimeService = dateTimeService;@@ -56,6 +56,7 @@ public class BlogViewModelService : IBlogViewModelService_languageService = languageService;_workContext = workContext;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public virtual async Task<(IEnumerable<BlogPostModel> blogPosts, int totalCount)> PrepareBlogPostsModel(@@ -83,8 +84,7 @@ public class BlogViewModelService : IBlogViewModelServiceawait _blogService.InsertBlogPost(blogPost);//search engine name- var seName = await blogPost.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService,- _languageService);+ var seName = await _slugNameValidator.ValidateSeName(blogPost, model.SeName, model.Title, true);blogPost.SeName = seName;blogPost.Locales =await model.Locales.ToTranslationProperty(blogPost, x => x.Title, _seoSettings, _slugService,@@ -105,8 +105,7 @@ public class BlogViewModelService : IBlogViewModelServiceawait _blogService.UpdateBlogPost(blogPost);//search engine name- var seName = await blogPost.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService,- _languageService);+ var seName = await _slugNameValidator.ValidateSeName(blogPost, model.SeName, model.Title, true);blogPost.SeName = seName;blogPost.Locales =await model.Locales.ToTranslationProperty(blogPost, x => x.Title, _seoSettings, _slugService,diff --git a/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs b/src/Web/Grand.Web.Admin/Services/BrandViewModelService.csindex 385d4944b..1354ec19b 100644--- a/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/BrandViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Catalog.Brands;+using Grand.Business.Core.Interfaces.Catalog.Brands;using Grand.Business.Core.Interfaces.Catalog.Discounts;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;@@ -20,6 +19,22 @@ namespace Grand.Web.Admin.Services;public class BrandViewModelService : IBrandViewModelService{++ #region Fields++ private readonly IBrandService _brandService;+ private readonly IBrandLayoutService _brandLayoutService;+ private readonly ISlugService _slugService;+ private readonly IPictureService _pictureService;+ private readonly IDiscountService _discountService;+ private readonly IDateTimeService _dateTimeService;+ private readonly ILanguageService _languageService;+ private readonly IWorkContext _workContext;+ private readonly SeoSettings _seoSettings;+ private readonly ISlugNameValidator _slugNameValidator;++ #endregion+#region Constructorspublic BrandViewModelService(@@ -31,7 +46,8 @@ public class BrandViewModelService : IBrandViewModelServiceIDateTimeService dateTimeService,ILanguageService languageService,IWorkContext workContext,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_brandLayoutService = brandLayoutService;_brandService = brandService;@@ -42,6 +58,7 @@ public class BrandViewModelService : IBrandViewModelService_languageService = languageService;_workContext = workContext;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}#endregion@@ -92,8 +109,7 @@ public class BrandViewModelService : IBrandViewModelService//search engine namebrand.Locales =await model.Locales.ToTranslationProperty(brand, x => x.Name, _seoSettings, _slugService, _languageService);- model.SeName = await brand.ValidateSeName(model.SeName, brand.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(brand, model.SeName, brand.Name, true);brand.SeName = model.SeName;await _brandService.UpdateBrand(brand);@@ -127,8 +143,7 @@ public class BrandViewModelService : IBrandViewModelServicebrand.AppliedDiscounts.Remove(discount.Id);}- model.SeName = await brand.ValidateSeName(model.SeName, brand.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(brand, model.SeName, brand.Name, true);brand.SeName = model.SeName;await _brandService.UpdateBrand(brand);@@ -153,18 +168,4 @@ public class BrandViewModelService : IBrandViewModelService{await _brandService.DeleteBrand(brand);}-- #region Fields-- private readonly IBrandService _brandService;- private readonly IBrandLayoutService _brandLayoutService;- private readonly ISlugService _slugService;- private readonly IPictureService _pictureService;- private readonly IDiscountService _discountService;- private readonly IDateTimeService _dateTimeService;- private readonly ILanguageService _languageService;- private readonly IWorkContext _workContext;- private readonly SeoSettings _seoSettings;-- #endregion}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs b/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.csindex 5ced723bb..a21d5ef4e 100644--- a/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/CategoryViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Catalog.Categories;+using Grand.Business.Core.Interfaces.Catalog.Categories;using Grand.Business.Core.Interfaces.Catalog.Discounts;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;@@ -34,7 +33,8 @@ public class CategoryViewModelService : ICategoryViewModelServiceprivate readonly IStoreService _storeService;private readonly ITranslationService _translationService;private readonly IVendorService _vendorService;-+ private readonly ISlugNameValidator _slugNameValidator;+public CategoryViewModelService(ICategoryService categoryService,IProductCategoryService productCategoryService,@@ -48,7 +48,8 @@ public class CategoryViewModelService : ICategoryViewModelServiceIVendorService vendorService,ILanguageService languageService,CatalogSettings catalogSettings,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_categoryService = categoryService;_productCategoryService = productCategoryService;@@ -63,6 +64,7 @@ public class CategoryViewModelService : ICategoryViewModelService_languageService = languageService;_catalogSettings = catalogSettings;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public virtual async Task<CategoryListModel> PrepareCategoryListModel(string storeId)@@ -141,8 +143,7 @@ public class CategoryViewModelService : ICategoryViewModelServicecategory.Locales =await model.Locales.ToTranslationProperty(category, x => x.Name, _seoSettings, _slugService,_languageService);- model.SeName = await category.ValidateSeName(model.SeName, category.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(category, model.SeName, category.Name, true);category.SeName = model.SeName;await _categoryService.UpdateCategory(category);@@ -158,8 +159,7 @@ public class CategoryViewModelService : ICategoryViewModelService{var prevPictureId = category.PictureId;category = model.ToEntity(category);- model.SeName = await category.ValidateSeName(model.SeName, category.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(category, model.SeName, category.Name, true);category.SeName = model.SeName;//localescategory.Locales =diff --git a/src/Web/Grand.Web.Admin/Services/CollectionViewModelService.cs b/src/Web/Grand.Web.Admin/Services/CollectionViewModelService.csindex 017395e10..c00f94676 100644--- a/src/Web/Grand.Web.Admin/Services/CollectionViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/CollectionViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Catalog.Collections;+using Grand.Business.Core.Interfaces.Catalog.Collections;using Grand.Business.Core.Interfaces.Catalog.Discounts;using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;@@ -22,6 +21,26 @@ namespace Grand.Web.Admin.Services;public class CollectionViewModelService : ICollectionViewModelService{++ #region Fields++ private readonly ICollectionService _collectionService;+ private readonly IProductCollectionService _productCollectionService;+ private readonly ICollectionLayoutService _collectionLayoutService;+ private readonly IProductService _productService;+ private readonly IStoreService _storeService;+ private readonly ISlugService _slugService;+ private readonly IPictureService _pictureService;+ private readonly ITranslationService _translationService;+ private readonly IDiscountService _discountService;+ private readonly IVendorService _vendorService;+ private readonly ILanguageService _languageService;+ private readonly IWorkContext _workContext;+ private readonly SeoSettings _seoSettings;+ private readonly ISlugNameValidator _slugNameValidator;++ #endregion+#region Constructorspublic CollectionViewModelService(@@ -37,7 +56,8 @@ public class CollectionViewModelService : ICollectionViewModelServiceIVendorService vendorService,ILanguageService languageService,IWorkContext workContext,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_collectionLayoutService = collectionLayoutService;_collectionService = collectionService;@@ -52,6 +72,7 @@ public class CollectionViewModelService : ICollectionViewModelService_languageService = languageService;_workContext = workContext;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}#endregion@@ -103,8 +124,7 @@ public class CollectionViewModelService : ICollectionViewModelServicecollection.Locales =await model.Locales.ToTranslationProperty(collection, x => x.Name, _seoSettings, _slugService,_languageService);- model.SeName = await collection.ValidateSeName(model.SeName, collection.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(collection, model.SeName, collection.Name, true);collection.SeName = model.SeName;await _collectionService.UpdateCollection(collection);@@ -139,8 +159,7 @@ public class CollectionViewModelService : ICollectionViewModelServicecollection.AppliedDiscounts.Remove(discount.Id);}- model.SeName = await collection.ValidateSeName(model.SeName, collection.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(collection, model.SeName, collection.Name, true);collection.SeName = model.SeName;await _collectionService.UpdateCollection(collection);@@ -272,22 +291,4 @@ public class CollectionViewModelService : ICollectionViewModelService}, product.Id);}}-- #region Fields-- private readonly ICollectionService _collectionService;- private readonly IProductCollectionService _productCollectionService;- private readonly ICollectionLayoutService _collectionLayoutService;- private readonly IProductService _productService;- private readonly IStoreService _storeService;- private readonly ISlugService _slugService;- private readonly IPictureService _pictureService;- private readonly ITranslationService _translationService;- private readonly IDiscountService _discountService;- private readonly IVendorService _vendorService;- private readonly ILanguageService _languageService;- private readonly IWorkContext _workContext;- private readonly SeoSettings _seoSettings;-- #endregion}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Services/CourseViewModelService.cs b/src/Web/Grand.Web.Admin/Services/CourseViewModelService.csindex 6fb8981ce..aca7e0f19 100644--- a/src/Web/Grand.Web.Admin/Services/CourseViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/CourseViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Catalog.Products;+using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Business.Core.Interfaces.Common.Stores;@@ -33,7 +32,8 @@ public class CourseViewModelService(IProductService productService,IStoreService storeService,IVendorService vendorService,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator): ICourseViewModelService{public virtual async Task<CourseModel> PrepareCourseModel(CourseModel model = null)@@ -58,8 +58,7 @@ public class CourseViewModelService(await courseService.Insert(course);//locales- model.SeName =- await course.ValidateSeName(model.SeName, course.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(course, model.SeName, course.Name, true);course.SeName = model.SeName;await courseService.Update(course);@@ -81,8 +80,7 @@ public class CourseViewModelService(var prevProductId = course.ProductId;course = model.ToEntity(course);- model.SeName =- await course.ValidateSeName(model.SeName, course.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(course, model.SeName, course.Name, true);course.SeName = model.SeName;//localescourse.Locales =diff --git a/src/Web/Grand.Web.Admin/Services/KnowledgebaseViewModelService.cs b/src/Web/Grand.Web.Admin/Services/KnowledgebaseViewModelService.csindex 99f5ae239..e4708c290 100644--- a/src/Web/Grand.Web.Admin/Services/KnowledgebaseViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/KnowledgebaseViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Cms;+using Grand.Business.Core.Interfaces.Cms;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;using Grand.Domain.Knowledgebase;@@ -17,7 +16,7 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelService{private readonly IKnowledgebaseService _knowledgebaseService;private readonly ILanguageService _languageService;-+ private readonly ISlugNameValidator _slugNameValidator;private readonly SeoSettings _seoSettings;private readonly ISlugService _slugService;@@ -25,12 +24,14 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelServiceIKnowledgebaseService knowledgebaseService,ISlugService slugService,ILanguageService languageService,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_knowledgebaseService = knowledgebaseService;_slugService = slugService;_languageService = languageService;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public virtual async Task PrepareCategory(KnowledgebaseCategoryModel model)@@ -112,8 +113,7 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelServicevar knowledgebaseCategory = model.ToEntity();knowledgebaseCategory.Locales = await model.Locales.ToTranslationProperty(knowledgebaseCategory, x => x.Name,_seoSettings, _slugService, _languageService);- model.SeName = await knowledgebaseCategory.ValidateSeName(model.SeName, knowledgebaseCategory.Name, true,- _seoSettings, _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(knowledgebaseCategory, model.SeName, knowledgebaseCategory.Name, true);knowledgebaseCategory.SeName = model.SeName;await _knowledgebaseService.InsertKnowledgebaseCategory(knowledgebaseCategory);await _slugService.SaveSlug(knowledgebaseCategory, model.SeName, "");@@ -127,8 +127,7 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelServiceknowledgebaseCategory = model.ToEntity(knowledgebaseCategory);knowledgebaseCategory.Locales = await model.Locales.ToTranslationProperty(knowledgebaseCategory, x => x.Name,_seoSettings, _slugService, _languageService);- model.SeName = await knowledgebaseCategory.ValidateSeName(model.SeName, knowledgebaseCategory.Name, true,- _seoSettings, _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(knowledgebaseCategory, model.SeName, knowledgebaseCategory.Name, true);knowledgebaseCategory.SeName = model.SeName;await _knowledgebaseService.UpdateKnowledgebaseCategory(knowledgebaseCategory);await _slugService.SaveSlug(knowledgebaseCategory, model.SeName, "");@@ -156,8 +155,7 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelServicevar knowledgebaseArticle = model.ToEntity();knowledgebaseArticle.Locales = await model.Locales.ToTranslationProperty(knowledgebaseArticle, x => x.Name,_seoSettings, _slugService, _languageService);- model.SeName = await knowledgebaseArticle.ValidateSeName(model.SeName, knowledgebaseArticle.Name, true,- _seoSettings, _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(knowledgebaseArticle, model.SeName, knowledgebaseArticle.Name, true);knowledgebaseArticle.SeName = model.SeName;knowledgebaseArticle.AllowComments = model.AllowComments;await _knowledgebaseService.InsertKnowledgebaseArticle(knowledgebaseArticle);@@ -172,8 +170,7 @@ public class KnowledgebaseViewModelService : IKnowledgebaseViewModelServiceknowledgebaseArticle = model.ToEntity(knowledgebaseArticle);knowledgebaseArticle.Locales = await model.Locales.ToTranslationProperty(knowledgebaseArticle, x => x.Name,_seoSettings, _slugService, _languageService);- model.SeName = await knowledgebaseArticle.ValidateSeName(model.SeName, knowledgebaseArticle.Name, true,- _seoSettings, _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(knowledgebaseArticle, model.SeName, knowledgebaseArticle.Name, true);knowledgebaseArticle.SeName = model.SeName;knowledgebaseArticle.AllowComments = model.AllowComments;await _knowledgebaseService.UpdateKnowledgebaseArticle(knowledgebaseArticle);diff --git a/src/Web/Grand.Web.Admin/Services/NewsViewModelService.cs b/src/Web/Grand.Web.Admin/Services/NewsViewModelService.csindex de5754e1d..651c7365b 100644--- a/src/Web/Grand.Web.Admin/Services/NewsViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/NewsViewModelService.cs@@ -18,6 +18,20 @@ namespace Grand.Web.Admin.Services;public class NewsViewModelService : INewsViewModelService{+ #region Fields++ private readonly INewsService _newsService;+ private readonly IDateTimeService _dateTimeService;+ private readonly ITranslationService _translationService;+ private readonly ISlugService _slugService;+ private readonly IPictureService _pictureService;+ private readonly ILanguageService _languageService;+ private readonly ICustomerService _customerService;+ private readonly SeoSettings _seoSettings;+ private readonly ISlugNameValidator _slugNameValidator;++ #endregion+#region Constructorspublic NewsViewModelService(INewsService newsService,@@ -27,7 +41,8 @@ public class NewsViewModelService : INewsViewModelServiceIPictureService pictureService,ILanguageService languageService,ICustomerService customerService,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_newsService = newsService;_dateTimeService = dateTimeService;@@ -37,6 +52,7 @@ public class NewsViewModelService : INewsViewModelService_languageService = languageService;_customerService = customerService;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}#endregion@@ -60,8 +76,7 @@ public class NewsViewModelService : INewsViewModelServicevar newsItem = model.ToEntity(_dateTimeService);await _newsService.InsertNews(newsItem);- var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService,- _languageService);+ var seName = await _slugNameValidator.ValidateSeName(newsItem, model.SeName, model.Title, true);newsItem.SeName = seName;newsItem.Locales =await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _seoSettings, _slugService,@@ -79,8 +94,7 @@ public class NewsViewModelService : INewsViewModelService{var prevPictureId = newsItem.PictureId;newsItem = model.ToEntity(newsItem, _dateTimeService);- var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService,- _languageService);+ var seName = await _slugNameValidator.ValidateSeName(newsItem, model.SeName, model.Title, true);newsItem.SeName = seName;newsItem.Locales =await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _seoSettings, _slugService,@@ -152,18 +166,5 @@ public class NewsViewModelService : INewsViewModelService//update totalsnewsItem.CommentCount = newsItem.NewsComments.Count;await _newsService.UpdateNews(newsItem);- }-- #region Fields-- private readonly INewsService _newsService;- private readonly IDateTimeService _dateTimeService;- private readonly ITranslationService _translationService;- private readonly ISlugService _slugService;- private readonly IPictureService _pictureService;- private readonly ILanguageService _languageService;- private readonly ICustomerService _customerService;- private readonly SeoSettings _seoSettings;-- #endregion+ }}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Services/PageViewModelService.cs b/src/Web/Grand.Web.Admin/Services/PageViewModelService.csindex 4639ecd8c..b058a44d6 100644--- a/src/Web/Grand.Web.Admin/Services/PageViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/PageViewModelService.cs@@ -1,5 +1,4 @@-using Grand.Business.Core.Extensions;-using Grand.Business.Core.Interfaces.Cms;+using Grand.Business.Core.Interfaces.Cms;using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Common.Seo;@@ -24,7 +23,8 @@ public class PageViewModelService : IPageViewModelServiceprivate readonly ISlugService _slugService;private readonly IStoreService _storeService;private readonly ITranslationService _translationService;-+ private readonly ISlugNameValidator _slugNameValidator;+public PageViewModelService(IPageLayoutService pageLayoutService,IPageService pageService,@@ -33,7 +33,8 @@ public class PageViewModelService : IPageViewModelServiceIStoreService storeService,ILanguageService languageService,IDateTimeService dateTimeService,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_pageLayoutService = pageLayoutService;_pageService = pageService;@@ -43,6 +44,7 @@ public class PageViewModelService : IPageViewModelService_languageService = languageService;_dateTimeService = dateTimeService;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public virtual async Task<PageListModel> PreparePageListModel()@@ -75,8 +77,7 @@ public class PageViewModelService : IPageViewModelServicevar page = model.ToEntity(_dateTimeService);await _pageService.InsertPage(page);//search engine name- model.SeName = await page.ValidateSeName(model.SeName, page.Title ?? page.SystemName, true, _seoSettings,- _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(page, model.SeName, page.Title ?? page.SystemName, true);page.Locales =await model.Locales.ToTranslationProperty(page, x => x.Title, _seoSettings, _slugService, _languageService);page.SeName = model.SeName;@@ -91,8 +92,7 @@ public class PageViewModelService : IPageViewModelServicepage = model.ToEntity(page, _dateTimeService);page.Locales =await model.Locales.ToTranslationProperty(page, x => x.Title, _seoSettings, _slugService, _languageService);- model.SeName = await page.ValidateSeName(model.SeName, page.Title ?? page.SystemName, true, _seoSettings,- _slugService, _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(page, model.SeName, page.Title ?? page.SystemName, true);page.SeName = model.SeName;await _pageService.UpdatePage(page);diff --git a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.csindex 7de0277e2..9e219feb3 100644--- a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs@@ -70,7 +70,8 @@ public class ProductViewModelService(TaxSettings taxSettings,SeoSettings seoSettings,IAuctionService auctionService,- IPriceFormatter priceFormatter)+ IPriceFormatter priceFormatter,+ ISlugNameValidator slugNameValidator): IProductViewModelService{public virtual async Task PrepareAddProductAttributeCombinationModel(ProductAttributeCombinationModel model,@@ -698,8 +699,7 @@ public class ProductViewModelService(var product = model.ToEntity(dateTimeService);await productService.InsertProduct(product);- model.SeName =- await product.ValidateSeName(model.SeName, product.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(product, model.SeName, product.Name, true);product.SeName = model.SeName;product.Locales =await model.Locales.ToTranslationProperty(product, x => x.Name, seoSettings, slugService, languageService);@@ -743,8 +743,7 @@ public class ProductViewModelService(//productproduct = model.ToEntity(product, dateTimeService);product.AutoAddRequiredProducts = model.AutoAddRequiredProducts;- model.SeName =- await product.ValidateSeName(model.SeName, product.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(product, model.SeName, product.Name, true);product.SeName = model.SeName;product.Locales =await model.Locales.ToTranslationProperty(product, x => x.Name, seoSettings, slugService, languageService);diff --git a/src/Web/Grand.Web.Admin/Services/VendorViewModelService.cs b/src/Web/Grand.Web.Admin/Services/VendorViewModelService.csindex 3e5e5ca8c..04cdb2d41 100644--- a/src/Web/Grand.Web.Admin/Services/VendorViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/VendorViewModelService.cs@@ -36,7 +36,8 @@ public class VendorViewModelService(IMediator mediator,VendorSettings vendorSettings,ILanguageService languageService,- SeoSettings seoSettings)+ SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator): IVendorViewModelService{public virtual async Task PrepareDiscountModel(VendorModel model, Vendor vendor, bool excludeProperties)@@ -185,8 +186,7 @@ public class VendorViewModelService(vendor.AppliedDiscounts.Add(discount.Id);//search engine name- model.SeName =- await vendor.ValidateSeName(model.SeName, vendor.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(vendor, model.SeName, vendor.Name, true);vendor.Locales =await model.Locales.ToTranslationProperty(vendor, x => x.Name, seoSettings, slugService, languageService);vendor.SeName = model.SeName;@@ -205,8 +205,7 @@ public class VendorViewModelService(vendor = model.ToEntity(vendor);vendor.Locales =await model.Locales.ToTranslationProperty(vendor, x => x.Name, seoSettings, slugService, languageService);- model.SeName =- await vendor.ValidateSeName(model.SeName, vendor.Name, true, seoSettings, slugService, languageService);+ model.SeName = await slugNameValidator.ValidateSeName(vendor, model.SeName, vendor.Name, true);vendor.Address = model.Address.ToEntity(vendor.Address);//discountsdiff --git a/src/Web/Grand.Web.Vendor/Controllers/VendorInfoController.cs b/src/Web/Grand.Web.Vendor/Controllers/VendorInfoController.csindex b3b6f9cdc..afaf68205 100644--- a/src/Web/Grand.Web.Vendor/Controllers/VendorInfoController.cs+++ b/src/Web/Grand.Web.Vendor/Controllers/VendorInfoController.cs@@ -28,7 +28,8 @@ public class VendorInfoController : BaseVendorControllerICountryService countryService,ISlugService slugService,SeoSettings seoSettings,- VendorSettings vendorSettings)+ VendorSettings vendorSettings,+ ISlugNameValidator slugNameValidator){_translationService = translationService;_vendorService = vendorService;@@ -38,6 +39,7 @@ public class VendorInfoController : BaseVendorController_slugService = slugService;_seoSettings = seoSettings;_vendorSettings = vendorSettings;+ _slugNameValidator = slugNameValidator;}#endregion@@ -50,7 +52,7 @@ public class VendorInfoController : BaseVendorControllerprivate readonly ILanguageService _languageService;private readonly ICountryService _countryService;private readonly ISlugService _slugService;-+ private readonly ISlugNameValidator _slugNameValidator;private readonly SeoSettings _seoSettings;private readonly VendorSettings _vendorSettings;@@ -108,8 +110,7 @@ public class VendorInfoController : BaseVendorControllervendor.Locales =await model.Locales.ToTranslationProperty(vendor, x => x.Name, _seoSettings, _slugService,_languageService);- model.SeName = await vendor.ValidateSeName(model.SeName, vendor.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(vendor, model.SeName, vendor.Name, true);vendor.Address = model.Address.ToEntity();vendor.SeName = model.SeName;diff --git a/src/Web/Grand.Web.Vendor/Services/ProductViewModelService.cs b/src/Web/Grand.Web.Vendor/Services/ProductViewModelService.csindex d44c75e79..ada7bfc40 100644--- a/src/Web/Grand.Web.Vendor/Services/ProductViewModelService.cs+++ b/src/Web/Grand.Web.Vendor/Services/ProductViewModelService.cs@@ -64,7 +64,7 @@ public class ProductViewModelService : IProductViewModelServiceprivate readonly ITranslationService _translationService;private readonly IWarehouseService _warehouseService;private readonly IWorkContext _workContext;-+ private readonly ISlugNameValidator _slugNameValidator;public ProductViewModelService(IProductService productService,IInventoryManageService inventoryManageService,@@ -95,7 +95,8 @@ public class ProductViewModelService : IProductViewModelServiceIPriceFormatter priceFormatter,CurrencySettings currencySettings,MeasureSettings measureSettings,- TaxSettings taxSettings, SeoSettings seoSettings)+ TaxSettings taxSettings, SeoSettings seoSettings,+ ISlugNameValidator slugNameValidator){_productService = productService;_inventoryManageService = inventoryManageService;@@ -128,6 +129,7 @@ public class ProductViewModelService : IProductViewModelService_measureSettings = measureSettings;_taxSettings = taxSettings;_seoSettings = seoSettings;+ _slugNameValidator = slugNameValidator;}public virtual async Task PrepareAddProductAttributeCombinationModel(ProductAttributeCombinationModel model,@@ -690,8 +692,7 @@ public class ProductViewModelService : IProductViewModelServiceproduct.VendorId = _workContext.CurrentVendor!.Id;await _productService.InsertProduct(product);- model.SeName = await product.ValidateSeName(model.SeName, product.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(product, model.SeName, product.Name, true);product.SeName = model.SeName;product.Locales = await model.Locales.ToTranslationProperty(product, x => x.Name,_seoSettings, _slugService, _languageService);@@ -717,8 +718,7 @@ public class ProductViewModelService : IProductViewModelService//productproduct = model.ToEntity(product, _dateTimeService);product.AutoAddRequiredProducts = model.AutoAddRequiredProducts;- model.SeName = await product.ValidateSeName(model.SeName, product.Name, true, _seoSettings, _slugService,- _languageService);+ model.SeName = await _slugNameValidator.ValidateSeName(product, model.SeName, product.Name, true);product.SeName = model.SeName;product.Locales = await model.Locales.ToTranslationProperty(product, x => x.Name, _seoSettings,_slugService, _languageService);diff --git a/src/Web/Grand.Web/Controllers/VendorController.cs b/src/Web/Grand.Web/Controllers/VendorController.csindex a8cfe2ea9..a5b4deb32 100644--- a/src/Web/Grand.Web/Controllers/VendorController.cs+++ b/src/Web/Grand.Web/Controllers/VendorController.cs@@ -132,7 +132,7 @@ public class VendorController : BasePublicController[AutoValidateAntiforgeryToken][DenySystemAccount][CustomerGroupAuthorize(SystemCustomerGroupNames.Registered)]- public virtual async Task<IActionResult> ApplyVendorSubmit(ApplyVendorModel model)+ public virtual async Task<IActionResult> ApplyVendorSubmit(ApplyVendorModel model, [FromServices] ISlugNameValidator slugNameValidator){if (!_vendorSettings.AllowCustomersToApplyForVendorAccount)return RedirectToRoute("HomePage");@@ -155,10 +155,7 @@ public class VendorController : BasePublicControllerawait _vendorService.InsertVendor(vendor);//search engine name (the same as vendor name)- var seName = await vendor.ValidateSeName(vendor.Name, vendor.Name, true,- HttpContext.RequestServices.GetRequiredService<SeoSettings>(),- HttpContext.RequestServices.GetRequiredService<ISlugService>(),- HttpContext.RequestServices.GetRequiredService<ILanguageService>());+ var seName = await slugNameValidator.ValidateSeName(vendor, vendor.Name, vendor.Name, true);await _slugService.SaveSlug(vendor, seName, "");vendor.SeName = seName;