mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-06-21 00:03:18 +02:00
added mysql connection
This commit is contained in:
parent
830c94d5e4
commit
57e8f64095
37
Server/DbConfig.cs
Normal file
37
Server/DbConfig.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Server;
|
||||||
|
|
||||||
|
public class DbConfig
|
||||||
|
{
|
||||||
|
private const string CONFIG_PATH = "config.json";
|
||||||
|
|
||||||
|
public string Username { get; set; } = "";
|
||||||
|
public string Password { get; set; } = "";
|
||||||
|
public string Server { get; set; } = "";
|
||||||
|
public string Port { get; set; } = ""; // new
|
||||||
|
public string Database { get; set; } = "";
|
||||||
|
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||||
|
string json = JsonSerializer.Serialize(this, options);
|
||||||
|
File.WriteAllText(CONFIG_PATH, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DbConfig? Load()
|
||||||
|
{
|
||||||
|
if (!File.Exists(CONFIG_PATH))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string json = File.ReadAllText(CONFIG_PATH);
|
||||||
|
return JsonSerializer.Deserialize<DbConfig>(json);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,11 +12,13 @@ namespace Server
|
|||||||
private MySqlConnection _connection;
|
private MySqlConnection _connection;
|
||||||
private MessageSender ms;
|
private MessageSender ms;
|
||||||
|
|
||||||
public MySQL(string username, string password, string server, string database, MessageSender ms)
|
|
||||||
|
public MySQL(string username, string password, string server, string port, string database, MessageSender ms)
|
||||||
{
|
{
|
||||||
this.ms = ms;
|
this.ms = ms;
|
||||||
ms.Log("[MySQL] Initializing connection...");
|
ms.Log("[MySQL] Initializing connection...");
|
||||||
string connectionString = $"Server={server};Database={database};User ID={username};Password={password};";
|
// include port in the connection string:
|
||||||
|
string connectionString = $"Server={server};Port={port};Database={database};User ID={username};Password={password};";
|
||||||
_connection = new MySqlConnection(connectionString);
|
_connection = new MySqlConnection(connectionString);
|
||||||
ms.Log("[MySQL] Connection to database...");
|
ms.Log("[MySQL] Connection to database...");
|
||||||
|
|
||||||
@ -51,6 +53,23 @@ namespace Server
|
|||||||
_ => "b"
|
_ => "b"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Executes a non‐query SQL statement (e.g. CREATE TABLE, ALTER, etc.).
|
||||||
|
/// Returns null on success, or an error JSON if it fails.
|
||||||
|
/// </summary>
|
||||||
|
public string ExecuteNonQuery(string sql)
|
||||||
|
{
|
||||||
|
using var cmd = new MySqlCommand(sql, _connection);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
return JsonResponse(new { success = true });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return JsonResponse($"Execute failed: {ex.Message}", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Insert(string table, Dictionary<string, object> data)
|
public string Insert(string table, Dictionary<string, object> data)
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using DX86;
|
using System.Text.Json;
|
||||||
|
using DX86;
|
||||||
using DX86.Modules;
|
using DX86.Modules;
|
||||||
using Library;
|
using Library;
|
||||||
|
|
||||||
@ -16,19 +17,121 @@ class Program
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Initializing server...");
|
Console.WriteLine("Initializing server...");
|
||||||
// Initialize the server components
|
// Initialize UI and logging components
|
||||||
messageSender = new MessageSender("log.txt");
|
messageSender = new MessageSender("log.txt");
|
||||||
messageBox = new MessageBox(messageSender);
|
messageBox = new MessageBox(messageSender);
|
||||||
inputBox = new InputBox(messageSender);
|
inputBox = new InputBox(messageSender);
|
||||||
itemSelector = new ItemSelector(messageSender);
|
itemSelector = new ItemSelector(messageSender);
|
||||||
servers = new List<TcpServer>();
|
servers = new List<TcpServer>();
|
||||||
|
|
||||||
// Connect to the database
|
// 1) Load existing DB config or prompt user + initialize
|
||||||
//mySql = new MySQL("username", "password", "localhost", "database", messageSender);
|
mySql = LoadOrInitDatabase();
|
||||||
|
|
||||||
// Start the server
|
// 2) Start the rest of your server loop
|
||||||
ServerLoop();
|
ServerLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MySQL LoadOrInitDatabase()
|
||||||
|
{
|
||||||
|
var cfg = DbConfig.Load();
|
||||||
|
if (cfg != null)
|
||||||
|
{
|
||||||
|
messageSender.Log("[Program] Loaded existing database configuration.");
|
||||||
|
// Use existing credentials to connect (including cfg.Port)
|
||||||
|
return new MySQL(cfg.Username, cfg.Password, cfg.Server, cfg.Port, cfg.Database, messageSender);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
messageSender.Warn("[Program] No existing DB configuration found. Starting init…");
|
||||||
|
|
||||||
|
// REPLACE THE FOUR PROMPTS WITH YOUR FIVE ShowAsync(...) CALLS:
|
||||||
|
string username = inputBox.ShowAsync("MySQL Configuration [1/5]", ["Username for MySQL Connections"]);
|
||||||
|
string password = inputBox.ShowAsync("MySQL Configuration [2/5]", ["Password for MySQL Connections"]);
|
||||||
|
string server = inputBox.ShowAsync("MySQL Configuration [3/5]", ["Server Address for MySQL Connections"]);
|
||||||
|
string port = inputBox.ShowAsync("MySQL Configuration [4/5]", ["Port for MySQL Connections"]);
|
||||||
|
string database = inputBox.ShowAsync("MySQL Configuration [5/5]", ["Database Name for Database Connections"]);
|
||||||
|
|
||||||
|
return InitializeDatabase(username, password, server, port, database);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new MySQL connection, runs the CREATE TABLE DDL,
|
||||||
|
/// saves credentials in config.json, and returns the MySQL instance.
|
||||||
|
/// </summary>
|
||||||
|
private static MySQL InitializeDatabase(
|
||||||
|
string username,
|
||||||
|
string password,
|
||||||
|
string server,
|
||||||
|
string port,
|
||||||
|
string database
|
||||||
|
)
|
||||||
|
{
|
||||||
|
// 1) Instantiate MySQL (will exit on failure)
|
||||||
|
var db = new MySQL(username, password, server, port, database, messageSender);
|
||||||
|
|
||||||
|
// 2) Create the 'employees' table if it does not exist:
|
||||||
|
string createEmployeesTable = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS `employees` (
|
||||||
|
`Id` VARCHAR(36) NOT NULL PRIMARY KEY,
|
||||||
|
`Code` VARCHAR(50) NOT NULL,
|
||||||
|
`Surname` VARCHAR(100) NOT NULL,
|
||||||
|
`Forename` VARCHAR(100) NOT NULL,
|
||||||
|
`Email` VARCHAR(150) NOT NULL,
|
||||||
|
`Phone` VARCHAR(50),
|
||||||
|
`Street` VARCHAR(200),
|
||||||
|
`City` VARCHAR(100),
|
||||||
|
`Postcode` VARCHAR(20),
|
||||||
|
`Country` VARCHAR(100),
|
||||||
|
`Department` VARCHAR(100),
|
||||||
|
`Position` VARCHAR(100),
|
||||||
|
`EmployeeState` INT NOT NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
";
|
||||||
|
|
||||||
|
messageSender.Log("[Program] Creating 'employees' table if not present…");
|
||||||
|
var resultJson = db.ExecuteNonQuery(createEmployeesTable);
|
||||||
|
|
||||||
|
// Parse resultJson safely using JsonDocument
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var doc = JsonDocument.Parse(resultJson);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
|
||||||
|
// Expecting { "error": bool, "data": ... }
|
||||||
|
bool isError = root.GetProperty("error").GetBoolean();
|
||||||
|
if (isError)
|
||||||
|
{
|
||||||
|
// You can inspect root.GetProperty("data").GetString() if needed
|
||||||
|
messageSender.Error("[Program] Failed to create 'employees' table: " + resultJson);
|
||||||
|
Environment.Exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// If something went wrong parsing JSON, treat it as a fatal error
|
||||||
|
messageSender.Error($"[Program] Could not parse CREATE TABLE response: {ex.Message}");
|
||||||
|
messageSender.Error("[Program] Raw response: " + resultJson);
|
||||||
|
Environment.Exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
messageSender.Log("[Program] 'employees' table is ready.");
|
||||||
|
|
||||||
|
// TODO: repeat db.ExecuteNonQuery(...) for any other tables you need
|
||||||
|
|
||||||
|
// 3) Save the new credentials (including port) into config.json:
|
||||||
|
var newCfg = new DbConfig
|
||||||
|
{
|
||||||
|
Username = username,
|
||||||
|
Password = password,
|
||||||
|
Server = server,
|
||||||
|
Port = port,
|
||||||
|
Database = database
|
||||||
|
};
|
||||||
|
newCfg.Save();
|
||||||
|
messageSender.Log("[Program] Saved DB configuration to config.json.");
|
||||||
|
|
||||||
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ServerLoop()
|
private static void ServerLoop()
|
||||||
@ -40,7 +143,7 @@ class Program
|
|||||||
// Main server loop
|
// Main server loop
|
||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
menuItems = [ "Benutzerverwaltung", "Einstellungen", "Beenden" ];
|
menuItems = new[] { "Benutzerverwaltung", "Einstellungen", "Beenden" };
|
||||||
itemSelector.SetTitle("Hauptmenü");
|
itemSelector.SetTitle("Hauptmenü");
|
||||||
//selectedItem = itemSelector.SelectItemFromList(menuItems);
|
//selectedItem = itemSelector.SelectItemFromList(menuItems);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user