implement startup database loading (path still hardcoded)

This commit is contained in:
Torben Nehmer 2024-09-20 19:01:09 +02:00
parent 275e6de228
commit 63761001fa
No known key found for this signature in database
4 changed files with 94 additions and 16 deletions

View File

@ -0,0 +1,23 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace StarsAssistant.Model;
public class Game
{
/// <summary>
/// Dummy primary key, normally not used, but makes working with EF easier.
/// </summary>
public int GameId { get; set; }
/// <summary>
/// The base path in which all game files reside.
/// </summary>
public string GamePath { get; set; } = String.Empty;
/// <summary>
/// The base name without extensions of your game, inside the GamePath folder.
/// All dependant files are resolved using this name.
/// </summary>
public string BaseName { get; set; } = String.Empty;
}

View File

@ -37,7 +37,7 @@ public class StarsDatabase(string DbPath) : DbContext
/// <summary> /// <summary>
/// The record with all game metadata, will only contain a single line at all times. /// The record with all game metadata, will only contain a single line at all times.
/// </summary> /// </summary>
// public DbSet<Game> Game { get; set; } public DbSet<Game> Game { get; set; }
#endregion #endregion
} }

View File

@ -25,17 +25,35 @@ sealed class Program
var logger = new ConsoleLogger() { Level = LogLevel.Debug }; var logger = new ConsoleLogger() { Level = LogLevel.Debug };
Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger)); Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger));
Services.Game g = new() bool newGame = false;
string dbPath = "/home/torben/Nextcloud/Documents/Stars!/Games/goingth/GOINGTH.sqlite";
Services.Game gameSvc;
using (StarsDatabase starsDB = new(dbPath))
{
if (Path.Exists(dbPath))
{
Model.Game dbGame = starsDB.Game.First();
gameSvc = new Services.Game(dbGame);
}
else
{
starsDB.Database.EnsureCreated();
gameSvc = new()
{ {
BaseName = "GOINGTH", BaseName = "GOINGTH",
GamePath = "/home/torben/goingth/" GamePath = "/home/torben/Nextcloud/Documents/Stars!/Games/goingth/"
}; };
gameSvc.SaveToDatabase(starsDB);
newGame = true;
}
}
Locator.CurrentMutable.RegisterConstant(g, typeof(Services.Game)); Locator.CurrentMutable.RegisterConstant(gameSvc, typeof(Services.Game));
Locator.CurrentMutable.RegisterConstant(new Services.CSVDataLoader(), typeof(Services.CSVDataLoader));
Locator.CurrentMutable.Register(() => new StarsDatabase(g.DatabaseFileName), typeof(StarsDatabase));
Locator.CurrentMutable.RegisterConstant(new Services.CSVDataLoader(), typeof(Services.CSVDataLoader)); Locator.CurrentMutable.RegisterConstant(new Services.CSVDataLoader(), typeof(Services.CSVDataLoader));
Locator.CurrentMutable.Register(() => new StarsDatabase(gameSvc.DatabaseFileName), typeof(StarsDatabase));
if (newGame)
__createTestData(); __createTestData();
BuildAvaloniaApp() BuildAvaloniaApp()
@ -53,12 +71,8 @@ sealed class Program
public static void __createTestData () public static void __createTestData ()
{ {
Services.Game game = Locator.Current.GetService<Services.Game>()!;
using var db = Locator.Current.GetService<StarsDatabase>()!; using var db = Locator.Current.GetService<StarsDatabase>()!;
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Note: This sample requires the database to be created before running. // Note: This sample requires the database to be created before running.
// Console.WriteLine($"Database path: {db.DbPath}."); // Console.WriteLine($"Database path: {db.DbPath}.");

View File

@ -1,17 +1,58 @@
using Splat;
namespace StarsAssistant.Services; namespace StarsAssistant.Services;
public class Game public class Game
{ {
/// <summary>
/// Default constructor, initialize required fields manually, targeted
/// for Initial DB creation.
/// </summary>
public Game() {}
/// <summary>
/// Init game state from database, targeted for CLI arg.
/// </summary>
/// <param name="dbGame">Record loaded from DB</param>
public Game(Model.Game dbGame)
{
GamePath = dbGame.GamePath;
BaseName = dbGame.BaseName;
}
/// <summary>
/// Save this record in the database. Uses the service locator to access the
/// database unless you specify an instance. This is needed during initial
/// game creation, where the services are not yet established.
/// </summary>
/// <param name="db">Optional DB instance if services are yet unavailable.</param>
public void SaveToDatabase(Model.StarsDatabase? db = null)
{
db ??= Locator.Current.GetService<Model.StarsDatabase>()!;
Model.Game? dbGame = db.Game.FirstOrDefault();
if (dbGame == null)
{
dbGame = new Model.Game();
db.Add(dbGame);
db.SaveChanges();
}
dbGame.GamePath = GamePath;
dbGame.BaseName = BaseName;
db.Update(dbGame);
db.SaveChanges();
}
/// <summary> /// <summary>
/// The base path in which all game files reside. /// The base path in which all game files reside.
/// </summary> /// </summary>
public required string GamePath { get; set; } public string GamePath { get; set; } = String.Empty;
/// <summary> /// <summary>
/// The base name without extensions of your game, inside the GamePath folder. /// The base name without extensions of your game, inside the GamePath folder.
/// All dependant files are resolved using this name. /// All dependant files are resolved using this name.
/// </summary> /// </summary>
public required string BaseName { get; set; } public string BaseName { get; set; } = String.Empty;
/// <summary> /// <summary>
/// Combine into the DatabaseName /// Combine into the DatabaseName