Skip to content

Commit

Permalink
Add new page - categories all
Browse files Browse the repository at this point in the history
  • Loading branch information
support committed Mar 5, 2023
1 parent 85cda94 commit 332a423
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12590,6 +12590,9 @@
<Resource Name="Categories.Breadcrumb.Top" Area="Front">
<Value>Home</Value>
</Resource>
<Resource Name="Categories.List" Area="Front">
<Value>Categories List</Value>
</Resource>
<Resource Name="Categories.TotalProducts" Area="Front">
<Value>({0})</Value>
</Resource>
Expand Down Expand Up @@ -15806,6 +15809,9 @@
<Resource Name="Title.Brands" Area="Front">
<Value>Brands</Value>
</Resource>
<Resource Name="Title.Categories" Area="Front">
<Value>Categories</Value>
</Resource>
<Resource Name="Title.Checkout" Area="Front">
<Value>Checkout</Value>
</Resource>
Expand Down
6 changes: 6 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 @@ -57,4 +57,10 @@
<Resource Name="Admin.Settings.Catalog.MaxCatalogPageSize" Area="Admin">
<Value>Page size for the all page of brand/collection/vendor/category</Value>
</Resource>
<Resource Name="Title.Categories" Area="Front">
<Value>Categories</Value>
</Resource>
<Resource Name="Categories.List" Area="Front">
<Value>Categories List</Value>
</Resource>
</Language>
12 changes: 12 additions & 0 deletions src/Web/Grand.Web/Controllers/CatalogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ public virtual async Task<IActionResult> Category(string categoryId, CatalogPagi

return View(layoutViewPath, model);
}
[HttpGet]
public virtual async Task<IActionResult> CategoryAll(CategoryPagingModel command)
{
var model = await _mediator.Send(new GetCategoryAll {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
Store = _workContext.CurrentStore,
Command = command
});
return View(model);
}


#endregion

Expand Down
5 changes: 5 additions & 0 deletions src/Web/Grand.Web/Endpoints/EndpointProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ private void RegisterCatalogRoute(IEndpointRouteBuilder endpointRouteBuilder, st
pattern + "producttag/all/",
new { controller = "Catalog", action = "ProductTagsAll" });

//categories
endpointRouteBuilder.MapControllerRoute("CategoryList",
pattern + "category/all/",
new { controller = "Catalog", action = "CategoryAll" });

//brands
endpointRouteBuilder.MapControllerRoute("BrandList",
pattern + "brand/all/",
Expand Down
100 changes: 100 additions & 0 deletions src/Web/Grand.Web/Features/Handlers/Catalog/GetCategoryAllHandler.cs
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 src/Web/Grand.Web/Features/Models/Catalog/GetCategoryAll.cs
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; }
}
}
12 changes: 12 additions & 0 deletions src/Web/Grand.Web/Models/Catalog/CategoryListModel.cs
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; }
}
8 changes: 8 additions & 0 deletions src/Web/Grand.Web/Models/Catalog/CategoryPagingModel.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 CategoryPagingModel: BasePageableModel
{

}
32 changes: 32 additions & 0 deletions src/Web/Grand.Web/Views/Catalog/CategoryAll.cshtml
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>

0 comments on commit 332a423

Please sign in to comment.