From 5ba5b5b8e83ab293b13bd6a133b891d43ab78377 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Tue, 22 Oct 2024 12:22:10 +0800 Subject: [PATCH 1/7] Add SocialLinkSettings to BlogConfig and initialization Added a new property `SocialLinkSettings` to the `IBlogConfig` interface and implemented it in the `BlogConfig` class. Updated the `LoadFromConfig` method to initialize `SocialLinkSettings` from the configuration dictionary. Introduced a new `SocialLinkSettings` class with properties `IsEnabled` and `Links`, and provided a static `DefaultValue`. Updated `BlogConfigInitializer` to handle `SocialLinkSettings` initialization using the mediator pattern. --- src/Moonglade.Configuration/BlogConfig.cs | 4 ++++ src/Moonglade.Configuration/SocialLink.cs | 14 ++++++++++++++ src/Moonglade.Setup/BlogConfigInitializer.cs | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/src/Moonglade.Configuration/BlogConfig.cs b/src/Moonglade.Configuration/BlogConfig.cs index e76004eb8..6ae3a5d40 100644 --- a/src/Moonglade.Configuration/BlogConfig.cs +++ b/src/Moonglade.Configuration/BlogConfig.cs @@ -14,6 +14,7 @@ public interface IBlogConfig CustomStyleSheetSettings CustomStyleSheetSettings { get; set; } CustomMenuSettings CustomMenuSettings { get; set; } LocalAccountSettings LocalAccountSettings { get; set; } + SocialLinkSettings SocialLinkSettings { get; set; } SystemManifestSettings SystemManifestSettings { get; set; } IEnumerable LoadFromConfig(IDictionary config); @@ -40,6 +41,8 @@ public class BlogConfig : IBlogConfig public LocalAccountSettings LocalAccountSettings { get; set; } + public SocialLinkSettings SocialLinkSettings { get; set; } + public SystemManifestSettings SystemManifestSettings { get; set; } public IEnumerable LoadFromConfig(IDictionary config) @@ -53,6 +56,7 @@ public IEnumerable LoadFromConfig(IDictionary config) CustomStyleSheetSettings = AssignValueForConfigItem(7, CustomStyleSheetSettings.DefaultValue, config); CustomMenuSettings = AssignValueForConfigItem(10, CustomMenuSettings.DefaultValue, config); LocalAccountSettings = AssignValueForConfigItem(11, LocalAccountSettings.DefaultValue, config); + SocialLinkSettings = AssignValueForConfigItem(12, SocialLinkSettings.DefaultValue, config); // Special case SystemManifestSettings = AssignValueForConfigItem(99, SystemManifestSettings.DefaultValue, config); diff --git a/src/Moonglade.Configuration/SocialLink.cs b/src/Moonglade.Configuration/SocialLink.cs index 4269825cc..792169df9 100644 --- a/src/Moonglade.Configuration/SocialLink.cs +++ b/src/Moonglade.Configuration/SocialLink.cs @@ -1,5 +1,19 @@ namespace Moonglade.Configuration; +public class SocialLinkSettings : IBlogSettings +{ + public bool IsEnabled { get; set; } + + public List Links { get; set; } + + public static SocialLinkSettings DefaultValue => + new() + { + IsEnabled = false, + Links = new() + }; +} + public class SocialLink { public string Name { get; set; } diff --git a/src/Moonglade.Setup/BlogConfigInitializer.cs b/src/Moonglade.Setup/BlogConfigInitializer.cs index 70945dcd5..fb7e225a6 100644 --- a/src/Moonglade.Setup/BlogConfigInitializer.cs +++ b/src/Moonglade.Setup/BlogConfigInitializer.cs @@ -59,6 +59,10 @@ await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(CustomMenuSet await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(LocalAccountSettings), LocalAccountSettings.DefaultValue.ToJson())); break; + case 12: + await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(SocialLinkSettings), + SocialLinkSettings.DefaultValue.ToJson())); + break; case 99: await mediator.Send(new AddDefaultConfigurationCommand(key, nameof(SystemManifestSettings), isNew ? From 0074982631f0f43fec7ada0366de4e76f991b3c6 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Tue, 22 Oct 2024 12:23:59 +0800 Subject: [PATCH 2/7] Add POST endpoint for updating social link settings Introduced a new POST endpoint in SettingsController mapped to "social-link". This endpoint updates the SocialLinkSettings in blogConfig with the provided model, saves the configuration asynchronously, and returns a 204 No Content response. --- src/Moonglade.Web/Controllers/SettingsController.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Moonglade.Web/Controllers/SettingsController.cs b/src/Moonglade.Web/Controllers/SettingsController.cs index 2e35972ae..fea25fda0 100644 --- a/src/Moonglade.Web/Controllers/SettingsController.cs +++ b/src/Moonglade.Web/Controllers/SettingsController.cs @@ -153,6 +153,16 @@ public async Task Advanced(AdvancedSettings model) return NoContent(); } + [HttpPost("social-link")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + public async Task SocialLink(SocialLinkSettings model) + { + blogConfig.SocialLinkSettings = model; + + await SaveConfigAsync(blogConfig.SocialLinkSettings); + return NoContent(); + } + [HttpPost("reset")] [ProducesResponseType(StatusCodes.Status202Accepted)] public async Task Reset(BlogDbContext context, IHostApplicationLifetime applicationLifetime) From ec4a6b3bbc1cd64a02429a8db6c6cba8be13852c Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Tue, 22 Oct 2024 12:27:02 +0800 Subject: [PATCH 3/7] Refactor social links config to use IBlogConfig Refactored `GetAllSocialLinksQueryHandler` to use `IBlogConfig` instead of `IConfiguration` for configuration management. Updated the handler to access social link settings directly from `blogConfig.SocialLinkSettings` and check if the feature is enabled via `IsEnabled`. Modified `_Aside.cshtml` to reflect the new configuration structure. Removed the `Experimental` section from `appsettings.json` as social links are now managed through `IBlogConfig`. These changes improve code readability, maintainability, and simplify the logic for managing social links. --- src/Moonglade.Core/GetAllSocialLinksQuery.cs | 11 +++++------ src/Moonglade.Web/Pages/Shared/_Aside.cshtml | 2 +- src/Moonglade.Web/appsettings.json | 3 --- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Moonglade.Core/GetAllSocialLinksQuery.cs b/src/Moonglade.Core/GetAllSocialLinksQuery.cs index 77cdd0fb2..276793df7 100644 --- a/src/Moonglade.Core/GetAllSocialLinksQuery.cs +++ b/src/Moonglade.Core/GetAllSocialLinksQuery.cs @@ -1,22 +1,21 @@ -using Microsoft.Extensions.Configuration; -using Moonglade.Configuration; +using Moonglade.Configuration; namespace Moonglade.Core; public record GetAllSocialLinksQuery : IRequest>; -public class GetAllSocialLinksQueryHandler(IConfiguration configuration) : IRequestHandler> +public class GetAllSocialLinksQueryHandler(IBlogConfig blogConfig) : IRequestHandler> { public Task> Handle(GetAllSocialLinksQuery request, CancellationToken ct) { - var section = configuration.GetSection("Experimental:SocialLinks"); + var section = blogConfig.SocialLinkSettings; - if (!section.Exists()) + if (!section.IsEnabled) { return Task.FromResult(new List()); } - var links = section.Get>(); + var links = blogConfig.SocialLinkSettings.Links; return Task.FromResult(links ?? new List()); } } \ No newline at end of file diff --git a/src/Moonglade.Web/Pages/Shared/_Aside.cshtml b/src/Moonglade.Web/Pages/Shared/_Aside.cshtml index e73ed8ebb..18c2c2726 100644 --- a/src/Moonglade.Web/Pages/Shared/_Aside.cshtml +++ b/src/Moonglade.Web/Pages/Shared/_Aside.cshtml @@ -40,7 +40,7 @@ } - @if (Configuration.GetSection("Experimental:SocialLinks").Exists()) + @if (BlogConfig.SocialLinkSettings.IsEnabled) {