Compare commits

...

3 Commits

Author SHA1 Message Date
5400e0c873 feat: create submit configuration
Some checks failed
.NET Test Pipeline / build-and-test (pull_request) Failing after 6m2s
2024-06-01 06:29:16 +03:00
1fd6c8657a fix: change connction string for mysql 2024-06-01 06:28:13 +03:00
d09011d25a feat: add create admin 2024-06-01 06:27:49 +03:00

View File

@ -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;
@ -28,9 +29,11 @@ namespace Mirea.Api.Endpoint.Controllers.Configuration;
[MaintenanceModeIgnore]
public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigureService notConfigureService, IMemoryCache cache) : BaseController
{
private const string CacheGeneralKey = "config_part";
private const string CacheGeneralKey = "config_general";
private const string CacheAdminKey = "config_admin";
private GeneralConfig GeneralConfig
{
{
get => cache.Get<GeneralConfig>(CacheGeneralKey) ?? new GeneralConfig();
set => cache.Set(CacheGeneralKey, value);
}
@ -85,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}");
@ -112,11 +115,11 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur
[BadRequestResponse]
public ActionResult<bool> SetMysql([FromBody] DatabaseRequest request)
{
string connectionString = $"Server={request.Server}:{request.Port};Uid={request.User};Database={request.Database}";
string connectionString = $"Server={request.Server}:{request.Port};Uid={request.User};Database={request.Database};";
if (request.Password != null)
connectionString += $";Pwd={request.Password}";
connectionString += $"Pwd={request.Password};";
if (request.Ssl)
connectionString += ";SslMode=Require;";
connectionString += "SslMode=Require;";
return SetDatabase<MySqlConnection, MySqlException>(connectionString, DbSettings.DatabaseEnum.Mysql);
}
@ -190,6 +193,16 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur
return Ok(true);
}
[HttpPost("CreateAdmin")]
[TokenAuthentication]
[BadRequestResponse]
public ActionResult<string> CreateAdmin([FromBody] CreateUserRequest user)
{
// todo: change CreateUserRequest to Domain entity
cache.Set(CacheAdminKey, user);
return Ok(true);
}
[HttpPost("SetLogging")]
[TokenAuthentication]
[BadRequestResponse]
@ -266,5 +279,31 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur
return true;
}
[HttpPost("Submit")]
[TokenAuthentication]
[BadRequestResponse]
public ActionResult<bool> 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;
}
}