-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
support
committed
Mar 5, 2023
1 parent
85cda94
commit 332a423
Showing
9 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryAllHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using Grand.Business.Core.Extensions; | ||
using Grand.Business.Core.Interfaces.Catalog.Categories; | ||
using Grand.Business.Core.Interfaces.Common.Localization; | ||
using Grand.Business.Core.Interfaces.Storage; | ||
using Grand.Domain.Catalog; | ||
using Grand.Domain.Media; | ||
using Grand.Web.Extensions; | ||
using Grand.Web.Features.Models.Catalog; | ||
using Grand.Web.Models.Catalog; | ||
using Grand.Web.Models.Media; | ||
using MediatR; | ||
|
||
namespace Grand.Web.Features.Handlers.Catalog | ||
{ | ||
public class GetCategoryAllHandler : IRequestHandler<GetCategoryAll, CategoryListModel> | ||
{ | ||
private readonly ICategoryService _categoryService; | ||
private readonly IPictureService _pictureService; | ||
private readonly ITranslationService _translationService; | ||
private readonly MediaSettings _mediaSettings; | ||
private readonly CatalogSettings _catalogSettings; | ||
|
||
public GetCategoryAllHandler(ICategoryService categoryService, | ||
IPictureService pictureService, | ||
ITranslationService translationService, | ||
MediaSettings mediaSettings, | ||
CatalogSettings catalogSettings) | ||
{ | ||
_categoryService = categoryService; | ||
_pictureService = pictureService; | ||
_translationService = translationService; | ||
_mediaSettings = mediaSettings; | ||
_catalogSettings = catalogSettings; | ||
} | ||
|
||
public async Task<CategoryListModel> Handle(GetCategoryAll request, CancellationToken cancellationToken) | ||
{ | ||
var model = new CategoryListModel(); | ||
model.CategoriesModel = await PrepareCategories(request, model); | ||
return model; | ||
} | ||
|
||
private async Task<List<CategoryModel>> PrepareCategories(GetCategoryAll request, CategoryListModel categoryListModel) | ||
{ | ||
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<CategoryModel>(); | ||
var categories = await _categoryService.GetAllCategories(storeId: request.Store.Id, | ||
pageIndex: request.Command.PageNumber - 1, | ||
pageSize: request.Command.PageSize | ||
); | ||
|
||
categoryListModel.PagingModel.LoadPagedList(categories); | ||
|
||
foreach (var category in categories) | ||
{ | ||
model.Add(await BuildCategory(category, request)); | ||
} | ||
|
||
return model; | ||
} | ||
|
||
private async Task<CategoryModel> BuildCategory(Category category, GetCategoryAll request) | ||
{ | ||
var model = category.ToModel(request.Language); | ||
|
||
//prepare picture model | ||
var picture = !string.IsNullOrEmpty(category.PictureId) | ||
? await _pictureService.GetPictureById(category.PictureId) | ||
: null; | ||
model.PictureModel = new PictureModel { | ||
Id = category.PictureId, | ||
FullSizeImageUrl = await _pictureService.GetPictureUrl(category.PictureId), | ||
ImageUrl = await _pictureService.GetPictureUrl(category.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"), | ||
category.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"), | ||
category.Name) | ||
}; | ||
|
||
return model; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Web/Grand.Web/Features/Models/Catalog/GetCategoryAll.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Grand.Domain.Customers; | ||
using Grand.Domain.Localization; | ||
using Grand.Domain.Stores; | ||
using Grand.Web.Models.Catalog; | ||
using MediatR; | ||
|
||
namespace Grand.Web.Features.Models.Catalog | ||
{ | ||
public class GetCategoryAll : IRequest<CategoryListModel> | ||
{ | ||
public Store Store { get; set; } | ||
public Customer Customer { get; set; } | ||
public Language Language { get; set; } | ||
public CategoryPagingModel Command { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Grand.Web.Models.Catalog; | ||
|
||
public class CategoryListModel | ||
{ | ||
public CategoryListModel() | ||
{ | ||
PagingModel = new CategoryPagingModel(); | ||
CategoriesModel = new List<CategoryModel>(); | ||
} | ||
public CategoryPagingModel PagingModel { get; set; } | ||
public IList<CategoryModel> CategoriesModel { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 CategoryPagingModel: BasePageableModel | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@model CategoryListModel | ||
@inject IPageHeadBuilder pagebuilder | ||
@{ | ||
Layout = "_SingleColumn"; | ||
|
||
//title | ||
pagebuilder.AddTitleParts(Loc["Title.Categories"]); | ||
} | ||
<div class="page category-page"> | ||
<h1 class="generalTitle h2">@Loc["Categories.List"]</h1> | ||
<div class="col-12 categoryGrid px-0"> | ||
<div class="form-row"> | ||
@foreach (var item in Model.CategoriesModel) | ||
{ | ||
<div class="col-md-4 col-6 mb-2"> | ||
<a href="@Url.RouteUrl("Category", new { item.SeName })" title="@item.PictureModel.Title"> | ||
<div class="card mb-2"> | ||
<img class="img-fluid" src="@item.PictureModel.ImageUrl" alt="@item.PictureModel.AlternateText"> | ||
<div class="card-body"> | ||
<h5>@item.Name</h5> | ||
</div> | ||
</div> | ||
</a> | ||
</div> | ||
} | ||
</div> | ||
@if (Model.CategoriesModel.Any()) | ||
{ | ||
<page-navigation asp-query-param="pagenumber" asp-pagination="Model.PagingModel"/> | ||
} | ||
</div> | ||
</div> |