Finished Settings page

This commit is contained in:
2025-11-12 17:06:14 +01:00
parent a608f98497
commit e0bf95060a
38 changed files with 675 additions and 34 deletions

View File

@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WorkTime.Models;
namespace WorkTime.Database;
internal sealed class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
public DbSet<TimeEntry> Entries { get; set; }
}
internal sealed class DbMigrationService(IServiceProvider provider) : IHostedService {
public async Task StartAsync(CancellationToken cancellationToken) {
await using var scope = provider.CreateAsyncScope();
var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
await context.Database.MigrateAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

View File

@@ -0,0 +1,42 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WorkTime.Database;
#nullable disable
namespace WorkTime.Database.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20251112121330_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.0");
modelBuilder.Entity("WorkTime.Models.TimeEntry", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Entries");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WorkTime.Database.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Entries",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Type = table.Column<int>(type: "INTEGER", nullable: false),
Timestamp = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Entries", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Entries");
}
}
}

View File

@@ -0,0 +1,39 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WorkTime.Database;
#nullable disable
namespace WorkTime.Database.Migrations
{
[DbContext(typeof(DatabaseContext))]
partial class DatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.0");
modelBuilder.Entity("WorkTime.Models.TimeEntry", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Entries");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,28 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using WorkTime.Models;
using WorkTime.Models.Repositories;
namespace WorkTime.Database.Repositories;
internal sealed class SettingsRepository(string filePath) : ISettingsRepository {
public async Task<Settings> LoadSettings() {
if (!File.Exists(filePath))
return new Settings();
var raw = await File.ReadAllTextAsync(filePath);
return JsonSerializer.Deserialize<Settings>(raw, SettingsSerializer.Default.Settings) ?? new Settings();
}
public async Task SaveSettings(Settings settings) {
if (File.Exists(filePath))
File.Delete(filePath);
var json = JsonSerializer.Serialize(settings, SettingsSerializer.Default.Settings);
await File.WriteAllTextAsync(filePath, json);
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Settings))]
internal partial class SettingsSerializer : JsonSerializerContext {}

View File

@@ -0,0 +1,22 @@
using WorkTime.Models;
using WorkTime.Models.Repositories;
namespace WorkTime.Database.Repositories;
internal sealed class TimeEntryRepository : ITimeEntryRepository {
public Task<IEnumerable<TimeEntry>> GetTimeEntries() {
throw new NotImplementedException();
}
public Task<IEnumerable<TimeEntry>> GetTimeEntries(DateOnly date) {
throw new NotImplementedException();
}
public Task AddTimeEntry(TimeEntry entry) {
throw new NotImplementedException();
}
public Task DeleteTimeEntry(Guid id) {
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using WorkTime.Database.Repositories;
using WorkTime.Models.Repositories;
namespace WorkTime.Database;
public static class ServiceCollectionExtensions {
public static void AddDatabase(this IServiceCollection services, string basePath) {
services.AddSqlite<DatabaseContext>($"Filename={Path.Combine(basePath, "data.db")}");
services.AddHostedService<DbMigrationService>();
services.AddTransient<ITimeEntryRepository, TimeEntryRepository>();
services.AddTransient<ISettingsRepository, SettingsRepository>(_ => new SettingsRepository(Path.Combine(basePath, "settings.json")));
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WorkTime.Models\WorkTime.Models.csproj" />
</ItemGroup>
</Project>