Skip to content

Commit

Permalink
Add pagination to the page all brands / add new catalog settings MaxB…
Browse files Browse the repository at this point in the history
…randPageSize
  • Loading branch information
support committed Feb 25, 2023
1 parent bb2cb8f commit 0b21d85
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 54 deletions.
6 changes: 6 additions & 0 deletions src/Core/Grand.Domain/Catalog/CatalogSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,11 @@ public CatalogSettings()
/// 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;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ public CatalogSettingsModel()
[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; }

Expand Down
3 changes: 3 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/Upgrade/en_210.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
5 changes: 3 additions & 2 deletions src/Web/Grand.Web/Controllers/CatalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ public virtual async Task<IActionResult> Brand(string brandId, CatalogPagingFilt
return 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);
}
Expand Down
10 changes: 0 additions & 10 deletions src/Web/Grand.Web/Events/Cache/CacheKeyConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ public static class CacheKeyConst
/// {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>
Expand Down
98 changes: 60 additions & 38 deletions src/Web/Grand.Web/Features/Handlers/Catalog/GetBrandAllHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,66 +12,90 @@

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;
}
}
}
}
3 changes: 2 additions & 1 deletion src/Web/Grand.Web/Features/Models/Catalog/GetBrandAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

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; }
}
}
12 changes: 12 additions & 0 deletions src/Web/Grand.Web/Models/Catalog/BrandListModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
8 changes: 8 additions & 0 deletions src/Web/Grand.Web/Models/Catalog/BrandPagingModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Grand.Web.Common.Page.Paging;

namespace Grand.Web.Models.Catalog;

public class BrandPagingModel: BasePageableModel
{

}
5 changes: 3 additions & 2 deletions src/Web/Grand.Web/Views/Catalog/BrandAll.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model IList<BrandModel>
@model BrandListModel
@inject IPageHeadBuilder pagebuilder
@{
Layout = "_TwoColumns";
Expand All @@ -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">
Expand All @@ -26,6 +26,7 @@
</div>
}
</div>
<page-navigation asp-query-param="pagenumber" asp-pagination="Model.PagingModel" />
</div>
</div>

0 comments on commit 0b21d85

Please sign in to comment.