From ba8ccf8b7e4278b6cbeefd44566716d2c4608471 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 29 May 2024 03:38:21 +0300 Subject: [PATCH] feat: add set database endpoints --- .../Requests/Configuration/DatabaseRequest.cs | 22 ++++++++ .../Configuration/SetupController.cs | 52 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 ApiDto/Requests/Configuration/DatabaseRequest.cs diff --git a/ApiDto/Requests/Configuration/DatabaseRequest.cs b/ApiDto/Requests/Configuration/DatabaseRequest.cs new file mode 100644 index 0000000..4308471 --- /dev/null +++ b/ApiDto/Requests/Configuration/DatabaseRequest.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace Mirea.Api.Dto.Requests.Configuration; + +public class DatabaseRequest +{ + [Required] + public required string Server { get; set; } + + [Required] + public int Port { get; set; } + + [Required] + public required string Database { get; set; } + + [Required] + public required string User { get; set; } + + [Required] + public bool Ssl { get; set; } + public string? Password { get; set; } +} \ No newline at end of file diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 1384d9b..04924ac 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -84,5 +84,57 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur } } + [HttpPost("SetPsql")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult SetPsql([FromBody] DatabaseRequest request) + { + string connectionString = $"Host={request.Server}:{request.Port};Username={request.User};Database={request.Database}"; + if (request.Password != null) + connectionString += $";Password={request.Password}"; + if (request.Ssl) + connectionString += ";SSL Mode=Require;"; + + return SetDatabase(connectionString, DbSettings.DatabaseEnum.PostgresSql); + } + + [HttpPost("SetMysql")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult SetMysql([FromBody] DatabaseRequest request) + { + string connectionString = $"Server={request.Server}:{request.Port};Uid={request.User};Database={request.Database}"; + if (request.Password != null) + connectionString += $";Pwd={request.Password}"; + if (request.Ssl) + connectionString += ";SslMode=Require;"; + + return SetDatabase(connectionString, DbSettings.DatabaseEnum.Mysql); + } + + [HttpPost("SetSqlite")] + [TokenAuthentication] + public ActionResult SetSqlite([FromQuery] string? path) + { + if (string.IsNullOrEmpty(path)) path = "database"; + + path = PathBuilder.Combine(path); + + if (!Directory.Exists(path)) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + Directory.CreateDirectory(path); + else + Directory.CreateDirectory(path, UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + else + throw new ControllerArgumentException("Such a folder exists. Enter a different name"); + + string connectionString = $"Data Source={PathBuilder.Combine(path, "database.db3")}"; + + return SetDatabase(connectionString, DbSettings.DatabaseEnum.Sqlite); + } + } + } \ No newline at end of file