Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added razor page model for archives. Fixes #1167 #1168

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions core/Piranha.AspNetCore/Models/ArchivePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) .NET Foundation and Contributors
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* https://github.com/piranhacms/piranha.core
*
*/

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Piranha.AspNetCore.Services;
using Piranha.Models;

namespace Piranha.AspNetCore.Models
{
/// <summary>
/// Razor Page model for an archive page.
/// </summary>
/// <typeparam name="T">The page type</typeparam>
public class ArchivePage<T> : ArchivePage<T, PostInfo>
where T : PageBase
{
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="api">The current api</param>
/// <param name="loader">The model loader</param>
public ArchivePage(IApi api, IModelLoader loader) : base(api, loader) { }
}

/// <summary>
/// Razor Page model for an archive page.
/// </summary>
/// <typeparam name="T">The page type</typeparam>
/// <typeparam name="TPost">The post type</typeparam>
public class ArchivePage<T, TPost> : Microsoft.AspNetCore.Mvc.RazorPages.PageModel
where T : PageBase
where TPost: PostBase
{
protected readonly IApi _api;
protected readonly IModelLoader _loader;

/// <summary>
/// Gets/sets the model data.
/// </summary>
public T Data { get; set; }

/// <summary>
/// Gets/sets the post archive model.
/// </summary>
public PostArchive<TPost> Archive { get; set; }

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="api">The current api</param>
/// <param name="loader">The model loader</param>
public ArchivePage(IApi api, IModelLoader loader)
{
_api = api;
_loader = loader;
}

/// <summary>
/// Gets the model data.
/// </summary>
/// <param name="id">The requested model id</param>
/// <param name="year">The optionally requested year</param>
/// <param name="month">The optionally requested month</param>
/// <param name="page">The optionally requested page</param>
/// <param name="category">The optionally requested category</param>
/// <param name="tag">The optionally requested tag</param>
/// <param name="draft">If the draft should be fetched</param>
public virtual async Task<IActionResult> OnGet(Guid id, int? year = null, int? month = null,
int? page = null, Guid? category = null, Guid? tag = null, bool draft = false)
{
try
{
Data = await _loader.GetPageAsync<T>(id, HttpContext.User, draft);

if (Data == null)
{
return NotFound();
}

Archive = await _api.Archives.GetByIdAsync<TPost>(id, page, category, tag, year, month);

return Page();
}
catch (UnauthorizedAccessException)
{
return Unauthorized();
}
}
}
}
5 changes: 0 additions & 5 deletions examples/RazorWeb/Models/BlogArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ public class BlogArchive : Page<BlogArchive>
/// </summary>
[Region]
public Regions.Hero Hero { get; set; }

/// <summary>
/// View model property for storing the current archive items.
/// </summary>
public PostArchive<DynamicPost> Archive { get; set; }
}
}
5 changes: 0 additions & 5 deletions examples/RazorWeb/Models/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,5 @@ namespace RazorWeb.Models
[PostType(Title = "Blog post")]
public class BlogPost : Post<BlogPost>
{
/// <summary>
/// Gets/sets the heading.
/// </summary>
[Region()]
public Regions.Hero Hero { get; set; }
}
}
2 changes: 1 addition & 1 deletion examples/RazorWeb/Models/StandardPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class StandardPage : Page<StandardPage>
/// <summary>
/// Gets/sets the page header.
/// </summary>
[Region(Display = RegionDisplayMode.Setting)]
[Region]
public Regions.Hero Hero { get; set; }
}
}
10 changes: 5 additions & 5 deletions examples/RazorWeb/Models/TeaserPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ namespace RazorWeb.Models
[PageTypeRoute(Title = "Default", Route = "/teaserpage")]
public class TeaserPage : Page<TeaserPage>
{
[Region(Title = "All fields")]
[RegionDescription("Vestibulum id ligula porta felis euismod <strong>semper</strong>. Curabitur blandit tempus porttitor.")]
public Regions.AllFields AllFields { get; set; }

/// <summary>
/// Gets/sets the page header.
/// </summary>
[Region(Display = RegionDisplayMode.Setting)]
[Region]
[RegionDescription("The Hero is shown on the top of your page")]
public Regions.Hero Hero { get; set; }

[Region(Title = "All fields")]
[RegionDescription("Vestibulum id ligula porta felis euismod <strong>semper</strong>. Curabitur blandit tempus porttitor.")]
public Regions.AllFields AllFields { get; set; }

/// <summary>
/// Gets/sets the available teasers.
/// </summary>
Expand Down
34 changes: 18 additions & 16 deletions examples/RazorWeb/Pages/Archive.cshtml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
@page
@model ArchiveModel
@model ArchivePage<BlogArchive>
@using System.Globalization
@{
ViewData["Title"] = Model.Data.Title;

Func<string> BlogLink = () => {
return Model.Data.Permalink
+ (Model.Data.Archive.Category != null ? $"/category/{Model.Data.Archive.Category.Slug}" : "")
+ (Model.Data.Archive.Year.HasValue ? $"/{Model.Data.Archive.Year}" : "" )
+ (Model.Data.Archive.Month.HasValue ? $"/{Model.Data.Archive.Month}" : "");
+ (Model.Archive.Category != null ? $"/category/{Model.Archive.Category.Slug}" : "")
+ (Model.Archive.Year.HasValue ? $"/{Model.Archive.Year}" : "" )
+ (Model.Archive.Month.HasValue ? $"/{Model.Archive.Month}" : "");
};

Func<string> MonthName = () => {
if (Model.Data.Archive.Month.HasValue) {
return new DateTime(2018, Model.Data.Archive.Month.Value, 1) .ToString("MMMM", CultureInfo.InvariantCulture);
if (Model.Archive.Month.HasValue) {
return new DateTime(2018, Model.Archive.Month.Value, 1) .ToString("MMMM", CultureInfo.InvariantCulture);
}
return "";
};
Expand All @@ -36,7 +36,7 @@
<div class="container body-container">
<div class="row justify-content-center">
<div class="col-lg-8 col-md-10 col-xs-12">
@foreach (var post in Model.Data.Archive.Posts)
@foreach (var post in Model.Archive.Posts)
{
<article class="archive-item">
<header>
Expand All @@ -57,19 +57,21 @@
<a href="@post.Permalink#comments">@post.CommentCount @(post.CommentCount == 1 ? "comment" : "comments")</a>
}
</p>
@if (post.Regions.Hero.PrimaryImage.Media != null)
@if (post.PrimaryImage.HasValue)
{
<img src="@Url.Content(post.Regions.Hero.PrimaryImage.Media.PublicUrl)">
<img src="@Url.Content(post.PrimaryImage)">
}
@Html.Raw(GetFirstHtmlBlock(post).Body)
<p>
@post.Excerpt
</p>

<a href="@post.Permalink" class="btn btn-sm btn-default">Read more</a>
</article>
}
</div>
</div>

@if (Model.Data.Archive.TotalPages > 1)
@if (Model.Archive.TotalPages > 1)
{
<div class="row">
<div class="col">
Expand All @@ -82,23 +84,23 @@
</a>
</li>
<li class="page-item">
<a class="page-link" href="@BlogLink()/page/@Math.Max(1, Model.Data.Archive.CurrentPage - 1)">
<a class="page-link" href="@BlogLink()/page/@Math.Max(1, Model.Archive.CurrentPage - 1)">
<span aria-hidden="true">&lsaquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
@for (var n = 1; n <= Model.Data.Archive.TotalPages; n++)
@for (var n = 1; n <= Model.Archive.TotalPages; n++)
{
<li class="page-item @(Model.Data.Archive.CurrentPage == n ? "active" : "")"><a class="page-link" href="@BlogLink()/page/@n">@n</a></li>
<li class="page-item @(Model.Archive.CurrentPage == n ? "active" : "")"><a class="page-link" href="@BlogLink()/page/@n">@n</a></li>
}
<li class="page-item">
<a class="page-link" href="@BlogLink()/page/@Math.Min(Model.Data.Archive.TotalPages, Model.Data.Archive.CurrentPage + 1)">
<a class="page-link" href="@BlogLink()/page/@Math.Min(Model.Archive.TotalPages, Model.Archive.CurrentPage + 1)">
<span aria-hidden="true">&rsaquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
<li class="page-item">
<a class="page-link" href="@BlogLink()/page/@Model.Data.Archive.TotalPages">
<a class="page-link" href="@BlogLink()/page/@Model.Archive.TotalPages">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
Expand Down
35 changes: 0 additions & 35 deletions examples/RazorWeb/Pages/Archive.cshtml.cs

This file was deleted.

8 changes: 4 additions & 4 deletions examples/RazorWeb/Pages/Post.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
</div>
</div>

@if (Model.Data.Hero.PrimaryImage.HasValue)
@if (Model.Data.PrimaryImage.HasValue)
{
<div class="row justify-content-center">
<div class="col-lg-10 col-md-12">
<img class="primary-image" src="@Url.Content(Model.Data.Hero.PrimaryImage)">
<img class="primary-image" src="@Url.Content(Model.Data.PrimaryImage)">
</div>
</div>
}

<div class="row justify-content-center">
<div class="col col-lg-8 col-md-10">
@if (!string.IsNullOrEmpty(Model.Data.Hero.Ingress.Value))
@if (!string.IsNullOrEmpty(Model.Data.Excerpt))
{
<p class="lead">@Model.Data.Hero.Ingress.Value</p>
<p class="lead">@Model.Data.Excerpt</p>
}

@Html.DisplayFor(m => m.Data.Blocks)
Expand Down
1 change: 1 addition & 0 deletions examples/RazorWeb/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ await api.Media.SaveAsync(new Piranha.Models.StreamMediaContent()
var blogpost = await Models.BlogPost.CreateAsync(api);
blogpost.BlogId = blogpage.Id;
blogpost.Title = "What is Piranha";
blogpost.Excerpt = "Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Nulla vitae elit libero, a pharetra augue. Etiam porta sem malesuada magna mollis euismod. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.";
blogpost.Category = "Piranha CMS";
blogpost.Tags.Add("welcome");

Expand Down