forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
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
KrzysztofPajak
committed
Mar 6, 2020
1 parent
6601e0a
commit 33c8d95
Showing
7 changed files
with
208 additions
and
91 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
123 changes: 123 additions & 0 deletions
123
Grand.Web/Features/Handlers/Common/GetSitemapHandler.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,123 @@ | ||
using Grand.Core.Caching; | ||
using Grand.Core.Domain.Blogs; | ||
using Grand.Core.Domain.Common; | ||
using Grand.Core.Domain.Forums; | ||
using Grand.Core.Domain.Knowledgebase; | ||
using Grand.Core.Domain.News; | ||
using Grand.Services.Catalog; | ||
using Grand.Services.Customers; | ||
using Grand.Services.Localization; | ||
using Grand.Services.Seo; | ||
using Grand.Services.Topics; | ||
using Grand.Web.Extensions; | ||
using Grand.Web.Features.Models.Common; | ||
using Grand.Web.Infrastructure.Cache; | ||
using Grand.Web.Models.Catalog; | ||
using Grand.Web.Models.Common; | ||
using Grand.Web.Models.Topics; | ||
using MediatR; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Grand.Web.Features.Handlers.Common | ||
{ | ||
public class GetSitemapHandler : IRequestHandler<GetSitemap, SitemapModel> | ||
{ | ||
private readonly ICacheManager _cacheManager; | ||
private readonly ICategoryService _categoryService; | ||
private readonly IManufacturerService _manufacturerService; | ||
private readonly IProductService _productService; | ||
private readonly ITopicService _topicService; | ||
|
||
private readonly CommonSettings _commonSettings; | ||
private readonly BlogSettings _blogSettings; | ||
private readonly ForumSettings _forumSettings; | ||
private readonly NewsSettings _newsSettings; | ||
private readonly KnowledgebaseSettings _knowledgebaseSettings; | ||
|
||
public GetSitemapHandler(ICacheManager cacheManager, | ||
ICategoryService categoryService, | ||
IManufacturerService manufacturerService, | ||
IProductService productService, | ||
ITopicService topicService, | ||
CommonSettings commonSettings, | ||
BlogSettings blogSettings, | ||
ForumSettings forumSettings, | ||
NewsSettings newsSettings, | ||
KnowledgebaseSettings knowledgebaseSettings) | ||
{ | ||
_cacheManager = cacheManager; | ||
_categoryService = categoryService; | ||
_manufacturerService = manufacturerService; | ||
_productService = productService; | ||
_topicService = topicService; | ||
|
||
_commonSettings = commonSettings; | ||
_blogSettings = blogSettings; | ||
_forumSettings = forumSettings; | ||
_newsSettings = newsSettings; | ||
_knowledgebaseSettings = knowledgebaseSettings; | ||
} | ||
|
||
public async Task<SitemapModel> Handle(GetSitemap request, CancellationToken cancellationToken) | ||
{ | ||
string cacheKey = string.Format(ModelCacheEventConst.SITEMAP_PAGE_MODEL_KEY, | ||
request.Language.Id, | ||
string.Join(",", request.Customer.GetCustomerRoleIds()), | ||
request.Store.Id); | ||
var cachedModel = await _cacheManager.GetAsync(cacheKey, async () => | ||
{ | ||
var model = new SitemapModel { | ||
BlogEnabled = _blogSettings.Enabled, | ||
ForumEnabled = _forumSettings.ForumsEnabled, | ||
NewsEnabled = _newsSettings.Enabled, | ||
KnowledgebaseEnabled = _knowledgebaseSettings.Enabled | ||
}; | ||
//categories | ||
if (_commonSettings.SitemapIncludeCategories) | ||
{ | ||
var categories = await _categoryService.GetAllCategories(); | ||
model.Categories = categories.Select(x => x.ToModel(request.Language)).ToList(); | ||
} | ||
//manufacturers | ||
if (_commonSettings.SitemapIncludeManufacturers) | ||
{ | ||
var manufacturers = await _manufacturerService.GetAllManufacturers(); | ||
model.Manufacturers = manufacturers.Select(x => x.ToModel(request.Language)).ToList(); | ||
} | ||
//products | ||
if (_commonSettings.SitemapIncludeProducts) | ||
{ | ||
//limit product to 200 until paging is supported on this page | ||
var products = (await _productService.SearchProducts( | ||
storeId: request.Store.Id, | ||
visibleIndividuallyOnly: true, | ||
pageSize: 200)).products; | ||
model.Products = products.Select(product => new ProductOverviewModel { | ||
Id = product.Id, | ||
Name = product.GetLocalized(x => x.Name, request.Language.Id), | ||
ShortDescription = product.GetLocalized(x => x.ShortDescription, request.Language.Id), | ||
FullDescription = product.GetLocalized(x => x.FullDescription, request.Language.Id), | ||
SeName = product.GetSeName(request.Language.Id), | ||
}).ToList(); | ||
} | ||
|
||
//topics | ||
var topics = (await _topicService.GetAllTopics(request.Store.Id)) | ||
.Where(t => t.IncludeInSitemap) | ||
.ToList(); | ||
model.Topics = topics.Select(topic => new TopicModel { | ||
Id = topic.Id, | ||
SystemName = topic.SystemName, | ||
IncludeInSitemap = topic.IncludeInSitemap, | ||
IsPasswordProtected = topic.IsPasswordProtected, | ||
Title = topic.GetLocalized(x => x.Title, request.Language.Id), | ||
}) | ||
.ToList(); | ||
return model; | ||
}); | ||
return cachedModel; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Grand.Web/Features/Handlers/Common/GetSitemapXMLHandler.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,34 @@ | ||
using Grand.Core.Caching; | ||
using Grand.Services.Customers; | ||
using Grand.Services.Seo; | ||
using Grand.Web.Features.Models.Common; | ||
using Grand.Web.Infrastructure.Cache; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Grand.Web.Features.Handlers.Common | ||
{ | ||
public class GetSitemapXMLHandler : IRequestHandler<GetSitemapXml, string> | ||
{ | ||
private readonly ISitemapGenerator _sitemapGenerator; | ||
private readonly ICacheManager _cacheManager; | ||
|
||
public GetSitemapXMLHandler(ISitemapGenerator sitemapGenerator, ICacheManager cacheManager) | ||
{ | ||
_sitemapGenerator = sitemapGenerator; | ||
_cacheManager = cacheManager; | ||
} | ||
|
||
public async Task<string> Handle(GetSitemapXml request, CancellationToken cancellationToken) | ||
{ | ||
string cacheKey = string.Format(ModelCacheEventConst.SITEMAP_SEO_MODEL_KEY, request.Id, | ||
request.Language.Id, | ||
string.Join(",", request.Customer.GetCustomerRoleIds()), | ||
request.Store.Id); | ||
var siteMap = await _cacheManager.GetAsync(cacheKey, () => _sitemapGenerator.Generate(request.UrlHelper, request.Id, request.Language.Id)); | ||
return siteMap; | ||
} | ||
} | ||
} |
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,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Grand.Core.Domain.Customers; | ||
using Grand.Core.Domain.Localization; | ||
using Grand.Core.Domain.Stores; | ||
using Grand.Web.Models.Common; | ||
using MediatR; | ||
|
||
namespace Grand.Web.Features.Models.Common | ||
{ | ||
public class GetSitemap : IRequest<SitemapModel> | ||
{ | ||
public Customer Customer { get; set; } | ||
public Store Store { get; set; } | ||
public Language Language { 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,17 @@ | ||
using Grand.Core.Domain.Customers; | ||
using Grand.Core.Domain.Localization; | ||
using Grand.Core.Domain.Stores; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Grand.Web.Features.Models.Common | ||
{ | ||
public class GetSitemapXml : IRequest<string> | ||
{ | ||
public int? Id { get; set; } | ||
public Customer Customer { get; set; } | ||
public Store Store { get; set; } | ||
public Language Language { get; set; } | ||
public IUrlHelper UrlHelper { 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
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