From c5cd772126afd480dea82d80508f3c496d140345 Mon Sep 17 00:00:00 2001 From: Toastie Date: Sat, 29 Jun 2024 17:50:16 +1200 Subject: [PATCH] Updated Db models --- src/EllieBot/Db/EllieContext.cs | 52 +++++++++---------- src/EllieBot/Db/EllieDbService.cs | 10 ++-- src/EllieBot/Db/LevelStats.cs | 1 + src/EllieBot/Db/Models/DiscordUser.cs | 1 + src/EllieBot/Db/Models/HoneypotChannel.cs | 4 +- .../Db/Models/anti/AntiRaidSetting.cs | 1 + src/EllieBot/Db/Models/support/PatronQuota.cs | 6 +-- 7 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/EllieBot/Db/EllieContext.cs b/src/EllieBot/Db/EllieContext.cs index 8f5c2d8..860e834 100644 --- a/src/EllieBot/Db/EllieContext.cs +++ b/src/EllieBot/Db/EllieContext.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable disable using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using EllieBot.Db.Models; @@ -149,7 +149,7 @@ public abstract class EllieContext : DbContext .OnDelete(DeleteBehavior.Cascade); // start antispam - + modelBuilder.Entity() .HasOne(x => x.AntiSpamSetting) .WithOne() @@ -282,10 +282,10 @@ public abstract class EllieContext : DbContext var selfassignableRolesEntity = modelBuilder.Entity(); selfassignableRolesEntity.HasIndex(s => new - { - s.GuildId, - s.RoleId - }) + { + s.GuildId, + s.RoleId + }) .IsUnique(); selfassignableRolesEntity.Property(x => x.Group).HasDefaultValue(0); @@ -358,10 +358,10 @@ public abstract class EllieContext : DbContext var xps = modelBuilder.Entity(); xps.HasIndex(x => new - { - x.UserId, - x.GuildId - }) + { + x.UserId, + x.GuildId + }) .IsUnique(); xps.HasIndex(x => x.UserId); @@ -407,9 +407,9 @@ public abstract class EllieContext : DbContext .OnDelete(DeleteBehavior.SetNull); ci.HasIndex(x => new - { - x.Name - }) + { + x.Name + }) .IsUnique(); #endregion @@ -528,10 +528,10 @@ public abstract class EllieContext : DbContext .IsUnique(false); rr2.HasIndex(x => new - { - x.MessageId, - x.Emote - }) + { + x.MessageId, + x.Emote + }) .IsUnique(); }); @@ -606,11 +606,11 @@ public abstract class EllieContext : DbContext { // user can own only one of each item x.HasIndex(model => new - { - model.UserId, - model.ItemType, - model.ItemKey - }) + { + model.UserId, + model.ItemType, + model.ItemKey + }) .IsUnique(); }); @@ -635,10 +635,10 @@ public abstract class EllieContext : DbContext #region Sticky Roles modelBuilder.Entity(sr => sr.HasIndex(x => new - { - x.GuildId, - x.UserId - }) + { + x.GuildId, + x.UserId + }) .IsUnique()); #endregion diff --git a/src/EllieBot/Db/EllieDbService.cs b/src/EllieBot/Db/EllieDbService.cs index 2a3382e..52e0bf9 100644 --- a/src/EllieBot/Db/EllieDbService.cs +++ b/src/EllieBot/Db/EllieDbService.cs @@ -4,14 +4,14 @@ using Microsoft.EntityFrameworkCore; namespace EllieBot.Db; -public sealed class EllieDbService : DbService +public sealed class EllieDbService : DbService { private readonly IBotCredsProvider _creds; // these are props because creds can change at runtime private string DbType => _creds.GetCreds().Db.Type.ToLowerInvariant().Trim(); private string ConnString => _creds.GetCreds().Db.ConnectionString; - + public EllieDbService(IBotCredsProvider creds) { LinqToDBForEFTools.Initialize(); @@ -26,13 +26,13 @@ public sealed class EllieDbService : DbService var connString = ConnString; await using var context = CreateRawDbContext(dbType, connString); - + // make sure sqlite db is in wal journal mode if (context is SqliteContext) { await context.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL"); } - + await context.Database.MigrateAsync(); } @@ -52,7 +52,7 @@ public sealed class EllieDbService : DbService throw new NotSupportedException($"The database provide type of '{dbType}' is not supported."); } } - + private EllieContext GetDbContextInternal() { var dbType = DbType; diff --git a/src/EllieBot/Db/LevelStats.cs b/src/EllieBot/Db/LevelStats.cs index 0f3e02c..25a3936 100644 --- a/src/EllieBot/Db/LevelStats.cs +++ b/src/EllieBot/Db/LevelStats.cs @@ -1,4 +1,5 @@ #nullable disable + namespace EllieBot.Db; public readonly struct LevelStats diff --git a/src/EllieBot/Db/Models/DiscordUser.cs b/src/EllieBot/Db/Models/DiscordUser.cs index cae2bb3..69eecb6 100644 --- a/src/EllieBot/Db/Models/DiscordUser.cs +++ b/src/EllieBot/Db/Models/DiscordUser.cs @@ -1,6 +1,7 @@ #nullable disable namespace EllieBot.Db.Models; + // FUTURE remove LastLevelUp from here and UserXpStats public class DiscordUser : DbEntity { diff --git a/src/EllieBot/Db/Models/HoneypotChannel.cs b/src/EllieBot/Db/Models/HoneypotChannel.cs index 418543a..bd074c9 100644 --- a/src/EllieBot/Db/Models/HoneypotChannel.cs +++ b/src/EllieBot/Db/Models/HoneypotChannel.cs @@ -6,6 +6,6 @@ public class HoneypotChannel { [Key] public ulong GuildId { get; set; } - + public ulong ChannelId { get; set; } -} +} \ No newline at end of file diff --git a/src/EllieBot/Db/Models/anti/AntiRaidSetting.cs b/src/EllieBot/Db/Models/anti/AntiRaidSetting.cs index 351d194..b5e5f67 100644 --- a/src/EllieBot/Db/Models/anti/AntiRaidSetting.cs +++ b/src/EllieBot/Db/Models/anti/AntiRaidSetting.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace EllieBot.Db.Models; + public class AntiRaidSetting : DbEntity { public int GuildConfigId { get; set; } diff --git a/src/EllieBot/Db/Models/support/PatronQuota.cs b/src/EllieBot/Db/Models/support/PatronQuota.cs index 688d005..d493807 100644 --- a/src/EllieBot/Db/Models/support/PatronQuota.cs +++ b/src/EllieBot/Db/Models/support/PatronQuota.cs @@ -6,12 +6,12 @@ public class PatronUser public string UniquePlatformUserId { get; set; } public ulong UserId { get; set; } public int AmountCents { get; set; } - + public DateTime LastCharge { get; set; } - + // Date Only component public DateTime ValidThru { get; set; } - + public PatronUser Clone() => new PatronUser() {