Skip to content

Commit

Permalink
Move Sitemap to IRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Mar 6, 2020
1 parent 6601e0a commit 33c8d95
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 91 deletions.
16 changes: 14 additions & 2 deletions Grand.Web/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Grand.Services.Messages;
using Grand.Services.Stores;
using Grand.Web.Commands.Models;
using Grand.Web.Features.Models.Common;
using Grand.Web.Interfaces;
using Grand.Web.Models.Common;
using MediatR;
Expand Down Expand Up @@ -272,7 +273,11 @@ public virtual async Task<IActionResult> Sitemap([FromServices] CommonSettings c
if (!commonSettings.SitemapEnabled)
return RedirectToRoute("HomePage");

var model = await _commonViewModelService.PrepareSitemap();
var model = await _mediator.Send(new GetSitemap() {
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
Store = _storeContext.CurrentStore
});
return View(model);
}

Expand All @@ -282,7 +287,14 @@ public virtual async Task<IActionResult> SitemapXml(int? id, [FromServices] Comm
{
if (!commonSettings.SitemapEnabled)
return RedirectToRoute("HomePage");
var siteMap = await _commonViewModelService.SitemapXml(id, Url);

var siteMap = await _mediator.Send(new GetSitemapXml() {
Id = id,
Customer = _workContext.CurrentCustomer,
Language = _workContext.WorkingLanguage,
Store = _storeContext.CurrentStore,
UrlHelper = Url,
});

return Content(siteMap, "text/xml");
}
Expand Down
123 changes: 123 additions & 0 deletions Grand.Web/Features/Handlers/Common/GetSitemapHandler.cs
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 Grand.Web/Features/Handlers/Common/GetSitemapXMLHandler.cs
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;
}
}
}
19 changes: 19 additions & 0 deletions Grand.Web/Features/Models/Common/GetSitemap.cs
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; }
}
}
17 changes: 17 additions & 0 deletions Grand.Web/Features/Models/Common/GetSitemapXml.cs
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; }
}
}
5 changes: 0 additions & 5 deletions Grand.Web/Interfaces/ICommonViewModelService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Grand.Core.Domain.Customers;
using Grand.Web.Models.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Grand.Web.Interfaces
Expand All @@ -22,8 +19,6 @@ public partial interface ICommonViewModelService
Task<ShoppingCartLinksModel> PrepareShoppingCartLinks(Customer customer);
Task<AdminHeaderLinksModel> PrepareAdminHeaderLinks(Customer customer);
Task<FooterModel> PrepareFooter();
Task<SitemapModel> PrepareSitemap();
Task<string> SitemapXml(int? id, IUrlHelper url);
StoreThemeSelectorModel PrepareStoreThemeSelector();
FaviconModel PrepareFavicon();
Task<string> PrepareRobotsTextFile();
Expand Down
85 changes: 1 addition & 84 deletions Grand.Web/Services/CommonViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
using Grand.Core.Domain.Vendors;
using Grand.Framework.Themes;
using Grand.Framework.UI;
using Grand.Services.Catalog;
using Grand.Services.Common;
using Grand.Services.Customers;
using Grand.Services.Directory;
using Grand.Services.Forums;
using Grand.Services.Localization;
Expand All @@ -26,14 +24,10 @@
using Grand.Services.Seo;
using Grand.Services.Stores;
using Grand.Services.Topics;
using Grand.Web.Extensions;
using Grand.Web.Infrastructure.Cache;
using Grand.Web.Interfaces;
using Grand.Web.Models.Catalog;
using Grand.Web.Models.Common;
using Grand.Web.Models.Topics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
Expand All @@ -58,9 +52,6 @@ public partial class CommonViewModelService : ICommonViewModelService
private readonly IPageHeadBuilder _pageHeadBuilder;
private readonly ITopicService _topicService;
private readonly ILocalizationService _localizationService;
private readonly ICategoryService _categoryService;
private readonly IManufacturerService _manufacturerService;
private readonly IProductService _productService;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IServiceProvider _serviceProvider;

Expand Down Expand Up @@ -89,9 +80,6 @@ public CommonViewModelService(ICacheManager cacheManager,
IPageHeadBuilder pageHeadBuilder,
ITopicService topicService,
ILocalizationService localizationService,
ICategoryService categoryService,
IManufacturerService manufacturerService,
IProductService productService,
IWebHostEnvironment hostingEnvironment,
IServiceProvider serviceProvider,
StoreInformationSettings storeInformationSettings,
Expand Down Expand Up @@ -119,9 +107,6 @@ public CommonViewModelService(ICacheManager cacheManager,
_pageHeadBuilder = pageHeadBuilder;
_topicService = topicService;
_localizationService = localizationService;
_categoryService = categoryService;
_manufacturerService = manufacturerService;
_productService = productService;
_hostingEnvironment = hostingEnvironment;
_serviceProvider = serviceProvider;
_storeInformationSettings = storeInformationSettings;
Expand Down Expand Up @@ -404,76 +389,7 @@ public virtual async Task<FooterModel> PrepareFooter()

return model;
}

public virtual async Task<SitemapModel> PrepareSitemap()
{
string cacheKey = string.Format(ModelCacheEventConst.SITEMAP_PAGE_MODEL_KEY,
_workContext.WorkingLanguage.Id,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
_storeContext.CurrentStore.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(_workContext.WorkingLanguage)).ToList();
}
//manufacturers
if (_commonSettings.SitemapIncludeManufacturers)
{
var manufacturers = await _manufacturerService.GetAllManufacturers();
model.Manufacturers = manufacturers.Select(x => x.ToModel(_workContext.WorkingLanguage)).ToList();
}
//products
if (_commonSettings.SitemapIncludeProducts)
{
//limit product to 200 until paging is supported on this page
var products = (await _productService.SearchProducts(
storeId: _storeContext.CurrentStore.Id,
visibleIndividuallyOnly: true,
pageSize: 200)).products;
model.Products = products.Select(product => new ProductOverviewModel {
Id = product.Id,
Name = product.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
ShortDescription = product.GetLocalized(x => x.ShortDescription, _workContext.WorkingLanguage.Id),
FullDescription = product.GetLocalized(x => x.FullDescription, _workContext.WorkingLanguage.Id),
SeName = product.GetSeName(_workContext.WorkingLanguage.Id),
}).ToList();
}

//topics
var topics = (await _topicService.GetAllTopics(_storeContext.CurrentStore.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, _workContext.WorkingLanguage.Id),
})
.ToList();
return model;
});
return cachedModel;
}
public virtual async Task<string> SitemapXml(int? id, [FromServices] IUrlHelper url)
{
var sitemapGenerator = _serviceProvider.GetRequiredService<ISitemapGenerator>();
string cacheKey = string.Format(ModelCacheEventConst.SITEMAP_SEO_MODEL_KEY, id,
_workContext.WorkingLanguage.Id,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
_storeContext.CurrentStore.Id);
var siteMap = await _cacheManager.GetAsync(cacheKey, () => sitemapGenerator.Generate(url, id, _workContext.WorkingLanguage.Id));
return siteMap;
}
public virtual StoreThemeSelectorModel PrepareStoreThemeSelector()
{
var model = new StoreThemeSelectorModel();
Expand Down Expand Up @@ -511,6 +427,7 @@ public virtual FaviconModel PrepareFavicon()
model.FaviconUrl = _webHelper.GetStoreLocation() + faviconFileName;
return model;
}

public virtual async Task<string> PrepareRobotsTextFile()
{
var sb = new StringBuilder();
Expand Down

0 comments on commit 33c8d95

Please sign in to comment.