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