From 9133b57a1b2e8ec51132efb72ee66dd2c47192d9 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Thu, 4 Jul 2024 23:45:33 +0300 Subject: [PATCH] refactor: GeneralConfig --- .../Configuration/General/GeneralConfig.cs | 22 +++++++++++++++++-- .../Configuration/SetupController.cs | 13 ++--------- Endpoint/Program.cs | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Endpoint/Configuration/General/GeneralConfig.cs b/Endpoint/Configuration/General/GeneralConfig.cs index 60f98a1..2f74a9a 100644 --- a/Endpoint/Configuration/General/GeneralConfig.cs +++ b/Endpoint/Configuration/General/GeneralConfig.cs @@ -1,14 +1,32 @@ -using Mirea.Api.Endpoint.Configuration.General.Settings; +using Mirea.Api.Endpoint.Common.Services; +using Mirea.Api.Endpoint.Configuration.General.Settings; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; namespace Mirea.Api.Endpoint.Configuration.General; public class GeneralConfig { - public const string FilePath = "Settings.json"; + [JsonIgnore] private const string FileName = "Settings.json"; + + [JsonIgnore] + public static string FilePath => PathBuilder.Combine(FileName); public DbSettings? DbSettings { get; set; } public CacheSettings? CacheSettings { get; set; } public ScheduleSettings? ScheduleSettings { get; set; } public EmailSettings? EmailSettings { get; set; } public LogSettings? LogSettings { get; set; } + + public void SaveSetting() + { + File.WriteAllText( + FilePath, + JsonSerializer.Serialize(this, new JsonSerializerOptions + { + WriteIndented = true + }) + ); + } } \ No newline at end of file diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 92ec9b6..9e35c8b 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -55,7 +55,7 @@ public partial class SetupController( if (!notConfigureService.IsMaintenanceMode) throw new ControllerArgumentException( "The token cannot be generated because the server has been configured. " + - $"If you need to restart the configuration, then delete the \"{PathBuilder.Combine(GeneralConfig.FilePath)}\" file and restart the application."); + $"If you need to restart the configuration, then delete the \"{GeneralConfig.FilePath}\" file and restart the application."); var token = new byte[32]; RandomNumberGenerator.Create().GetBytes(token); @@ -333,18 +333,9 @@ public partial class SetupController( if (!cache.TryGetValue(CacheAdminKey, out Admin? admin) || admin == null) throw new ControllerArgumentException("The administrator's data was not set."); - if (System.IO.File.Exists(PathBuilder.Combine(GeneralConfig.FilePath))) - System.IO.File.Delete(PathBuilder.Combine(GeneralConfig.FilePath)); - System.IO.File.WriteAllText(PathBuilder.Combine(Admin.PathToSave), JsonSerializer.Serialize(admin)); - System.IO.File.WriteAllText( - PathBuilder.Combine(GeneralConfig.FilePath), - JsonSerializer.Serialize(GeneralConfig, new JsonSerializerOptions - { - WriteIndented = true - }) - ); + GeneralConfig.SaveSetting(); return true; } diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index d368177..3066fa4 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -36,7 +36,7 @@ public class Program var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment()); - builder.Configuration.AddJsonFile(PathBuilder.Combine(GeneralConfig.FilePath), optional: true, reloadOnChange: true); + builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true); builder.Services.Configure(builder.Configuration); builder.Configuration.AddJsonFile(PathBuilder.Combine(Admin.PathToSave), optional: true, reloadOnChange: true); builder.Services.Configure(builder.Configuration);