From 5400e0c8737704f04de2afd03c28bbe520266a90 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 1 Jun 2024 06:29:16 +0300 Subject: [PATCH] feat: create submit configuration --- .../Configuration/SetupController.cs | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 1e9bacd..bc96cb1 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -11,6 +11,7 @@ using Mirea.Api.Endpoint.Common.Interfaces; using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Configuration.General; using Mirea.Api.Endpoint.Configuration.General.Settings; +using Mirea.Api.Endpoint.Configuration.General.Validators; using MySqlConnector; using Npgsql; using StackExchange.Redis; @@ -32,7 +33,7 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur private const string CacheAdminKey = "config_admin"; private GeneralConfig GeneralConfig -{ + { get => cache.Get(CacheGeneralKey) ?? new GeneralConfig(); set => cache.Set(CacheGeneralKey, value); } @@ -87,8 +88,8 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur }; GeneralConfig = general; - return Ok(true); - } + return Ok(true); + } catch (TException ex) { throw new ControllerArgumentException($"Error when connecting: {ex.Message}"); @@ -278,5 +279,31 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur return true; } + [HttpPost("Submit")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult Submit() + { + if (!new SettingsRequiredValidator(GeneralConfig).AreSettingsValid()) + throw new ControllerArgumentException("The necessary data has not been configured."); + // todo: change CreateUserRequest to Domain entity + if (!cache.TryGetValue(CacheAdminKey, out CreateUserRequest? user) || user == 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.json"), JsonSerializer.Serialize(user)); + + System.IO.File.WriteAllText( + PathBuilder.Combine(GeneralConfig.FilePath), + JsonSerializer.Serialize(GeneralConfig, new JsonSerializerOptions + { + WriteIndented = true + }) + ); + + return true; + } } \ No newline at end of file