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.

2.4 KB
Raw Blame
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Catalog.Collections;
using Grand.Domain.Catalog;
using Grand.Domain.Customers;
using Grand.Infrastructure.Caching;
using Grand.Web.Events.Cache;
using Grand.Web.Features.Models.Catalog;
using Grand.Web.Models.Catalog;
using Grand.Mediator;

namespace Grand.Web.Features.Handlers.Catalog;

public class GetCollectionNavigationHandler : IRequestHandler<GetCollectionNavigation, CollectionNavigationModel>
{
    private readonly ICacheBase _cacheBase;
    private readonly CatalogSettings _catalogSettings;
    private readonly ICollectionService _collectionService;

    public GetCollectionNavigationHandler(ICacheBase cacheBase,
        ICollectionService collectionService,
        CatalogSettings catalogSettings)
    {
        _cacheBase = cacheBase;
        _collectionService = collectionService;
        _catalogSettings = catalogSettings;
    }

    public async Task<CollectionNavigationModel> Handle(GetCollectionNavigation request,
        CancellationToken cancellationToken)
    {
        var cacheKey = string.Format(CacheKeyConst.COLLECTION_NAVIGATION_MODEL_KEY,
            request.CurrentCollectionId, request.Language.Id, string.Join(",", request.Customer.GetCustomerGroupIds()),
            request.Store.Id);
        var cacheModel = await _cacheBase.GetAsync(cacheKey, async () =>
        {
            var currentCollection = await _collectionService.GetCollectionById(request.CurrentCollectionId);
            var collections =
                await _collectionService.GetAllCollections(collectionName: "", pageSize: _catalogSettings.CollectionsBlockItemsToDisplay,
                    storeId: request.Store.Id);
            var model = new CollectionNavigationModel {
                TotalCollections = collections.TotalCount
            };

            foreach (var collection in collections)
            {
                var modelMan = new CollectionBriefInfoModel {
                    Id = collection.Id,
                    Name = collection.GetTranslation(x => x.Name, request.Language.Id),
                    Icon = collection.Icon,
                    SeName = collection.GetSeName(request.Language.Id),
                    IsActive = currentCollection != null && currentCollection.Id == collection.Id
                };
                model.Collections.Add(modelMan);
            }

            return model;
        });
        return cacheModel;
    }
}