From c523552d2f5cd90cf3621f32f6ad932f4e9e6bea Mon Sep 17 00:00:00 2001 From: Elias Jansson Date: Sat, 24 Jan 2026 15:24:10 +0100 Subject: [PATCH] Ny setup fil --- Aberwyn/Data/SetupService.cs | 50 ++++++++++++++++++++++---------- Aberwyn/Views/Setup/Index.cshtml | 2 +- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Aberwyn/Data/SetupService.cs b/Aberwyn/Data/SetupService.cs index 282e4f5..45063c2 100644 --- a/Aberwyn/Data/SetupService.cs +++ b/Aberwyn/Data/SetupService.cs @@ -4,24 +4,26 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; using System.IO; -using System.Text.Json; -using Microsoft.Extensions.Hosting; namespace Aberwyn.Data { - // SetupService.cs public class SetupService { - private readonly IWebHostEnvironment _env; private readonly string _filePath; - public SetupService(IWebHostEnvironment env) + public SetupService() { - _env = env; - _filePath = Path.Combine(_env.ContentRootPath, "infrastructure", "setup.json"); + // Persistenta data ligger i /app/data + var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); // /app/data i containern + _filePath = Path.Combine(dataRoot, "infrastructure", "setup.json"); + + // Skapa mappen om den inte finns + Directory.CreateDirectory(Path.GetDirectoryName(_filePath)!); } - + /// + /// Hämtar setup-inställningar + /// public SetupSettings GetSetup() { if (!File.Exists(_filePath)) @@ -31,12 +33,22 @@ namespace Aberwyn.Data return JsonSerializer.Deserialize(json) ?? new SetupSettings { IsConfigured = false }; } + /// + /// Sparar setup-inställningar + /// + public void SaveSetup(SetupSettings setup) + { + var json = JsonSerializer.Serialize(setup, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(_filePath, json); + } + /// + /// Temporär service provider för initial setup / EF + Identity + /// internal static IServiceProvider BuildTemporaryServices(string connectionString) { var services = new ServiceCollection(); - // Konfigurera EF + Identity services.AddDbContext(options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))); @@ -44,20 +56,28 @@ namespace Aberwyn.Data .AddEntityFrameworkStores() .AddDefaultTokenProviders(); - // Lägg till en tom konfiguration för att undvika null services.AddSingleton(new ConfigurationBuilder().Build()); - - // Valfritt: Lägg till loggning om något kräver det services.AddLogging(); return services.BuildServiceProvider(); } + /// + /// Loader-klass för enkel läsning utan instans + /// public static class SetupLoader { - public static SetupSettings Load(IHostEnvironment env) + public static SetupSettings Load() { - var path = Path.Combine(env.ContentRootPath, "infrastructure", "setup.json"); + var dataRoot = Path.Combine(Directory.GetCurrentDirectory(), "data"); + var path = Path.Combine(dataRoot, "infrastructure", "setup.json"); + + // Skapa mappen om den inte finns + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + + if (!File.Exists(path)) + return new SetupSettings { IsConfigured = false }; + var json = File.ReadAllText(path); return JsonSerializer.Deserialize(json)!; } @@ -67,7 +87,5 @@ namespace Aberwyn.Data return $"server={setup.DbHost};port={setup.DbPort};database={setup.DbName};user={setup.DbUser};password={setup.DbPassword}"; } } - } - } diff --git a/Aberwyn/Views/Setup/Index.cshtml b/Aberwyn/Views/Setup/Index.cshtml index 6a091aa..5f1abf6 100644 --- a/Aberwyn/Views/Setup/Index.cshtml +++ b/Aberwyn/Views/Setup/Index.cshtml @@ -340,7 +340,7 @@ function updateCriteria(id, isValid) { } }); - async function submitSetup() { + async function submitSetup() { goToStep(3); const form = document.getElementById('setup-form'); const formData = new FormData(form);