From 12fb2425999ddaae6bf4099265a1759c8a78927b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Edling?= Date: Thu, 14 May 2020 08:56:18 +0200 Subject: [PATCH] Added razor page model for archives. Fixes #1167 --- core/Piranha.AspNetCore/Models/ArchivePage.cs | 99 +++++++++++++++++++ examples/RazorWeb/Models/BlogArchive.cs | 5 - examples/RazorWeb/Models/BlogPost.cs | 5 - examples/RazorWeb/Models/StandardPage.cs | 2 +- examples/RazorWeb/Models/TeaserPage.cs | 10 +- examples/RazorWeb/Pages/Archive.cshtml | 34 ++++--- examples/RazorWeb/Pages/Archive.cshtml.cs | 35 ------- examples/RazorWeb/Pages/Post.cshtml | 8 +- examples/RazorWeb/Seed.cs | 1 + 9 files changed, 128 insertions(+), 71 deletions(-) create mode 100644 core/Piranha.AspNetCore/Models/ArchivePage.cs delete mode 100644 examples/RazorWeb/Pages/Archive.cshtml.cs diff --git a/core/Piranha.AspNetCore/Models/ArchivePage.cs b/core/Piranha.AspNetCore/Models/ArchivePage.cs new file mode 100644 index 000000000..e9163cb00 --- /dev/null +++ b/core/Piranha.AspNetCore/Models/ArchivePage.cs @@ -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 +{ + /// + /// Razor Page model for an archive page. + /// + /// The page type + public class ArchivePage : ArchivePage + where T : PageBase + { + /// + /// Default constructor. + /// + /// The current api + /// The model loader + public ArchivePage(IApi api, IModelLoader loader) : base(api, loader) { } + } + + /// + /// Razor Page model for an archive page. + /// + /// The page type + /// The post type + public class ArchivePage : Microsoft.AspNetCore.Mvc.RazorPages.PageModel + where T : PageBase + where TPost: PostBase + { + protected readonly IApi _api; + protected readonly IModelLoader _loader; + + /// + /// Gets/sets the model data. + /// + public T Data { get; set; } + + /// + /// Gets/sets the post archive model. + /// + public PostArchive Archive { get; set; } + + /// + /// Default constructor. + /// + /// The current api + /// The model loader + public ArchivePage(IApi api, IModelLoader loader) + { + _api = api; + _loader = loader; + } + + /// + /// Gets the model data. + /// + /// The requested model id + /// The optionally requested year + /// The optionally requested month + /// The optionally requested page + /// The optionally requested category + /// The optionally requested tag + /// If the draft should be fetched + public virtual async Task 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(id, HttpContext.User, draft); + + if (Data == null) + { + return NotFound(); + } + + Archive = await _api.Archives.GetByIdAsync(id, page, category, tag, year, month); + + return Page(); + } + catch (UnauthorizedAccessException) + { + return Unauthorized(); + } + } + } +} \ No newline at end of file diff --git a/examples/RazorWeb/Models/BlogArchive.cs b/examples/RazorWeb/Models/BlogArchive.cs index e514642d6..623cb30dc 100644 --- a/examples/RazorWeb/Models/BlogArchive.cs +++ b/examples/RazorWeb/Models/BlogArchive.cs @@ -25,10 +25,5 @@ public class BlogArchive : Page /// [Region] public Regions.Hero Hero { get; set; } - - /// - /// View model property for storing the current archive items. - /// - public PostArchive Archive { get; set; } } } diff --git a/examples/RazorWeb/Models/BlogPost.cs b/examples/RazorWeb/Models/BlogPost.cs index b431ac134..a3f8b61e4 100644 --- a/examples/RazorWeb/Models/BlogPost.cs +++ b/examples/RazorWeb/Models/BlogPost.cs @@ -20,10 +20,5 @@ namespace RazorWeb.Models [PostType(Title = "Blog post")] public class BlogPost : Post { - /// - /// Gets/sets the heading. - /// - [Region()] - public Regions.Hero Hero { get; set; } } } diff --git a/examples/RazorWeb/Models/StandardPage.cs b/examples/RazorWeb/Models/StandardPage.cs index ba6d77bad..c0a65d34d 100644 --- a/examples/RazorWeb/Models/StandardPage.cs +++ b/examples/RazorWeb/Models/StandardPage.cs @@ -26,7 +26,7 @@ public class StandardPage : Page /// /// Gets/sets the page header. /// - [Region(Display = RegionDisplayMode.Setting)] + [Region] public Regions.Hero Hero { get; set; } } } diff --git a/examples/RazorWeb/Models/TeaserPage.cs b/examples/RazorWeb/Models/TeaserPage.cs index 10ff8043e..6d9c49c78 100644 --- a/examples/RazorWeb/Models/TeaserPage.cs +++ b/examples/RazorWeb/Models/TeaserPage.cs @@ -22,17 +22,17 @@ namespace RazorWeb.Models [PageTypeRoute(Title = "Default", Route = "/teaserpage")] public class TeaserPage : Page { - [Region(Title = "All fields")] - [RegionDescription("Vestibulum id ligula porta felis euismod semper. Curabitur blandit tempus porttitor.")] - public Regions.AllFields AllFields { get; set; } - /// /// Gets/sets the page header. /// - [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 semper. Curabitur blandit tempus porttitor.")] + public Regions.AllFields AllFields { get; set; } + /// /// Gets/sets the available teasers. /// diff --git a/examples/RazorWeb/Pages/Archive.cshtml b/examples/RazorWeb/Pages/Archive.cshtml index 8840561e9..db5c8706b 100644 --- a/examples/RazorWeb/Pages/Archive.cshtml +++ b/examples/RazorWeb/Pages/Archive.cshtml @@ -1,19 +1,19 @@ @page -@model ArchiveModel +@model ArchivePage @using System.Globalization @{ ViewData["Title"] = Model.Data.Title; Func 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 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 ""; }; @@ -36,7 +36,7 @@
- @foreach (var post in Model.Data.Archive.Posts) + @foreach (var post in Model.Archive.Posts) { @@ -69,7 +71,7 @@
- @if (Model.Data.Archive.TotalPages > 1) + @if (Model.Archive.TotalPages > 1) {
@@ -82,23 +84,23 @@
  • - + Previous
  • - @for (var n = 1; n <= Model.Data.Archive.TotalPages; n++) + @for (var n = 1; n <= Model.Archive.TotalPages; n++) { -
  • @n
  • +
  • @n
  • }
  • - + Next
  • - + Next diff --git a/examples/RazorWeb/Pages/Archive.cshtml.cs b/examples/RazorWeb/Pages/Archive.cshtml.cs deleted file mode 100644 index af0707887..000000000 --- a/examples/RazorWeb/Pages/Archive.cshtml.cs +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - * - * http://github.com/tidyui/coreweb - * - */ - -using System; -using System.Threading.Tasks; -using Piranha; -using RazorWeb.Models; - -namespace RazorWeb.Pages -{ - public class ArchiveModel : Microsoft.AspNetCore.Mvc.RazorPages.PageModel - { - private readonly IApi _api; - public BlogArchive Data { get; private set; } - - public ArchiveModel(IApi api) : base() - { - _api = api; - } - - public async Task OnGet(Guid id, int? year = null, int? month = null, int? page = null, - Guid? category = null, Guid? tag = null) - { - Data = await _api.Pages.GetByIdAsync(id); - Data.Archive = await _api.Archives.GetByIdAsync(id, page, category, tag, year, month); - } - } -} \ No newline at end of file diff --git a/examples/RazorWeb/Pages/Post.cshtml b/examples/RazorWeb/Pages/Post.cshtml index c3747d9b0..cc6d01d45 100644 --- a/examples/RazorWeb/Pages/Post.cshtml +++ b/examples/RazorWeb/Pages/Post.cshtml @@ -26,20 +26,20 @@
  • - @if (Model.Data.Hero.PrimaryImage.HasValue) + @if (Model.Data.PrimaryImage.HasValue) {
    - +
    }
    - @if (!string.IsNullOrEmpty(Model.Data.Hero.Ingress.Value)) + @if (!string.IsNullOrEmpty(Model.Data.Excerpt)) { -

    @Model.Data.Hero.Ingress.Value

    +

    @Model.Data.Excerpt

    } @Html.DisplayFor(m => m.Data.Blocks) diff --git a/examples/RazorWeb/Seed.cs b/examples/RazorWeb/Seed.cs index d46bef663..c625ccd5f 100644 --- a/examples/RazorWeb/Seed.cs +++ b/examples/RazorWeb/Seed.cs @@ -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");