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 pagination to the page all brands / add new catalog settings MaxBrandPageSize
support0b21d85e0557
diff --git a/src/Core/Grand.Domain/Catalog/CatalogSettings.cs b/src/Core/Grand.Domain/Catalog/CatalogSettings.csindex 0d7804f3d..bb7abbe98 100644--- a/src/Core/Grand.Domain/Catalog/CatalogSettings.cs+++ b/src/Core/Grand.Domain/Catalog/CatalogSettings.cs@@ -409,5 +409,11 @@ namespace Grand.Domain.Catalog/// Gets or sets a sorting by availability/// </summary>public bool SortingByAvailability { get; set; }++ /// <summary>+ /// Gets or sets the max value for page size to use for pagination on all brands page+ /// </summary>+ public int MaxBrandPageSize { get; set; } = 10;+}}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Catalog.TabGeneralSettings.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Catalog.TabGeneralSettings.cshtmlindex f7380f984..5f1e78ca7 100644--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Catalog.TabGeneralSettings.cshtml+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/Partials/Catalog.TabGeneralSettings.cshtml@@ -677,7 +677,16 @@</div><div class="form-group"><div class="col-6 col-md-6 col-sm-6">- <admin-label asp-for="SortingByAvailability" class="control-label text-left" />+ <admin-label asp-for="MaxBrandPageSize" class="control-label" />+ </div>+ <div class="col-6 col-md-6 col-sm-6">+ <admin-input asp-for="MaxBrandPageSize" />+ <span asp-validation-for="MaxBrandPageSize"></span>+ </div>+ </div>+ <div class="form-group">+ <div class="col-6 col-md-6 col-sm-6">+ <admin-label asp-for="SortingByAvailability" class="control-label text-left" /></div><div class="col-6 col-md-6 col-sm-6"><label class="mt-checkbox mt-checkbox-outline control control-checkbox">diff --git a/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs b/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.csindex 97c82f07c..18c24f952 100644--- a/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs+++ b/src/Web/Grand.Web.Admin/Models/Settings/CatalogSettingsModel.cs@@ -240,6 +240,9 @@ namespace Grand.Web.Admin.Models.Settings[GrandResourceDisplayName("Admin.Settings.Catalog.DefaultCollectionPageSize")]public int DefaultCollectionPageSize { get; set; }+ [GrandResourceDisplayName("Admin.Settings.Catalog.MaxBrandPageSize")]+ public int MaxBrandPageSize { get; set; }+[GrandResourceDisplayName("Admin.Settings.Catalog.ShowProductsFromSubcategoriesInSearchBox")]public bool ShowProductsFromSubcategoriesInSearchBox { get; set; }diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xmlindex 3cb5228b8..7da873e33 100644--- a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml+++ b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml@@ -10038,6 +10038,9 @@<Resource Name="Admin.Settings.Catalog.LimitOfFeaturedProducts" Area="Admin"><Value>Limit Of Featured Products</Value></Resource>+ <Resource Name="Admin.Settings.Catalog.MaxBrandPageSize" Area="Admin">+ <Value>The max number of brands that can be displayed on the all brands page (page size)</Value>+ </Resource><Resource Name="Admin.Settings.Catalog.NewProductsEnabled" Area="Admin"><Value>'New products' page enabled</Value></Resource>diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xmlindex 9d300141f..748cb7a48 100644--- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml+++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml@@ -54,4 +54,7 @@<Resource Name="Admin.Settings.Vendor.AllowToUploadFile" Area="Admin"><Value>Allow vendor to upload picture file</Value></Resource>+ <Resource Name="Admin.Settings.Catalog.MaxBrandPageSize" Area="Admin">+ <Value>The max number of brands that can be displayed on the all brands page (page size)</Value>+ </Resource></Language>\ No newline at end of filediff --git a/src/Web/Grand.Web/Controllers/CatalogController.cs b/src/Web/Grand.Web/Controllers/CatalogController.csindex 2cdafde34..4bc254ef0 100644--- a/src/Web/Grand.Web/Controllers/CatalogController.cs+++ b/src/Web/Grand.Web/Controllers/CatalogController.cs@@ -206,12 +206,13 @@ namespace Grand.Web.Controllersreturn View(layoutViewPath, model);}[HttpGet]- public virtual async Task<IActionResult> BrandAll()+ public virtual async Task<IActionResult> BrandAll(BrandPagingModel command){var model = await _mediator.Send(new GetBrandAll {Customer = _workContext.CurrentCustomer,Language = _workContext.WorkingLanguage,- Store = _workContext.CurrentStore+ Store = _workContext.CurrentStore,+ Command = command});return View(model);}diff --git a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.csindex 2528c63dd..4f2ccb849 100644--- a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs+++ b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs@@ -12,16 +12,6 @@ namespace Grand.Web.Events.Cache/// {2} : current store ID/// </remarks>public const string SEARCH_CATEGORIES_MODEL_KEY = "Grand.category-{0}-{1}-{2}.pres.search";-- /// <summary>- /// Key for List of BrandModel caching- /// </summary>- /// <remarks>- /// {0} : language id- /// {1} : comma separated list of customer groups- /// {2} : current store ID- /// </remarks>- public const string BRAND_ALL_MODEL_KEY = "Grand.brand.navigation.all-{0}-{1}-{2}.pres";/// <summary>/// Key for caching of brand displayed on home page/// </summary>diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.csindex e265b2326..6b33ee7d8 100644--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs@@ -2,10 +2,8 @@using Grand.Business.Core.Interfaces.Catalog.Brands;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Storage;-using Grand.Domain.Customers;+using Grand.Domain.Catalog;using Grand.Domain.Media;-using Grand.Infrastructure.Caching;-using Grand.Web.Events.Cache;using Grand.Web.Extensions;using Grand.Web.Features.Models.Catalog;using Grand.Web.Models.Catalog;@@ -14,66 +12,90 @@ using MediatR;namespace Grand.Web.Features.Handlers.Catalog{- public class GetBrandAllHandler : IRequestHandler<GetBrandAll, IList<BrandModel>>+ public class GetBrandAllHandler : IRequestHandler<GetBrandAll, BrandListModel>{private readonly IBrandService _brandService;private readonly IPictureService _pictureService;private readonly ITranslationService _translationService;- private readonly ICacheBase _cacheBase;private readonly MediaSettings _mediaSettings;+ private readonly CatalogSettings _catalogSettings;public GetBrandAllHandler(IBrandService brandService,IPictureService pictureService,ITranslationService translationService,- ICacheBase cacheBase,- MediaSettings mediaSettings)+ MediaSettings mediaSettings,+ CatalogSettings catalogSettings){_brandService = brandService;_pictureService = pictureService;_translationService = translationService;- _cacheBase = cacheBase;_mediaSettings = mediaSettings;+ _catalogSettings = catalogSettings;}- public async Task<IList<BrandModel>> Handle(GetBrandAll request, CancellationToken cancellationToken)+ public async Task<BrandListModel> Handle(GetBrandAll request, CancellationToken cancellationToken){- var cacheKey = string.Format(CacheKeyConst.BRAND_ALL_MODEL_KEY,- request.Language.Id,- string.Join(",", request.Customer.GetCustomerGroupIds()),- request.Store.Id);- return await _cacheBase.GetAsync(cacheKey, () => PrepareBrandAll(request));+ var model = new BrandListModel();+ model.BrandsModel = await PrepareBrands(request, model);+ return model;}- private async Task<List<BrandModel>> PrepareBrandAll(GetBrandAll request)+ private async Task<List<BrandModel>> PrepareBrands(GetBrandAll request, BrandListModel brandListModel){+ request.Command.PageSize = 10;+ if (request.Command.PageNumber <= 0) request.Command.PageNumber = 1;+ if (request.Command.PageSize == 0 || request.Command.PageSize > _catalogSettings.MaxBrandPageSize)+ {+ request.Command.PageSize = _catalogSettings.MaxBrandPageSize;+ }+var model = new List<BrandModel>();- var brands = await _brandService.GetAllBrands(storeId: request.Store.Id);+ var brands = await _brandService.GetAllBrands(storeId: request.Store.Id,+ pageIndex: request.Command.PageNumber - 1,+ pageSize: request.Command.PageSize+ );++ brandListModel.PagingModel.LoadPagedList(brands);+foreach (var brand in brands){- var modelBrand = brand.ToModel(request.Language);+ model.Add(await BuildBrand(brand, request));+ }- //prepare picture model- var picture = !string.IsNullOrEmpty(brand.PictureId) ? await _pictureService.GetPictureById(brand.PictureId) : null;- modelBrand.PictureModel = new PictureModel- {- Id = brand.PictureId,- FullSizeImageUrl = await _pictureService.GetPictureUrl(brand.PictureId),- ImageUrl = await _pictureService.GetPictureUrl(brand.PictureId, _mediaSettings.BrandThumbPictureSize),- Style = picture?.Style,- ExtraField = picture?.ExtraField,- //"title" attribute- Title = picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, request.Language.Id)) ?- picture.GetTranslation(x => x.TitleAttribute, request.Language.Id) :- string.Format(_translationService.GetResource("Media.Brand.ImageLinkTitleFormat"), brand.Name),- //"alt" attribute- AlternateText = picture != null && !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, request.Language.Id)) ?- picture.GetTranslation(x => x.AltAttribute, request.Language.Id) :- string.Format(_translationService.GetResource("Media.Brand.ImageAlternateTextFormat"), brand.Name)- };+ return model;+ }++ private async Task<BrandModel> BuildBrand(Brand brand, GetBrandAll request)+ {+ var model = brand.ToModel(request.Language);++ //prepare picture model+ var picture = !string.IsNullOrEmpty(brand.PictureId)+ ? await _pictureService.GetPictureById(brand.PictureId)+ : null;+ model.PictureModel = new PictureModel {+ Id = brand.PictureId,+ FullSizeImageUrl = await _pictureService.GetPictureUrl(brand.PictureId),+ ImageUrl = await _pictureService.GetPictureUrl(brand.PictureId, _mediaSettings.BrandThumbPictureSize),+ Style = picture?.Style,+ ExtraField = picture?.ExtraField,+ //"title" attribute+ Title =+ picture != null &&+ !string.IsNullOrEmpty(picture.GetTranslation(x => x.TitleAttribute, request.Language.Id))+ ? picture.GetTranslation(x => x.TitleAttribute, request.Language.Id)+ : string.Format(_translationService.GetResource("Media.Brand.ImageLinkTitleFormat"),+ brand.Name),+ //"alt" attribute+ AlternateText =+ picture != null &&+ !string.IsNullOrEmpty(picture.GetTranslation(x => x.AltAttribute, request.Language.Id))+ ? picture.GetTranslation(x => x.AltAttribute, request.Language.Id)+ : string.Format(_translationService.GetResource("Media.Brand.ImageAlternateTextFormat"),+ brand.Name)+ };- model.Add(modelBrand);- }return model;}}-}+}\ No newline at end of filediff --git a/src/Web/Grand.Web/Features/Models/Catalog/GetBrandAll.cs b/src/Web/Grand.Web/Features/Models/Catalog/GetBrandAll.csindex 79f553745..5b34abc23 100644--- a/src/Web/Grand.Web/Features/Models/Catalog/GetBrandAll.cs+++ b/src/Web/Grand.Web/Features/Models/Catalog/GetBrandAll.cs@@ -6,10 +6,11 @@ using MediatR;namespace Grand.Web.Features.Models.Catalog{- public class GetBrandAll : IRequest<IList<BrandModel>>+ public class GetBrandAll : IRequest<BrandListModel>{public Store Store { get; set; }public Customer Customer { get; set; }public Language Language { get; set; }+ public BrandPagingModel Command { get; set; }}}diff --git a/src/Web/Grand.Web/Models/Catalog/BrandListModel.cs b/src/Web/Grand.Web/Models/Catalog/BrandListModel.csnew file mode 100644index 000000000..5bc1f518c--- /dev/null+++ b/src/Web/Grand.Web/Models/Catalog/BrandListModel.cs@@ -0,0 +1,12 @@+namespace Grand.Web.Models.Catalog;++public class BrandListModel+{+ public BrandListModel()+ {+ PagingModel = new BrandPagingModel();+ BrandsModel = new List<BrandModel>();+ }+ public BrandPagingModel PagingModel { get; set; }+ public IList<BrandModel> BrandsModel { get; set; }+}\ No newline at end of filediff --git a/src/Web/Grand.Web/Models/Catalog/BrandPagingModel.cs b/src/Web/Grand.Web/Models/Catalog/BrandPagingModel.csnew file mode 100644index 000000000..735fc3797--- /dev/null+++ b/src/Web/Grand.Web/Models/Catalog/BrandPagingModel.cs@@ -0,0 +1,8 @@+using Grand.Web.Common.Page.Paging;++namespace Grand.Web.Models.Catalog;++public class BrandPagingModel: BasePageableModel+{++}\ No newline at end of filediff --git a/src/Web/Grand.Web/Views/Catalog/BrandAll.cshtml b/src/Web/Grand.Web/Views/Catalog/BrandAll.cshtmlindex c5ea2dbe4..2cda89cbe 100644--- a/src/Web/Grand.Web/Views/Catalog/BrandAll.cshtml+++ b/src/Web/Grand.Web/Views/Catalog/BrandAll.cshtml@@ -1,4 +1,4 @@-@model IList<BrandModel>+@model BrandListModel@inject IPageHeadBuilder pagebuilder@{Layout = "_TwoColumns";@@ -10,7 +10,7 @@<h1 class="generalTitle h2">@Loc["Brands.List"]</h1><div class="collectionGrid"><div class="row flex-grid">- @foreach (var item in Model)+ @foreach (var item in Model.BrandsModel){<div class="col-md-4 col-6 mb-2"><a class="mb-2" href="@Url.RouteUrl("Brand", new { item.SeName })" title="@item.PictureModel.Title">@@ -26,6 +26,7 @@</div>}</div>+ <page-navigation asp-query-param="pagenumber" asp-pagination="Model.PagingModel" /></div></div>