From 43b33c99da978d921548c169674d3d89cd7ed7a0 Mon Sep 17 00:00:00 2001 From: Mark Downie Date: Wed, 12 Apr 2023 23:26:46 -0400 Subject: [PATCH 1/2] Allows you to pin a specific post to your home page. Your RSS feed will continue to be updated but the home page is more versatile. --- .../ConfigFile/Interfaces/ISiteConfig.cs | 2 ++ .../DasBlog.Services/ConfigFile/SiteConfig.cs | 2 +- .../DasBlog.Tests/UnitTests/SiteConfigTest.cs | 1 + .../DasBlog.Web.Repositories/BlogManager.cs | 5 +++ .../Interfaces/IBlogManager.cs | 2 ++ .../Controllers/AdminController.cs | 2 ++ .../Controllers/HomeController.cs | 31 ++++++++++++++++--- .../AdminViewModels/BlogPostListViewModel.cs | 23 ++++++++++++++ .../DasBlogSettingsViewModel.cs | 7 ++--- .../Models/AdminViewModels/SiteViewModel.cs | 10 ++++-- .../Views/Admin/Settings.cshtml | 15 +++++++++ 11 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 source/DasBlog.Web.UI/Models/AdminViewModels/BlogPostListViewModel.cs diff --git a/source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs b/source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs index 5acf2e787..b2cca4fdf 100644 --- a/source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs +++ b/source/DasBlog.Services/ConfigFile/Interfaces/ISiteConfig.cs @@ -370,5 +370,7 @@ public interface ISiteConfig bool EnableRewritingHashtagsToCategoryLinks { get; set; } bool EnableRewritingBareLinksToEmbeddings { get; set; } bool EnableRewritingBareLinksToIcons { get; set; } + + string PostPinnedToHomePage { get; set; } } } diff --git a/source/DasBlog.Services/ConfigFile/SiteConfig.cs b/source/DasBlog.Services/ConfigFile/SiteConfig.cs index b4f9b8b02..be2345dbf 100644 --- a/source/DasBlog.Services/ConfigFile/SiteConfig.cs +++ b/source/DasBlog.Services/ConfigFile/SiteConfig.cs @@ -251,6 +251,6 @@ public string Root { public string MastodonServerUrl { get; set; } public string MastodonAccount { get; set; } - + public string PostPinnedToHomePage { get; set; } } } diff --git a/source/DasBlog.Tests/UnitTests/SiteConfigTest.cs b/source/DasBlog.Tests/UnitTests/SiteConfigTest.cs index 91363d389..21487251b 100644 --- a/source/DasBlog.Tests/UnitTests/SiteConfigTest.cs +++ b/source/DasBlog.Tests/UnitTests/SiteConfigTest.cs @@ -173,5 +173,6 @@ public class SiteConfigTest : ISiteConfig public bool EnableRewritingHashtagsToCategoryLinks { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public bool EnableRewritingBareLinksToEmbeddings { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public bool EnableRewritingBareLinksToIcons { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public string PostPinnedToHomePage { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } } } diff --git a/source/DasBlog.Web.Repositories/BlogManager.cs b/source/DasBlog.Web.Repositories/BlogManager.cs index bdb0a977c..e8bd91dbe 100644 --- a/source/DasBlog.Web.Repositories/BlogManager.cs +++ b/source/DasBlog.Web.Repositories/BlogManager.cs @@ -205,6 +205,11 @@ public void DeleteEntry(string postid) LogEvent(EventCodes.EntryDeleted, entry); } + public EntryCollection GetAllEntries() + { + return dataService.GetEntries(false); + } + private static StringCollection GetSearchWords(string searchString) { var searchWords = new StringCollection(); diff --git a/source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs b/source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs index 8aefe59fe..797399788 100644 --- a/source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs +++ b/source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs @@ -17,6 +17,8 @@ public interface IBlogManager EntryCollection GetEntriesForPage(int pageIndex, string acceptLanguageHeader); + EntryCollection GetAllEntries(); + EntrySaveState CreateEntry(Entry entry); EntrySaveState UpdateEntry(Entry entry); diff --git a/source/DasBlog.Web.UI/Controllers/AdminController.cs b/source/DasBlog.Web.UI/Controllers/AdminController.cs index f028adafe..f2f6826b7 100644 --- a/source/DasBlog.Web.UI/Controllers/AdminController.cs +++ b/source/DasBlog.Web.UI/Controllers/AdminController.cs @@ -46,6 +46,8 @@ public IActionResult Settings() var dbsvm = new DasBlogSettingsViewModel(); dbsvm.MetaConfig = mapper.Map(dasBlogSettings.MetaTags); dbsvm.SiteConfig = mapper.Map(dasBlogSettings.SiteConfiguration); + dbsvm.Posts = blogManager.GetAllEntries() + .Select(entry => mapper.Map(entry)).ToList(); return View(dbsvm); } diff --git a/source/DasBlog.Web.UI/Controllers/HomeController.cs b/source/DasBlog.Web.UI/Controllers/HomeController.cs index 68ce53077..aa30b9d12 100644 --- a/source/DasBlog.Web.UI/Controllers/HomeController.cs +++ b/source/DasBlog.Web.UI/Controllers/HomeController.cs @@ -11,10 +11,11 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; +using Quartz.Util; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Threading.Tasks; namespace DasBlog.Web.Controllers { @@ -47,9 +48,7 @@ public IActionResult Index() { lpvm = new ListPostsViewModel { - Posts = blogManager.GetFrontPagePosts(Request.Headers["Accept-Language"]) - .Select(entry => mapper.Map(entry)) - .Select(editentry => editentry).ToList() + Posts = HomePagePosts() }; foreach( var post in lpvm.Posts ) @@ -92,7 +91,6 @@ public IActionResult Page(int index) return Index(); } - var lpvm = new ListPostsViewModel { Posts = blogManager.GetEntriesForPage(index, Request.Headers["Accept-Language"]) @@ -157,6 +155,29 @@ private ListPostsViewModel AddComments(ListPostsViewModel listPostsViewModel) return listPostsViewModel; } + + private IList HomePagePosts() + { + IList posts = new List(); + + if (!dasBlogSettings.SiteConfiguration.PostPinnedToHomePage.IsNullOrWhiteSpace() && + Guid.TryParse(dasBlogSettings.SiteConfiguration.PostPinnedToHomePage, out var results)) + { + var entry = blogManager.GetBlogPostByGuid(results); + + if (entry != null) + { + posts.Add(mapper.Map(entry)); + } + } + else + { + posts = blogManager.GetFrontPagePosts(Request.Headers["Accept-Language"]) + .Select(entry => mapper.Map(entry)).ToList(); + } + + return posts; + } } } diff --git a/source/DasBlog.Web.UI/Models/AdminViewModels/BlogPostListViewModel.cs b/source/DasBlog.Web.UI/Models/AdminViewModels/BlogPostListViewModel.cs new file mode 100644 index 000000000..a32666b65 --- /dev/null +++ b/source/DasBlog.Web.UI/Models/AdminViewModels/BlogPostListViewModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using DasBlog.Web.Models.BlogViewModels; + +namespace DasBlog.Web.Models.AdminViewModels +{ + public class BlogPostListViewModel + { + public string Name { get; set; } + + public string Id { get; set; } + + public List Init(List posts) + { + var allposts = posts.Select(p => new BlogPostListViewModel { Name = p.Title, Id = p.EntryId }).ToList(); + + allposts.Insert(0, new BlogPostListViewModel { Name = "--Disable Pinning--", Id = "" }); + + return allposts; + } + } +} diff --git a/source/DasBlog.Web.UI/Models/AdminViewModels/DasBlogSettingsViewModel.cs b/source/DasBlog.Web.UI/Models/AdminViewModels/DasBlogSettingsViewModel.cs index eaca5efba..2d4a37dee 100644 --- a/source/DasBlog.Web.UI/Models/AdminViewModels/DasBlogSettingsViewModel.cs +++ b/source/DasBlog.Web.UI/Models/AdminViewModels/DasBlogSettingsViewModel.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; +using DasBlog.Web.Models.BlogViewModels; namespace DasBlog.Web.Models.AdminViewModels { @@ -9,5 +7,6 @@ public class DasBlogSettingsViewModel { public MetaViewModel MetaConfig { get; set; } public SiteViewModel SiteConfig { get; set; } + public List Posts { get; set; } } } diff --git a/source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs b/source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs index 3fc1b87f2..f1850b7b5 100644 --- a/source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs +++ b/source/DasBlog.Web.UI/Models/AdminViewModels/SiteViewModel.cs @@ -302,21 +302,25 @@ public class SiteViewModel [Description("Help meet some of the EU General Data Protection Regulation (GDPR) requirements")] public bool CookieConsentEnabled { get; set; } - [DisplayName("Default Sources (seperated by semi colon")] + [DisplayName("Default Sources (separated by semi colon")] [Description("")] [StringLength(50, MinimumLength = 1, ErrorMessage = "{0} should be between 1 to 50 characters")] public string DefaultSources { get; set; } - [DisplayName("Mastadon Server")] + [DisplayName("Mastodon Server")] [Description("")] [DataType(DataType.Url, ErrorMessage = "Invalid URL format")] public string MastodonServerUrl { get; set; } - [DisplayName("Mastadon Account (@username)")] + [DisplayName("Mastodon Account (@username)")] [Description("")] [RegularExpression("(@)((?:[A-Za-z0-9-_]*))")] public string MastodonAccount { get; set; } + [DisplayName("Pin this Post to the Home Page")] + [Description("")] + [DataType(DataType.Text, ErrorMessage = "Invalid Guid format")] + public string PostPinnedToHomePage { get; set; } public bool EntryTitleAsLink { get; set; } public bool ObfuscateEmail { get; set; } diff --git a/source/DasBlog.Web.UI/Views/Admin/Settings.cshtml b/source/DasBlog.Web.UI/Views/Admin/Settings.cshtml index cf3b0df0e..fc7f8250d 100644 --- a/source/DasBlog.Web.UI/Views/Admin/Settings.cshtml +++ b/source/DasBlog.Web.UI/Views/Admin/Settings.cshtml @@ -157,6 +157,21 @@ +
+ + @Html.LabelFor(m => @Model.SiteConfig.PostPinnedToHomePage, null, new { @class = "col-form-label col-sm-2" }) + +
+ @Html.DropDownListFor(n => n.SiteConfig.PostPinnedToHomePage, + new SelectList(new BlogPostListViewModel().Init(Model.Posts), "Id", "Name"), + new { @class = "form-select col-1" }) +
+ + + @Html.ValidationMessageFor(m => m.SiteConfig.PostPinnedToHomePage, null, new { @class = "text-danger" }) + +
+
From 1ee225e09eaa84c692a1cbf66d7b1ca63778af4a Mon Sep 17 00:00:00 2001 From: Mark Downie Date: Wed, 12 Apr 2023 23:30:16 -0400 Subject: [PATCH 2/2] Bumping the version number as I updated the config file --- azure-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 65c4bee0c..c07097ff8 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -10,7 +10,7 @@ pool: variables: buildConfiguration: 'Release' - version: 3.3 + version: 3.4 steps: - task: UseDotNet@2