diff --git a/src/Nadeko.Bot.Common/Extensions/DbExtensions.cs b/src/Nadeko.Bot.Common/Extensions/DbExtensions.cs index 1731b5157..08fe9426a 100644 --- a/src/Nadeko.Bot.Common/Extensions/DbExtensions.cs +++ b/src/Nadeko.Bot.Common/Extensions/DbExtensions.cs @@ -7,6 +7,6 @@ namespace NadekoBot.Extensions; public static class DbExtensions { - public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original, Func, IQueryable>? includes = null) + public static DiscordUser GetOrCreateUser(this NadekoBaseContext ctx, IUser original, Func, IQueryable>? includes = null) => ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes); } \ No newline at end of file diff --git a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs index ae4c603e9..cb2ecc98c 100644 --- a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs +++ b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs @@ -3,7 +3,6 @@ using LinqToDB; using LinqToDB.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using NadekoBot.Db.Models; -using NadekoBot.Services.Database; namespace NadekoBot.Db; @@ -15,12 +14,12 @@ public static class DiscordUserExtensions => set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId); public static void EnsureUserCreated( - this NadekoContext ctx, + this DbContext ctx, ulong userId, string username, string discrim, string avatarId) - => ctx.DiscordUser.ToLinqToDBTable() + => ctx.GetTable() .InsertOrUpdate( () => new() { @@ -43,10 +42,9 @@ public static class DiscordUserExtensions }); public static Task EnsureUserCreatedAsync( - this NadekoContext ctx, + this NadekoBaseContext ctx, ulong userId) - => ctx.DiscordUser - .ToLinqToDBTable() + => ctx.GetTable() .InsertOrUpdateAsync( () => new() { @@ -68,7 +66,7 @@ public static class DiscordUserExtensions //temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown public static DiscordUser GetOrCreateUser( - this NadekoContext ctx, + this NadekoBaseContext ctx, ulong userId, string username, string discrim, @@ -77,7 +75,7 @@ public static class DiscordUserExtensions { ctx.EnsureUserCreated(userId, username, discrim, avatarId); - IQueryable queryable = ctx.DiscordUser; + IQueryable queryable = ctx.Set(); if (includes is not null) queryable = includes(queryable); return queryable.First(u => u.UserId == userId); diff --git a/src/Nadeko.Bot.Db/Extensions/GuildConfigExtensions.cs b/src/Nadeko.Bot.Db/Extensions/GuildConfigExtensions.cs index a75c2bb20..df55da37d 100644 --- a/src/Nadeko.Bot.Db/Extensions/GuildConfigExtensions.cs +++ b/src/Nadeko.Bot.Db/Extensions/GuildConfigExtensions.cs @@ -29,12 +29,12 @@ public static class GuildConfigExtensions /// Db Context /// Id of the guild to get stream role settings for. /// Guild'p stream role settings - public static StreamRoleSettings GetStreamRoleSettings(this NadekoContext ctx, ulong guildId) + public static StreamRoleSettings GetStreamRoleSettings(this NadekoBaseContext ctx, ulong guildId) { var conf = ctx.GuildConfigsForId(guildId, set => set.Include(y => y.StreamRole) - .Include(y => y.StreamRole.Whitelist) - .Include(y => y.StreamRole.Blacklist)); + .Include(y => y.StreamRole.Whitelist) + .Include(y => y.StreamRole.Blacklist)); if (conf.StreamRole is null) conf.StreamRole = new(); @@ -44,13 +44,13 @@ public static class GuildConfigExtensions private static IQueryable IncludeEverything(this DbSet configs) => configs.AsQueryable() - .AsSplitQuery() - .Include(gc => gc.CommandCooldowns) - .Include(gc => gc.FollowedStreams) - .Include(gc => gc.StreamRole) - .Include(gc => gc.XpSettings) - .ThenInclude(x => x.ExclusionList) - .Include(gc => gc.DelMsgOnCmdChannels); + .AsSplitQuery() + .Include(gc => gc.CommandCooldowns) + .Include(gc => gc.FollowedStreams) + .Include(gc => gc.StreamRole) + .Include(gc => gc.XpSettings) + .ThenInclude(x => x.ExclusionList) + .Include(gc => gc.DelMsgOnCmdChannels); public static IEnumerable GetAllGuildConfigs( this DbSet configs, @@ -65,23 +65,23 @@ public static class GuildConfigExtensions /// Use to manipulate the set however you want. Pass null to include everything /// Config for the guild public static GuildConfig GuildConfigsForId( - this NadekoContext ctx, + this NadekoBaseContext ctx, ulong guildId, Func, IQueryable> includes) { GuildConfig config; if (includes is null) - config = ctx.GuildConfigs.IncludeEverything().FirstOrDefault(c => c.GuildId == guildId); + config = ctx.Set().IncludeEverything().FirstOrDefault(c => c.GuildId == guildId); else { - var set = includes(ctx.GuildConfigs); + var set = includes(ctx.Set()); config = set.FirstOrDefault(c => c.GuildId == guildId); } if (config is null) { - ctx.GuildConfigs.Add(config = new() + ctx.Set().Add(config = new() { GuildId = guildId, Permissions = Permissionv2.GetDefaultPermlist, @@ -120,19 +120,21 @@ public static class GuildConfigExtensions // .First(x => x.GuildId == guildId); } - public static LogSetting LogSettingsFor(this NadekoContext ctx, ulong guildId) + public static LogSetting LogSettingsFor(this NadekoBaseContext ctx, ulong guildId) { - var logSetting = ctx.LogSettings.AsQueryable() - .Include(x => x.LogIgnores) - .Where(x => x.GuildId == guildId) - .FirstOrDefault(); + var logSetting = ctx.Set() + .AsQueryable() + .Include(x => x.LogIgnores) + .Where(x => x.GuildId == guildId) + .FirstOrDefault(); if (logSetting is null) { - ctx.LogSettings.Add(logSetting = new() - { - GuildId = guildId - }); + ctx.Set() + .Add(logSetting = new() + { + GuildId = guildId + }); ctx.SaveChanges(); } @@ -146,16 +148,16 @@ public static class GuildConfigExtensions return query.ToList(); } - public static GuildConfig GcWithPermissionsFor(this NadekoContext ctx, ulong guildId) + public static GuildConfig GcWithPermissionsFor(this NadekoBaseContext ctx, ulong guildId) { - var config = ctx.GuildConfigs.AsQueryable() - .Where(gc => gc.GuildId == guildId) - .Include(gc => gc.Permissions) - .FirstOrDefault(); + var config = ctx.Set().AsQueryable() + .Where(gc => gc.GuildId == guildId) + .Include(gc => gc.Permissions) + .FirstOrDefault(); if (config is null) // if there is no guildconfig, create new one { - ctx.GuildConfigs.Add(config = new() + ctx.Set().Add(config = new() { GuildId = guildId, Permissions = Permissionv2.GetDefaultPermlist @@ -176,10 +178,10 @@ public static class GuildConfigExtensions public static IEnumerable GetFollowedStreams(this DbSet configs, List included) => configs.AsQueryable() - .Where(gc => included.Contains(gc.GuildId)) - .Include(gc => gc.FollowedStreams) - .SelectMany(gc => gc.FollowedStreams) - .ToList(); + .Where(gc => included.Contains(gc.GuildId)) + .Include(gc => gc.FollowedStreams) + .SelectMany(gc => gc.FollowedStreams) + .ToList(); public static void SetCleverbotEnabled(this DbSet configs, ulong id, bool cleverbotEnabled) { @@ -191,15 +193,15 @@ public static class GuildConfigExtensions conf.CleverbotEnabled = cleverbotEnabled; } - public static XpSettings XpSettingsFor(this NadekoContext ctx, ulong guildId) + public static XpSettings XpSettingsFor(this NadekoBaseContext ctx, ulong guildId) { var gc = ctx.GuildConfigsForId(guildId, set => set.Include(x => x.XpSettings) - .ThenInclude(x => x.RoleRewards) - .Include(x => x.XpSettings) - .ThenInclude(x => x.CurrencyRewards) - .Include(x => x.XpSettings) - .ThenInclude(x => x.ExclusionList)); + .ThenInclude(x => x.RoleRewards) + .Include(x => x.XpSettings) + .ThenInclude(x => x.CurrencyRewards) + .Include(x => x.XpSettings) + .ThenInclude(x => x.ExclusionList)); if (gc.XpSettings is null) gc.XpSettings = new(); @@ -209,15 +211,15 @@ public static class GuildConfigExtensions public static IEnumerable GetGeneratingChannels(this DbSet configs) => configs.AsQueryable() - .Include(x => x.GenerateCurrencyChannelIds) - .Where(x => x.GenerateCurrencyChannelIds.Any()) - .SelectMany(x => x.GenerateCurrencyChannelIds) - .Select(x => new GeneratingChannel - { - ChannelId = x.ChannelId, - GuildId = x.GuildConfig.GuildId - }) - .ToArray(); + .Include(x => x.GenerateCurrencyChannelIds) + .Where(x => x.GenerateCurrencyChannelIds.Any()) + .SelectMany(x => x.GenerateCurrencyChannelIds) + .Select(x => new GeneratingChannel + { + ChannelId = x.ChannelId, + GuildId = x.GuildConfig.GuildId + }) + .ToArray(); public class GeneratingChannel { diff --git a/src/Nadeko.Bot.Db/NadekoBaseContext.cs b/src/Nadeko.Bot.Db/NadekoBaseContext.cs new file mode 100644 index 000000000..04c3b484a --- /dev/null +++ b/src/Nadeko.Bot.Db/NadekoBaseContext.cs @@ -0,0 +1,7 @@ +using Microsoft.EntityFrameworkCore; + +namespace NadekoBot.Db; + +public abstract class NadekoBaseContext : DbContext +{ +} \ No newline at end of file diff --git a/src/Nadeko.Bot.Modules.Gambling/Gambling/Bank/BankService.cs b/src/Nadeko.Bot.Modules.Gambling/Gambling/Bank/BankService.cs index e4a4d0f3b..67237d9b8 100644 --- a/src/Nadeko.Bot.Modules.Gambling/Gambling/Bank/BankService.cs +++ b/src/Nadeko.Bot.Modules.Gambling/Gambling/Bank/BankService.cs @@ -1,5 +1,6 @@ using LinqToDB; using LinqToDB.EntityFrameworkCore; +using NadekoBot.Db.Models; namespace NadekoBot.Modules.Gambling.Bank; @@ -20,8 +21,7 @@ public sealed class BankService : IBankService, INService throw new ArgumentOutOfRangeException(nameof(amount)); await using var ctx = _db.GetDbContext(); - await ctx.BankUsers - .ToLinqToDBTable() + await ctx.GetTable() .InsertOrUpdateAsync(() => new() { UserId = userId, diff --git a/src/Nadeko.Bot.Modules.Utility/Nadeko.Bot.Modules.Utility.csproj b/src/Nadeko.Bot.Modules.Utility/Nadeko.Bot.Modules.Utility.csproj index 807f6287f..919f7ceab 100644 --- a/src/Nadeko.Bot.Modules.Utility/Nadeko.Bot.Modules.Utility.csproj +++ b/src/Nadeko.Bot.Modules.Utility/Nadeko.Bot.Modules.Utility.csproj @@ -7,21 +7,21 @@ - + - + - + - - + + - + diff --git a/src/Nadeko.Bot.Db/Models/CommandAlias.cs b/src/Nadeko.Bot.Modules.Utility/_Common/Db/CommandAlias.cs similarity index 100% rename from src/Nadeko.Bot.Db/Models/CommandAlias.cs rename to src/Nadeko.Bot.Modules.Utility/_Common/Db/CommandAlias.cs diff --git a/src/Nadeko.Bot.Db/MysqlContext.cs b/src/NadekoBot/Db/MysqlContext.cs similarity index 100% rename from src/Nadeko.Bot.Db/MysqlContext.cs rename to src/NadekoBot/Db/MysqlContext.cs diff --git a/src/Nadeko.Bot.Db/NadekoContext.cs b/src/NadekoBot/Db/NadekoContext.cs similarity index 100% rename from src/Nadeko.Bot.Db/NadekoContext.cs rename to src/NadekoBot/Db/NadekoContext.cs diff --git a/src/Nadeko.Bot.Db/PostgreSqlContext.cs b/src/NadekoBot/Db/PostgreSqlContext.cs similarity index 100% rename from src/Nadeko.Bot.Db/PostgreSqlContext.cs rename to src/NadekoBot/Db/PostgreSqlContext.cs diff --git a/src/Nadeko.Bot.Db/SqliteContext.cs b/src/NadekoBot/Db/SqliteContext.cs similarity index 100% rename from src/Nadeko.Bot.Db/SqliteContext.cs rename to src/NadekoBot/Db/SqliteContext.cs diff --git a/src/Nadeko.Bot.Db/Migrations/MigrationQueries.cs b/src/NadekoBot/Migrations/MigrationQueries.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/MigrationQueries.cs rename to src/NadekoBot/Migrations/MigrationQueries.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Mysql/20221122204432_feed-text.Designer.cs b/src/NadekoBot/Migrations/Mysql/20221122204432_feed-text.Designer.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Mysql/20221122204432_feed-text.Designer.cs rename to src/NadekoBot/Migrations/Mysql/20221122204432_feed-text.Designer.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Mysql/20221122204432_feed-text.cs b/src/NadekoBot/Migrations/Mysql/20221122204432_feed-text.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Mysql/20221122204432_feed-text.cs rename to src/NadekoBot/Migrations/Mysql/20221122204432_feed-text.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Mysql/MysqlContextModelSnapshot.cs b/src/NadekoBot/Migrations/Mysql/MysqlContextModelSnapshot.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Mysql/MysqlContextModelSnapshot.cs rename to src/NadekoBot/Migrations/Mysql/MysqlContextModelSnapshot.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Postgresql/20221122204423_feed-text.Designer.cs b/src/NadekoBot/Migrations/Postgresql/20221122204423_feed-text.Designer.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Postgresql/20221122204423_feed-text.Designer.cs rename to src/NadekoBot/Migrations/Postgresql/20221122204423_feed-text.Designer.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Postgresql/20221122204423_feed-text.cs b/src/NadekoBot/Migrations/Postgresql/20221122204423_feed-text.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Postgresql/20221122204423_feed-text.cs rename to src/NadekoBot/Migrations/Postgresql/20221122204423_feed-text.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Postgresql/PostgreSqlContextModelSnapshot.cs b/src/NadekoBot/Migrations/Postgresql/PostgreSqlContextModelSnapshot.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Postgresql/PostgreSqlContextModelSnapshot.cs rename to src/NadekoBot/Migrations/Postgresql/PostgreSqlContextModelSnapshot.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Sqlite/20221122204324_feed-text.Designer.cs b/src/NadekoBot/Migrations/Sqlite/20221122204324_feed-text.Designer.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Sqlite/20221122204324_feed-text.Designer.cs rename to src/NadekoBot/Migrations/Sqlite/20221122204324_feed-text.Designer.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Sqlite/20221122204324_feed-text.cs b/src/NadekoBot/Migrations/Sqlite/20221122204324_feed-text.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Sqlite/20221122204324_feed-text.cs rename to src/NadekoBot/Migrations/Sqlite/20221122204324_feed-text.cs diff --git a/src/Nadeko.Bot.Db/Migrations/Sqlite/SqliteContextModelSnapshot.cs b/src/NadekoBot/Migrations/Sqlite/SqliteContextModelSnapshot.cs similarity index 100% rename from src/Nadeko.Bot.Db/Migrations/Sqlite/SqliteContextModelSnapshot.cs rename to src/NadekoBot/Migrations/Sqlite/SqliteContextModelSnapshot.cs