Update `User` Model For Currency System

Refactor ``DataContext``

REQUIRES DB UPDATE
This commit is contained in:
Alan Moon 2025-06-19 13:35:04 -07:00
parent 7a2f04dee9
commit eb0b957020
4 changed files with 26 additions and 28 deletions

View File

@ -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<RefreshToken> ValidRefreshTokens { get; set; }
public DbSet<Contact> 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

View File

@ -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<RefreshToken>? RefreshTokens { get; }
public virtual IEnumerable<Contact>? ContactsMade { get; }

View File

@ -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<DataContext>(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 =>

View File

@ -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"
},