init
This commit is contained in:
commit
ded4f4db26
43 changed files with 2052 additions and 0 deletions
97
Catalogger.Backend/Database/DatabaseContext.cs
Normal file
97
Catalogger.Backend/Database/DatabaseContext.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
using Catalogger.Backend.Database.Models;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using EntityFramework.Exceptions.PostgreSQL;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql;
|
||||
|
||||
namespace Catalogger.Backend.Database;
|
||||
|
||||
public class DatabaseContext : DbContext
|
||||
{
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
private readonly ILoggerFactory? _loggerFactory;
|
||||
|
||||
public DbSet<Guild> Guilds { get; set; }
|
||||
public DbSet<Message> Messages { get; set; }
|
||||
public DbSet<IgnoredMessage> IgnoredMessages { get; set; }
|
||||
public DbSet<Invite> Invites { get; set; }
|
||||
public DbSet<Watchlist> Watchlists { get; set; }
|
||||
|
||||
public DatabaseContext(Config config, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
Timeout = config.Database.Timeout ?? 5,
|
||||
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
|
||||
}.ConnectionString;
|
||||
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
|
||||
dataSourceBuilder
|
||||
.EnableDynamicJson()
|
||||
.UseNodaTime();
|
||||
_dataSource = dataSourceBuilder.Build();
|
||||
_loggerFactory = loggerFactory;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.ConfigureWarnings(c =>
|
||||
c.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)
|
||||
.Ignore(CoreEventId.SaveChangesFailed))
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.UseLoggerFactory(_loggerFactory)
|
||||
.UseExceptionProcessor();
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
configurationBuilder.Properties<ulong>().HaveConversion<UlongValueConverter>();
|
||||
configurationBuilder.Properties<List<ulong>>().HaveConversion<UlongArrayValueConverter>();
|
||||
}
|
||||
|
||||
private static readonly ValueComparer<List<ulong>> UlongListValueComparer = new(
|
||||
(c1, c2) => c1 != null && c2 != null && c1.SequenceEqual(c2),
|
||||
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode()))
|
||||
);
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Guild>().Property(g => g.KeyRoles)
|
||||
.Metadata.SetValueComparer(UlongListValueComparer);
|
||||
|
||||
modelBuilder.Entity<Invite>().HasKey(i => i.Code);
|
||||
modelBuilder.Entity<Invite>().HasIndex(i => i.GuildId);
|
||||
|
||||
modelBuilder.Entity<Watchlist>().HasKey(w => new { w.GuildId, w.UserId });
|
||||
modelBuilder.Entity<Watchlist>().Property(w => w.AddedAt).HasDefaultValueSql("now()");
|
||||
}
|
||||
}
|
||||
|
||||
public class DesignTimeDatabaseContextFactory : IDesignTimeDbContextFactory<DatabaseContext>
|
||||
{
|
||||
public DatabaseContext CreateDbContext(string[] args)
|
||||
{
|
||||
// Read the configuration file
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddConfiguration()
|
||||
.Build()
|
||||
// Get the configuration as our config class
|
||||
.Get<Config>() ?? new();
|
||||
|
||||
return new DatabaseContext(config, null);
|
||||
}
|
||||
}
|
||||
|
||||
public class UlongValueConverter() : ValueConverter<ulong, long>(
|
||||
convertToProviderExpression: x => (long)x,
|
||||
convertFromProviderExpression: x => (ulong)x
|
||||
);
|
||||
|
||||
public class UlongArrayValueConverter() : ValueConverter<List<ulong>, List<long>>(
|
||||
convertToProviderExpression: x => x.Select(i => (long)i).ToList(),
|
||||
convertFromProviderExpression: x => x.Select(i => (ulong)i).ToList()
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue