Add server configuration service and repo

This commit is contained in:
Holden
2026-06-18 13:43:59 -05:00
parent 7fc7e1dc91
commit 608def3ac8
15 changed files with 272 additions and 9 deletions

View File

@@ -14,6 +14,7 @@ public static class DependencyInjection
builder.AddNpgsqlDbContext<DatabaseContext>("recnet");
builder.Services.AddScoped<IProfileRepository, ProfileRepository>();
builder.Services.AddScoped<IServerConfigRepository, ServerConfigRepository>();
return builder;
}

View File

@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using RecNet.Domain.Entities.Configuration;
namespace RecNet.Infrastructure.Persistence.Configurations;
public class ServerConfigConfiguration : IEntityTypeConfiguration<ServerConfig>
{
public void Configure(EntityTypeBuilder<ServerConfig> builder)
{
builder.HasKey(x => x.Key);
builder.Property(x => x.Key)
.HasMaxLength(128)
.IsRequired();
builder.Property(x => x.Value)
.HasColumnType("jsonb")
.IsRequired();
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using RecNet.Domain.Entities.Configuration;
using RecNet.Domain.Entities.Profiles;
namespace RecNet.Infrastructure.Persistence;
@@ -6,6 +7,7 @@ namespace RecNet.Infrastructure.Persistence;
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options)
{
public DbSet<Profile> Profiles => Set<Profile>();
public DbSet<ServerConfig> ServerConfigs => Set<ServerConfig>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{

View File

@@ -0,0 +1,81 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using RecNet.Infrastructure.Persistence;
#nullable disable
namespace RecNet.Infrastructure.Persistence.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20260618183916_ServerConfigs")]
partial class ServerConfigs
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("RecNet.Domain.Entities.Configuration.ServerConfig", b =>
{
b.Property<string>("Key")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("jsonb");
b.HasKey("Key");
b.ToTable("ServerConfigs");
});
modelBuilder.Entity("RecNet.Domain.Entities.Profiles.Profile", b =>
{
b.Property<Guid>("ProfileId")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.PrimitiveCollection<string>("DeviceIds")
.IsRequired()
.HasColumnType("jsonb");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<string>("Platform")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.Property<string>("PlatformId")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("character varying(50)");
b.HasKey("ProfileId");
b.HasIndex("Platform", "PlatformId")
.IsUnique();
b.ToTable("Profiles");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace RecNet.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class ServerConfigs : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ServerConfigs",
columns: table => new
{
Key = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Value = table.Column<string>(type: "jsonb", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ServerConfigs", x => x.Key);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ServerConfigs");
}
}
}

View File

@@ -22,6 +22,21 @@ namespace RecNet.Infrastructure.Persistence.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("RecNet.Domain.Entities.Configuration.ServerConfig", b =>
{
b.Property<string>("Key")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("jsonb");
b.HasKey("Key");
b.ToTable("ServerConfigs");
});
modelBuilder.Entity("RecNet.Domain.Entities.Profiles.Profile", b =>
{
b.Property<Guid>("ProfileId")

View File

@@ -20,7 +20,10 @@ public class ProfileRepository(DatabaseContext dbContext) : IProfileRepository
.Where(x => profileIds.Contains(x.ProfileId))
.ToListAsync(ct);
public Task<Profile?> GetByPlatform(PlatformType platform, string platformId, CancellationToken ct = default)
public Task<Profile?> GetByPlatform(
PlatformType platform,
string platformId,
CancellationToken ct = default)
=> dbContext.Profiles
.FirstOrDefaultAsync(x =>
x.Platform == platform &&

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using RecNet.Domain.Entities.Configuration;
using RecNet.Domain.Repositories;
namespace RecNet.Infrastructure.Persistence.Repositories;
public class ServerConfigRepository(DatabaseContext dbContext) : IServerConfigRepository
{
public Task<ServerConfig?> GetAsync(string key, CancellationToken ct = default)
=> dbContext.ServerConfigs
.FirstOrDefaultAsync(x => x.Key == key, ct);
public async Task SetAsync(string key, string value, CancellationToken ct = default)
{
var config = await GetAsync(key, ct);
if (config is null)
{
config = ServerConfig.Create(key, value);
await dbContext.ServerConfigs.AddAsync(config, ct);
return;
}
config.SetValue(value);
}
public Task SaveChangesAsync(CancellationToken ct = default)
=> dbContext.SaveChangesAsync(ct);
}