2023-09-10 21:32:25 +02:00
|
|
|
using System;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2023-10-08 00:46:42 +02:00
|
|
|
using SharpRSS.API.Models;
|
2023-09-10 21:32:25 +02:00
|
|
|
using ToolQit;
|
|
|
|
using ToolQit.Logging;
|
|
|
|
|
|
|
|
namespace SharpRSS.API.Data
|
|
|
|
{
|
2023-10-08 00:46:42 +02:00
|
|
|
internal sealed class DbAccess : DbContext
|
2023-09-10 21:32:25 +02:00
|
|
|
{
|
|
|
|
public DbAccess(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
_log = LogManager.CreateLogger(typeof(DbAccess));
|
2023-10-08 00:46:42 +02:00
|
|
|
_log.Debug("Initializing new context...");
|
2023-09-10 21:32:25 +02:00
|
|
|
_configuration = configuration;
|
2023-10-08 00:46:42 +02:00
|
|
|
_dbServer = _configuration["DataBase:Server"] ?? "Unknown";
|
|
|
|
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
|
|
|
ChangeTracker.LazyLoadingEnabled = false;
|
2023-09-10 21:32:25 +02:00
|
|
|
Database.EnsureCreated();
|
|
|
|
}
|
|
|
|
|
2023-10-08 00:46:42 +02:00
|
|
|
public DbSet<DbUser> Users { get; set; }
|
|
|
|
public DbSet<DbGroup> Groups { get; set; }
|
|
|
|
public DbSet<DbSession> Sessions { get; set; }
|
2023-09-10 21:32:25 +02:00
|
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly ILog _log;
|
2023-10-08 00:46:42 +02:00
|
|
|
private readonly string _dbServer;
|
|
|
|
|
2023-09-10 21:32:25 +02:00
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
{
|
|
|
|
string connection = _configuration["DataBase:Connection"] ?? throw new ArgumentNullException(nameof(_configuration), "No connection string in appsettings!");
|
2023-10-08 00:46:42 +02:00
|
|
|
switch (_dbServer)
|
2023-09-10 21:32:25 +02:00
|
|
|
{
|
|
|
|
case "MariaDB":
|
|
|
|
var dbSrvVersion = ServerVersion.AutoDetect(connection);
|
|
|
|
_log.Information("Server {SrvType} detected, version: {SrvVersion}", dbSrvVersion.Type.ToString(), dbSrvVersion.Version);
|
|
|
|
optionsBuilder.UseMySql(connection, dbSrvVersion);
|
|
|
|
break;
|
|
|
|
default: // TODO: Add more database support.
|
2023-10-08 00:46:42 +02:00
|
|
|
_log.Warning("No valid db server: {Server}\nSupported db servers: 'MariaDB'", _dbServer);
|
2023-09-10 21:32:25 +02:00
|
|
|
throw new Exception("Database server not specified!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
{
|
2023-10-08 00:46:42 +02:00
|
|
|
switch (_dbServer)
|
|
|
|
{
|
|
|
|
case "MariaDB":
|
|
|
|
modelBuilder.UseCollation("utf8mb4_unicode_520_ci");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
modelBuilder.Entity<DbUser>().ToTable("srss_users").HasKey(dbu => dbu.Uid);
|
|
|
|
modelBuilder.Entity<DbGroup>().ToTable("srss_groups").HasKey(dbg => dbg.Gid);
|
|
|
|
modelBuilder.Entity<DbSession>().ToTable("srss_sessions").HasKey(dbs => dbs.Sid);
|
2023-09-10 21:32:25 +02:00
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|