Skip to content

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.

Add pagination to the page all vendor

supporte1a37f07b471
diff --git a/src/Web/Grand.Web/Controllers/CatalogController.cs b/src/Web/Grand.Web/Controllers/CatalogController.cs
index 197b80b09..66500381d 100644
--- a/src/Web/Grand.Web/Controllers/CatalogController.cs
+++ b/src/Web/Grand.Web/Controllers/CatalogController.cs
@@ -317,13 +317,13 @@ namespace Grand.Web.Controllers
return View(model);
}
[HttpGet]
- public virtual async Task<IActionResult> VendorAll()
+ public virtual async Task<IActionResult> VendorAll(VendorPagingModel command)
{
//we don't allow viewing of vendors if "vendors" block is hidden
if (_vendorSettings.VendorsBlockItemsToDisplay == 0)
return RedirectToRoute("HomePage");
- var model = await _mediator.Send(new GetVendorAll { Language = _workContext.WorkingLanguage });
+ var model = await _mediator.Send(new GetVendorAll { Language = _workContext.WorkingLanguage, Command = command });
return View(model);
}
diff --git a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
index 4f2ccb849..0595b0ba4 100644
--- a/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
+++ b/src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
@@ -95,12 +95,6 @@ namespace Grand.Web.Events.Cache
public const string VENDOR_NAVIGATION_MODEL_KEY = "Grand.pres.vendor.navigation";
public const string VENDOR_NAVIGATION_PATTERN_KEY = "Grand.pres.vendor.navigation";
- /// <summary>
- /// Key for List of VendorModel caching
- /// </summary>
- public const string VENDOR_ALL_MODEL_KEY = "Grand.pres.vendor.navigation.all";
-
-
/// <summary>
/// Key for CategoryNavigationModel caching
/// </summary>
diff --git a/src/Web/Grand.Web/Features/Handlers/Catalog/GetVendorAllHandler.cs b/src/Web/Grand.Web/Features/Handlers/Catalog/GetVendorAllHandler.cs
index e4c41aeaa..4abca42c3 100644
--- a/src/Web/Grand.Web/Features/Handlers/Catalog/GetVendorAllHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Catalog/GetVendorAllHandler.cs
@@ -2,10 +2,9 @@
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Interfaces.Storage;
+using Grand.Domain.Catalog;
using Grand.Domain.Media;
using Grand.Domain.Vendors;
-using Grand.Infrastructure.Caching;
-using Grand.Web.Events.Cache;
using Grand.Web.Features.Models.Catalog;
using Grand.Web.Features.Models.Common;
using Grand.Web.Models.Catalog;
@@ -14,21 +13,20 @@ using MediatR;
namespace Grand.Web.Features.Handlers.Catalog
{
- public class GetVendorAllHandler : IRequestHandler<GetVendorAll, IList<VendorModel>>
+ public class GetVendorAllHandler : IRequestHandler<GetVendorAll, VendorListModel>
{
private readonly IVendorService _vendorService;
private readonly IPictureService _pictureService;
private readonly ITranslationService _translationService;
- private readonly ICacheBase _cacheBase;
private readonly IMediator _mediator;
private readonly MediaSettings _mediaSettings;
private readonly VendorSettings _vendorSettings;
-
+ private readonly CatalogSettings _catalogSettings;
public GetVendorAllHandler(
IVendorService vendorService,
IPictureService pictureService,
ITranslationService translationService,
- ICacheBase cacheBase,
+ CatalogSettings catalogSettings,
IMediator mediator,
MediaSettings mediaSettings,
VendorSettings vendorSettings)
@@ -36,57 +34,76 @@ namespace Grand.Web.Features.Handlers.Catalog
_vendorService = vendorService;
_pictureService = pictureService;
_translationService = translationService;
- _cacheBase = cacheBase;
+ _catalogSettings = catalogSettings;
_mediator = mediator;
_mediaSettings = mediaSettings;
_vendorSettings = vendorSettings;
}
- public async Task<IList<VendorModel>> Handle(GetVendorAll request, CancellationToken cancellationToken)
+ public async Task<VendorListModel> Handle(GetVendorAll request, CancellationToken cancellationToken)
{
- return await _cacheBase.GetAsync(CacheKeyConst.VENDOR_ALL_MODEL_KEY, () => PrepareVendorAll(request));
+ var model = new VendorListModel();
+ model.VendorsModel = await PrepareVendors(request, model);
+ return model;
}
- private async Task<List<VendorModel>> PrepareVendorAll(GetVendorAll request)
+ private async Task<List<VendorModel>> PrepareVendors(GetVendorAll request, VendorListModel vendorListModel)
{
+ if (request.Command.PageNumber <= 0) request.Command.PageNumber = 1;
+ if (request.Command.PageSize == 0 || request.Command.PageSize > _catalogSettings.MaxCatalogPageSize)
+ {
+ request.Command.PageSize = _catalogSettings.MaxCatalogPageSize;
+ }
+
var model = new List<VendorModel>();
- var vendors = await _vendorService.GetAllVendors();
+ var vendors = await _vendorService.GetAllVendors(
+ pageIndex: request.Command.PageNumber - 1,
+ pageSize: request.Command.PageSize);
+
+ vendorListModel.PagingModel.LoadPagedList(vendors);
+
foreach (var vendor in vendors)
{
- var vendorModel = new VendorModel
- {
- Id = vendor.Id,
- Name = vendor.GetTranslation(x => x.Name, request.Language.Id),
- Description = vendor.GetTranslation(x => x.Description, request.Language.Id),
- MetaKeywords = vendor.GetTranslation(x => x.MetaKeywords, request.Language.Id),
- MetaDescription = vendor.GetTranslation(x => x.MetaDescription, request.Language.Id),
- MetaTitle = vendor.GetTranslation(x => x.MetaTitle, request.Language.Id),
- SeName = vendor.GetSeName(request.Language.Id),
- AllowCustomersToContactVendors = _vendorSettings.AllowCustomersToContactVendors,
- UserFields = vendor.UserFields,
- //prepare vendor address
- Address = await _mediator.Send(new GetVendorAddress {
- Language = request.Language,
- Address = vendor.Address,
- ExcludeProperties = false
- })
- };
-
- //prepare picture model
- var pictureModel = new PictureModel
- {
- Id = vendor.PictureId,
- FullSizeImageUrl = await _pictureService.GetPictureUrl(vendor.PictureId),
- ImageUrl = await _pictureService.GetPictureUrl(vendor.PictureId, _mediaSettings.VendorThumbPictureSize),
- Title = string.Format(_translationService.GetResource("Media.Vendor.ImageLinkTitleFormat"), vendorModel.Name),
- AlternateText = string.Format(_translationService.GetResource("Media.Vendor.ImageAlternateTextFormat"), vendorModel.Name)
- };
- vendorModel.PictureModel = pictureModel;
- model.Add(vendorModel);
+ model.Add(await BuildVendor(vendor, request));
}
return model;
}
+
+ private async Task<VendorModel> BuildVendor(Vendor vendor, GetVendorAll request)
+ {
+ var model = new VendorModel
+ {
+ Id = vendor.Id,
+ Name = vendor.GetTranslation(x => x.Name, request.Language.Id),
+ Description = vendor.GetTranslation(x => x.Description, request.Language.Id),
+ MetaKeywords = vendor.GetTranslation(x => x.MetaKeywords, request.Language.Id),
+ MetaDescription = vendor.GetTranslation(x => x.MetaDescription, request.Language.Id),
+ MetaTitle = vendor.GetTranslation(x => x.MetaTitle, request.Language.Id),
+ SeName = vendor.GetSeName(request.Language.Id),
+ AllowCustomersToContactVendors = _vendorSettings.AllowCustomersToContactVendors,
+ UserFields = vendor.UserFields,
+ //prepare vendor address
+ Address = await _mediator.Send(new GetVendorAddress {
+ Language = request.Language,
+ Address = vendor.Address,
+ ExcludeProperties = false
+ })
+ };
+
+ //prepare picture model
+ var pictureModel = new PictureModel
+ {
+ Id = vendor.PictureId,
+ FullSizeImageUrl = await _pictureService.GetPictureUrl(vendor.PictureId),
+ ImageUrl = await _pictureService.GetPictureUrl(vendor.PictureId, _mediaSettings.VendorThumbPictureSize),
+ Title = string.Format(_translationService.GetResource("Media.Vendor.ImageLinkTitleFormat"), model.Name),
+ AlternateText = string.Format(_translationService.GetResource("Media.Vendor.ImageAlternateTextFormat"), model.Name)
+ };
+ model.PictureModel = pictureModel;
+
+ return model;
+ }
}
}
diff --git a/src/Web/Grand.Web/Features/Models/Catalog/GetVendorAll.cs b/src/Web/Grand.Web/Features/Models/Catalog/GetVendorAll.cs
index b48ccd7a7..f9f24d0a5 100644
--- a/src/Web/Grand.Web/Features/Models/Catalog/GetVendorAll.cs
+++ b/src/Web/Grand.Web/Features/Models/Catalog/GetVendorAll.cs
@@ -4,8 +4,9 @@ using MediatR;
namespace Grand.Web.Features.Models.Catalog
{
- public class GetVendorAll : IRequest<IList<VendorModel>>
+ public class GetVendorAll : IRequest<VendorListModel>
{
public Language Language { get; set; }
+ public VendorPagingModel Command { get; set; }
}
}
diff --git a/src/Web/Grand.Web/Models/Catalog/VendorListModel.cs b/src/Web/Grand.Web/Models/Catalog/VendorListModel.cs
new file mode 100644
index 000000000..e8f046db7
--- /dev/null
+++ b/src/Web/Grand.Web/Models/Catalog/VendorListModel.cs
@@ -0,0 +1,12 @@
+namespace Grand.Web.Models.Catalog;
+
+public class VendorListModel
+{
+ public VendorListModel()
+ {
+ PagingModel = new VendorPagingModel();
+ VendorsModel = new List<VendorModel>();
+ }
+ public VendorPagingModel PagingModel { get; set; }
+ public IList<VendorModel> VendorsModel { get; set; }
+}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Models/Catalog/VendorPagingModel.cs b/src/Web/Grand.Web/Models/Catalog/VendorPagingModel.cs
new file mode 100644
index 000000000..5c87a2d1b
--- /dev/null
+++ b/src/Web/Grand.Web/Models/Catalog/VendorPagingModel.cs
@@ -0,0 +1,8 @@
+using Grand.Web.Common.Page.Paging;
+
+namespace Grand.Web.Models.Catalog;
+
+public class VendorPagingModel: BasePageableModel
+{
+
+}
\ No newline at end of file
diff --git a/src/Web/Grand.Web/Views/Catalog/VendorAll.cshtml b/src/Web/Grand.Web/Views/Catalog/VendorAll.cshtml
index 389f31cd5..208839a06 100644
--- a/src/Web/Grand.Web/Views/Catalog/VendorAll.cshtml
+++ b/src/Web/Grand.Web/Views/Catalog/VendorAll.cshtml
@@ -1,4 +1,4 @@
-@model IList<VendorModel>
+@model VendorListModel
@inject IPageHeadBuilder pagebuilder
@{
Layout = "_TwoColumns";
@@ -11,7 +11,7 @@
<h1 class="generalTitle h2">@Loc["Vendors.List"]</h1>
<div class="vendor-grid">
<div class="form-row flex-grid">
- @foreach (var item in Model)
+ @foreach (var item in Model.VendorsModel)
{
<div class="col-md-4 col-6">
<a class="mb-2" href="@Url.RouteUrl("Vendor", new { item.SeName })" title="@item.PictureModel.Title">
@@ -27,5 +27,9 @@
</div>
}
</div>
+ @if (Model.VendorsModel.Any())
+ {
+ <page-navigation asp-query-param="pagenumber" asp-pagination="Model.PagingModel"/>
+ }
</div>
</div>
\ No newline at end of file