From 63a38297c72dd444e567b1457035ae15d31158a8 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Mon, 16 Feb 2026 12:06:40 -0800 Subject: [PATCH] Create DbDriver and DI the driver + the python interface --- WebServer/Controllers/Database.cs | 80 +++++++++++++++++++++++++++++++ WebServer/Program.cs | 28 ++++++----- WebServer/WebServer.csproj | 1 + 3 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 WebServer/Controllers/Database.cs diff --git a/WebServer/Controllers/Database.cs b/WebServer/Controllers/Database.cs new file mode 100644 index 00000000..a97a7fcd --- /dev/null +++ b/WebServer/Controllers/Database.cs @@ -0,0 +1,80 @@ +using Microsoft.Data.Sqlite; + +namespace DataBase { + + public class DbDriver : IDisposable { + + static SqliteConnection? singletonConnector = null; + + public DbDriver() { + // Load in the datastore if not already loaded + if (singletonConnector == null) { + singletonConnector = new SqliteConnection("Data Source=test.db"); + singletonConnector.Open(); + + // Create the key value store if not exist + using var command = singletonConnector.CreateCommand(); + command.CommandText = """ + CREATE TABLE IF NOT EXISTS KeyValuePair ( + key TEXT NOT NULL, + value TEXT, + PRIMARY KEY(key) + ); + """; + command.ExecuteReader(); + } + } + + // Return Values from keys + public string Get(string Key) { + if (singletonConnector != null) { + using var command = singletonConnector.CreateCommand(); + command.CommandText = """ + SELECT value + FROM KeyValuePair + Where key = $key; + """; + command.Parameters.AddWithValue("$key", Key); + command.ExecuteReader(); + using var reader = command.ExecuteReader(); + if (reader.Read()) { + return reader.GetString(0); + } + } + return ""; + } + + // Set a key value pair in the store + public void Set(string Key, string Value) { + if (singletonConnector != null) { + using var command = singletonConnector.CreateCommand(); + command.CommandText = """ + INSERT INTO KeyValuePair + (key,value) + VALUES + ($key, $value) + ON CONFLICT(key) + DO UPDATE + SET value = excluded.value; + """; + command.Parameters.AddWithValue("$key", Key); + command.Parameters.AddWithValue("$value", Value); + command.ExecuteReader(); + } + } + + // Deconstructor + ~DbDriver() { + Dispose(); + } + + // Deconstructor + public void Dispose() { + if (singletonConnector != null) { + singletonConnector.Close(); + singletonConnector = null; + } + } + } + +} \ No newline at end of file diff --git a/WebServer/Program.cs b/WebServer/Program.cs index a12c2d88..43c5c10c 100644 --- a/WebServer/Program.cs +++ b/WebServer/Program.cs @@ -1,11 +1,24 @@ using WebServer.Components; using PythonInterop; +using DataBase; var builder = WebApplication.CreateBuilder(args); +// Load the module in globally and use correct path for local or docker runners +#if DEBUG + AIModule interopModule = new AIModule(PythonPathBase: "/usr/", PythonVersion: "python3.11"); +#else + AIModule interopModule = new AIModule(); +#endif + // Add services to the container. -builder.Services.AddRazorComponents() - .AddInteractiveServerComponents(); +builder.Services.AddRazorComponents().AddInteractiveServerComponents(); + +// Insert the DB driver for Dependency Injection +builder.Services.AddSingleton(); + +// Insert the python modlue for Dependency Injection +builder.Services.AddSingleton(interopModule); var app = builder.Build(); @@ -16,17 +29,6 @@ if (!app.Environment.IsDevelopment()) { app.UseHsts(); } -// Load the module in globally and use correct path for local or docker runners -#if DEBUG - AIModule interopModule = new AIModule(PythonPathBase: "/usr/", PythonVersion: "python3.11"); -#else - AIModule interopModule = new AIModule(); -#endif - -// Run this for testing purposes -interopModule.TestAI(); - - app.UseHttpsRedirection(); app.UseAntiforgery(); diff --git a/WebServer/WebServer.csproj b/WebServer/WebServer.csproj index bd30b0f1..74369535 100644 --- a/WebServer/WebServer.csproj +++ b/WebServer/WebServer.csproj @@ -7,6 +7,7 @@ +