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 collection
support3deea76174a2
diff --git a/src/Web/Grand.Web/Controllers/CatalogController.cs b/src/Web/Grand.Web/Controllers/CatalogController.csindex 4bc254ef0..197b80b09 100644--- a/src/Web/Grand.Web/Controllers/CatalogController.cs+++ b/src/Web/Grand.Web/Controllers/CatalogController.cs@@ -270,12 +270,13 @@ namespace Grand.Web.Controllersreturn View(layoutViewPath, model);}[HttpGet]- public virtual async Task<IActionResult> CollectionAll()+ public virtual async Task<IActionResult> CollectionAll(CollectionPagingModel command){var model = await _mediator.Send(new GetCollectionAll {Customer = _workContext.CurrentCustomer,Language = _workContext.WorkingLanguage,- Store = _workContext.CurrentStore+ Store = _workContext.CurrentStore,+ Command = command});return View(model);}diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.csindex 03c970600..9d8d34b4d 100644--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetCollectionAllHandler.cs@@ -2,6 +2,7 @@using Grand.Business.Core.Interfaces.Catalog.Collections;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Business.Core.Interfaces.Storage;+using Grand.Domain.Catalog;using Grand.Domain.Customers;using Grand.Domain.Media;using Grand.Infrastructure.Caching;@@ -14,65 +15,82 @@ using MediatR;namespace Grand.Web.Features.Handlers.Catalog{- public class GetCollectionAllHandler : IRequestHandler<GetCollectionAll, IList<CollectionModel>>+ public class GetCollectionAllHandler : IRequestHandler<GetCollectionAll, CollectionListModel>{private readonly ICollectionService _collectionService;private readonly IPictureService _pictureService;private readonly ITranslationService _translationService;- private readonly ICacheBase _cacheBase;+ private readonly CatalogSettings _catalogSettings;private readonly MediaSettings _mediaSettings;public GetCollectionAllHandler(ICollectionService collectionService,IPictureService pictureService,ITranslationService translationService,- ICacheBase cacheBase,+ CatalogSettings catalogSettings,MediaSettings mediaSettings){_collectionService = collectionService;_pictureService = pictureService;_translationService = translationService;- _cacheBase = cacheBase;+ _catalogSettings = catalogSettings;_mediaSettings = mediaSettings;}- public async Task<IList<CollectionModel>> Handle(GetCollectionAll request, CancellationToken cancellationToken)+ public async Task<CollectionListModel> Handle(GetCollectionAll request, CancellationToken cancellationToken){var cacheKey = string.Format(CacheKeyConst.COLLECTION_ALL_MODEL_KEY,request.Language.Id,string.Join(",", request.Customer.GetCustomerGroupIds()),request.Store.Id);- return await _cacheBase.GetAsync(cacheKey, () => PrepareCollectionAll(request));++ var model = new CollectionListModel();+ model.CollectionModel = await PrepareCollectionAll(request, model);+ return model;}- private async Task<List<CollectionModel>> PrepareCollectionAll(GetCollectionAll request)+ private async Task<List<CollectionModel>> PrepareCollectionAll(GetCollectionAll request, CollectionListModel collectionListModel){+ 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<CollectionModel>();- var collections = await _collectionService.GetAllCollections(storeId: request.Store.Id);+ var collections = await _collectionService.GetAllCollections(storeId: request.Store.Id,+ pageIndex: request.Command.PageNumber - 1,+ pageSize: request.Command.PageSize);++ collectionListModel.PagingModel.LoadPagedList(collections);foreach (var collection in collections){- var modelcollection = collection.ToModel(request.Language);+ model.Add(await BuildCollection(collection, request));+ }+ return model;+ }- //prepare picture model- var picture = !string.IsNullOrEmpty(collection.PictureId) ? await _pictureService.GetPictureById(collection.PictureId) : null;- modelcollection.PictureModel = new PictureModel- {- Id = collection.PictureId,- FullSizeImageUrl = await _pictureService.GetPictureUrl(collection.PictureId),- ImageUrl = await _pictureService.GetPictureUrl(collection.PictureId, _mediaSettings.CollectionThumbPictureSize),- 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.Collection.ImageLinkTitleFormat"), modelcollection.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.Collection.ImageAlternateTextFormat"), modelcollection.Name)- };+ private async Task<CollectionModel> BuildCollection(Collection collection, GetCollectionAll request)+ {+ var model = collection.ToModel(request.Language);- model.Add(modelcollection);- }+ //prepare picture model+ var picture = !string.IsNullOrEmpty(collection.PictureId) ? await _pictureService.GetPictureById(collection.PictureId) : null;+ model.PictureModel = new PictureModel+ {+ Id = collection.PictureId,+ FullSizeImageUrl = await _pictureService.GetPictureUrl(collection.PictureId),+ ImageUrl = await _pictureService.GetPictureUrl(collection.PictureId, _mediaSettings.CollectionThumbPictureSize),+ 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.Collection.ImageLinkTitleFormat"), model.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.Collection.ImageAlternateTextFormat"), model.Name)+ };return model;}}diff --git a/src/Web/Grand.Web/Features/Models/Catalog/GetCollectionAll.cs b/src/Web/Grand.Web/Features/Models/Catalog/GetCollectionAll.csindex 3fd10babd..ce0d7ad55 100644--- a/src/Web/Grand.Web/Features/Models/Catalog/GetCollectionAll.cs+++ b/src/Web/Grand.Web/Features/Models/Catalog/GetCollectionAll.cs@@ -6,10 +6,11 @@ using MediatR;namespace Grand.Web.Features.Models.Catalog{- public class GetCollectionAll : IRequest<IList<CollectionModel>>+ public class GetCollectionAll : IRequest<CollectionListModel>{public Store Store { get; set; }public Customer Customer { get; set; }public Language Language { get; set; }+ public CollectionPagingModel Command { get; set; }}}diff --git a/src/Web/Grand.Web/Models/Catalog/CollectionListModel.cs b/src/Web/Grand.Web/Models/Catalog/CollectionListModel.csnew file mode 100644index 000000000..f688abffa--- /dev/null+++ b/src/Web/Grand.Web/Models/Catalog/CollectionListModel.cs@@ -0,0 +1,12 @@+namespace Grand.Web.Models.Catalog;++public class CollectionListModel+{+ public CollectionListModel()+ {+ PagingModel = new CollectionPagingModel();+ CollectionModel = new List<CollectionModel>();+ }+ public CollectionPagingModel PagingModel { get; set; }+ public IList<CollectionModel> CollectionModel { get; set; }+}\ No newline at end of filediff --git a/src/Web/Grand.Web/Models/Catalog/CollectionPagingModel.cs b/src/Web/Grand.Web/Models/Catalog/CollectionPagingModel.csnew file mode 100644index 000000000..55e658041--- /dev/null+++ b/src/Web/Grand.Web/Models/Catalog/CollectionPagingModel.cs@@ -0,0 +1,8 @@+using Grand.Web.Common.Page.Paging;++namespace Grand.Web.Models.Catalog;++public class CollectionPagingModel: BasePageableModel+{++}\ No newline at end of filediff --git a/src/Web/Grand.Web/Views/Catalog/CollectionAll.cshtml b/src/Web/Grand.Web/Views/Catalog/CollectionAll.cshtmlindex 27264b5b1..989c607b1 100644--- a/src/Web/Grand.Web/Views/Catalog/CollectionAll.cshtml+++ b/src/Web/Grand.Web/Views/Catalog/CollectionAll.cshtml@@ -1,4 +1,4 @@-@model IList<CollectionModel>+@model CollectionListModel@inject IPageHeadBuilder pagebuilder@{Layout = "_TwoColumns";@@ -10,7 +10,7 @@<h1 class="generalTitle h2">@Loc["Collections.List"]</h1><div class="collectionGrid"><div class="form-row flex-grid">- @foreach (var item in Model)+ @foreach (var item in Model.CollectionModel){<div class="col-md-4 col-6 mb-2"><a class="mb-2" href="@Url.RouteUrl("Collection", new { item.SeName })" title="@item.PictureModel.Title">@@ -26,6 +26,10 @@</div>}</div>+ @if (Model.CollectionModel.Any())+ {+ <page-navigation asp-query-param="pagenumber" asp-pagination="Model.PagingModel"/>+ }</div></div>