Fix/8302 manage pagination blogpostarchive (#8304)

This commit is contained in:
ElenaRepository
2020-01-09 21:13:52 +01:00
committed by Sébastien Ros
parent 608df75429
commit fc9a08fd12
4 changed files with 34 additions and 10 deletions

View File

@@ -9,7 +9,9 @@ using Orchard.Core.Feeds;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc;
using Orchard.Settings;
using Orchard.Themes;
using Orchard.UI.Navigation;
namespace Orchard.Blogs.Controllers {
[Themed]
@@ -19,19 +21,22 @@ namespace Orchard.Blogs.Controllers {
private readonly IBlogPostService _blogPostService;
private readonly IFeedManager _feedManager;
private readonly IArchiveConstraint _archiveConstraint;
private readonly ISiteService _siteService;
public BlogPostController(
IOrchardServices services,
IBlogService blogService,
IBlogPostService blogPostService,
IFeedManager feedManager,
IShapeFactory shapeFactory,
IArchiveConstraint archiveConstraint) {
IArchiveConstraint archiveConstraint,
ISiteService siteService) {
_services = services;
_blogService = blogService;
_blogPostService = blogPostService;
_feedManager = feedManager;
_archiveConstraint = archiveConstraint;
_siteService = siteService;
T = NullLocalizer.Instance;
Shape = shapeFactory;
}
@@ -39,7 +44,8 @@ namespace Orchard.Blogs.Controllers {
dynamic Shape { get; set; }
public Localizer T { get; set; }
public ActionResult ListByArchive(string path) {
public ActionResult ListByArchive(string path, PagerParameters pagerParameters) {
Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
var blogPath = _archiveConstraint.FindPath(path);
var archive = _archiveConstraint.FindArchiveData(path);
@@ -60,15 +66,21 @@ namespace Orchard.Blogs.Controllers {
return new ShapeResult(this, Shape.Parts_Blogs_BlogArchives(Blog: blogPart, Archives: _blogPostService.GetArchives(blogPart)));
}
pager.PageSize = blogPart.PostsPerPage;
var list = Shape.List();
list.AddRange(_blogPostService.Get(blogPart, archive).Select(b => _services.ContentManager.BuildDisplay(b, "Summary")));
list.AddRange(_blogPostService.Get(blogPart, archive, pager.GetStartIndex(), pager.PageSize)
.Select(b => _services.ContentManager.BuildDisplay(b, "Summary")));
var totalItemCount = _blogPostService.Get(blogPart, archive).Count();
_feedManager.Register(blogPart, _services.ContentManager.GetItemMetadata(blogPart).DisplayText);
var viewModel = Shape.ViewModel()
.ContentItems(list)
.Blog(blogPart)
.ArchiveData(archive);
.ArchiveData(archive)
.Pager(Shape.Pager(pager).TotalItemCount(totalItemCount));
return View(viewModel);
}

View File

@@ -61,6 +61,18 @@ namespace Orchard.Blogs.Services {
}
public IEnumerable<BlogPostPart> Get(BlogPart blogPart, ArchiveData archiveData) {
return GetBlogArchiveQuery(blogPart,archiveData)
.List().Select(ci => ci.As<BlogPostPart>());
}
public IEnumerable<BlogPostPart> Get(BlogPart blogPart, ArchiveData archiveData, int skip, int count) {
return GetBlogArchiveQuery(blogPart,archiveData)
.Slice(skip, count)
.ToList()
.Select(ci => ci.As<BlogPostPart>());
}
private IContentQuery<ContentItem, CommonPartRecord> GetBlogArchiveQuery(BlogPart blogPart, ArchiveData archiveData) {
var query = GetBlogQuery(blogPart, VersionOptions.Published);
if (archiveData.Day > 0) {
@@ -68,8 +80,7 @@ namespace Orchard.Blogs.Services {
query = query.Where(cr => cr.CreatedUtc >= dayDate && cr.CreatedUtc < dayDate.AddDays(1));
}
else if (archiveData.Month > 0)
{
else if (archiveData.Month > 0) {
var monthDate = new DateTime(archiveData.Year, archiveData.Month, 1);
query = query.Where(cr => cr.CreatedUtc >= monthDate && cr.CreatedUtc < monthDate.AddMonths(1));
@@ -79,8 +90,7 @@ namespace Orchard.Blogs.Services {
query = query.Where(cr => cr.CreatedUtc >= yearDate && cr.CreatedUtc < yearDate.AddYears(1));
}
return query.List().Select(ci => ci.As<BlogPostPart>());
return query;
}
public IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(BlogPart blogPart) {

View File

@@ -10,6 +10,7 @@ namespace Orchard.Blogs.Services {
IEnumerable<BlogPostPart> Get(BlogPart blogPart);
IEnumerable<BlogPostPart> Get(BlogPart blogPart, VersionOptions versionOptions);
IEnumerable<BlogPostPart> Get(BlogPart blogPart, ArchiveData archiveData);
IEnumerable<BlogPostPart> Get(BlogPart blogPart, ArchiveData archiveData, int skip, int count);
IEnumerable<BlogPostPart> Get(BlogPart blogPart, int skip, int count);
IEnumerable<BlogPostPart> Get(BlogPart blogPart, int skip, int count, VersionOptions versionOptions);
int PostCount(BlogPart blogPart);

View File

@@ -15,4 +15,5 @@
@(new MvcHtmlString(Model.ArchiveData.Month > 0 ? string.Format(" / {0}", Html.Link(monthNames[Model.ArchiveData.Month - 1], Url.BlogArchiveMonth((BlogPart)Model.Blog, (int)Model.ArchiveData.Year, (int)Model.ArchiveData.Month))) : ""))
@(new MvcHtmlString(Model.ArchiveData.Day > 0 ? string.Format(" / {0}", Html.Link((string)Model.ArchiveData.Day.ToString(), Url.BlogArchiveDay((BlogPart)Model.Blog, (int)Model.ArchiveData.Year, (int)Model.ArchiveData.Month, (int)Model.ArchiveData.Day))) : ""))
</div>
@Display(Model.ContentItems)
@Display(Model.ContentItems)
@Display(Model.Pager)