From eb0b957020e24cb9674d87feeb2d5c256616b23d Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Thu, 19 Jun 2025 13:35:04 -0700 Subject: [PATCH] Update ``User`` Model For Currency System Refactor ``DataContext`` REQUIRES DB UPDATE --- qtc-net-server/Data/DataContext.cs | 25 ++++++++++++++++++++- qtc-net-server/Models/User.cs | 2 ++ qtc-net-server/Program.cs | 23 ------------------- qtc-net-server/appsettings.Development.json | 4 ---- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/qtc-net-server/Data/DataContext.cs b/qtc-net-server/Data/DataContext.cs index 8e5ab31..7780ea0 100644 --- a/qtc-net-server/Data/DataContext.cs +++ b/qtc-net-server/Data/DataContext.cs @@ -1,4 +1,7 @@ -namespace qtc_api.Data +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.Extensions.Options; + +namespace qtc_api.Data { public class DataContext : DbContext { @@ -11,6 +14,26 @@ public DbSet ValidRefreshTokens { get; set; } public DbSet Contacts { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + var dbProvider = Environment.GetEnvironmentVariable("DB_PROVIDER"); + var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING"); + + if (dbProvider != null && connectionString != null) + { + switch (dbProvider) + { + case "MySQL": + options.UseMySQL(connectionString); + break; + case "SQLite": + options.UseSqlite(connectionString); + break; + default: throw new Exception("Unsupported Database Provider. Please Check 'DB_PROVIDER' Environment Variable."); + } + } else throw new Exception("Cannot Find Environment Variables 'DB_PROVIDER' And 'DB_CONNECTION_STRING'. Please Check Environment."); + } + protected override void OnModelCreating(ModelBuilder builder) { // Users diff --git a/qtc-net-server/Models/User.cs b/qtc-net-server/Models/User.cs index 39cb98c..c2342dd 100644 --- a/qtc-net-server/Models/User.cs +++ b/qtc-net-server/Models/User.cs @@ -12,6 +12,8 @@ public DateTime DateOfBirth { get; set; } public DateTime CreatedAt { get; set; } public int Status { get; set; } = 0; + public int CurrencyAmount { get; set; } = 0; + public DateTime LastCurrencySpin { get; set; } public virtual IEnumerable? RefreshTokens { get; } public virtual IEnumerable? ContactsMade { get; } diff --git a/qtc-net-server/Program.cs b/qtc-net-server/Program.cs index 609bafd..5cbadf4 100644 --- a/qtc-net-server/Program.cs +++ b/qtc-net-server/Program.cs @@ -20,29 +20,6 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddDbContext(options => -{ - if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DB_PROVIDER"))) - { - switch(Environment.GetEnvironmentVariable("DB_PROVIDER")) - { - case "MySQL": - options.UseMySQL(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")!); - break; - case "SQLite": - options.UseSqlite(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")!); - break; - } - - return; - } - - if (builder.Environment.IsProduction()) options.UseMySQL(builder.Configuration.GetConnectionString("DefaultConnection")!); - else options.UseSqlite(builder.Configuration.GetConnectionString("DevelopmentConnection")); - - // ignore pending model changes warning - options.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); -}); builder.Services.AddSignalR(); builder.Services.AddAuthentication().AddJwtBearer(options => diff --git a/qtc-net-server/appsettings.Development.json b/qtc-net-server/appsettings.Development.json index 479ad2b..05d6270 100644 --- a/qtc-net-server/appsettings.Development.json +++ b/qtc-net-server/appsettings.Development.json @@ -1,13 +1,9 @@ { "Jwt": { - "Key": "bgpLLhY2L2UeZN3sj6WwSzScFmY3JgWfs33ZEJNcaPzC2TEnfZz", "Issuer": "http://localhost", "Audience": "http://localhost", "DefaultUserRole": "User" }, - "ConnectionStrings": { - "DevelopmentConnection": "Data Source=qtcdev.db" - }, "GeneralConfig": { "CDNPath": "./user-content" },