This commit is contained in:
Kwoth
2023-04-24 17:00:59 +02:00
parent ec2b7b3ad6
commit b34cd534c3
21 changed files with 72 additions and 65 deletions

View File

@@ -7,6 +7,6 @@ namespace NadekoBot.Extensions;
public static class DbExtensions public static class DbExtensions
{ {
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>>? includes = null) public static DiscordUser GetOrCreateUser(this NadekoBaseContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>>? includes = null)
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes); => ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes);
} }

View File

@@ -3,7 +3,6 @@ using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models; using NadekoBot.Db.Models;
using NadekoBot.Services.Database;
namespace NadekoBot.Db; namespace NadekoBot.Db;
@@ -15,12 +14,12 @@ public static class DiscordUserExtensions
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId); => set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated( public static void EnsureUserCreated(
this NadekoContext ctx, this DbContext ctx,
ulong userId, ulong userId,
string username, string username,
string discrim, string discrim,
string avatarId) string avatarId)
=> ctx.DiscordUser.ToLinqToDBTable() => ctx.GetTable<DiscordUser>()
.InsertOrUpdate( .InsertOrUpdate(
() => new() () => new()
{ {
@@ -43,10 +42,9 @@ public static class DiscordUserExtensions
}); });
public static Task EnsureUserCreatedAsync( public static Task EnsureUserCreatedAsync(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong userId) ulong userId)
=> ctx.DiscordUser => ctx.GetTable<DiscordUser>()
.ToLinqToDBTable()
.InsertOrUpdateAsync( .InsertOrUpdateAsync(
() => new() () => 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 //temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
public static DiscordUser GetOrCreateUser( public static DiscordUser GetOrCreateUser(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong userId, ulong userId,
string username, string username,
string discrim, string discrim,
@@ -77,7 +75,7 @@ public static class DiscordUserExtensions
{ {
ctx.EnsureUserCreated(userId, username, discrim, avatarId); ctx.EnsureUserCreated(userId, username, discrim, avatarId);
IQueryable<DiscordUser> queryable = ctx.DiscordUser; IQueryable<DiscordUser> queryable = ctx.Set<DiscordUser>();
if (includes is not null) if (includes is not null)
queryable = includes(queryable); queryable = includes(queryable);
return queryable.First(u => u.UserId == userId); return queryable.First(u => u.UserId == userId);

View File

@@ -29,7 +29,7 @@ public static class GuildConfigExtensions
/// <param name="ctx">Db Context</param> /// <param name="ctx">Db Context</param>
/// <param name="guildId">Id of the guild to get stream role settings for.</param> /// <param name="guildId">Id of the guild to get stream role settings for.</param>
/// <returns>Guild'p stream role settings</returns> /// <returns>Guild'p stream role settings</returns>
public static StreamRoleSettings GetStreamRoleSettings(this NadekoContext ctx, ulong guildId) public static StreamRoleSettings GetStreamRoleSettings(this NadekoBaseContext ctx, ulong guildId)
{ {
var conf = ctx.GuildConfigsForId(guildId, var conf = ctx.GuildConfigsForId(guildId,
set => set.Include(y => y.StreamRole) set => set.Include(y => y.StreamRole)
@@ -65,23 +65,23 @@ public static class GuildConfigExtensions
/// <param name="includes">Use to manipulate the set however you want. Pass null to include everything</param> /// <param name="includes">Use to manipulate the set however you want. Pass null to include everything</param>
/// <returns>Config for the guild</returns> /// <returns>Config for the guild</returns>
public static GuildConfig GuildConfigsForId( public static GuildConfig GuildConfigsForId(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong guildId, ulong guildId,
Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes) Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes)
{ {
GuildConfig config; GuildConfig config;
if (includes is null) if (includes is null)
config = ctx.GuildConfigs.IncludeEverything().FirstOrDefault(c => c.GuildId == guildId); config = ctx.Set<GuildConfig>().IncludeEverything().FirstOrDefault(c => c.GuildId == guildId);
else else
{ {
var set = includes(ctx.GuildConfigs); var set = includes(ctx.Set<GuildConfig>());
config = set.FirstOrDefault(c => c.GuildId == guildId); config = set.FirstOrDefault(c => c.GuildId == guildId);
} }
if (config is null) if (config is null)
{ {
ctx.GuildConfigs.Add(config = new() ctx.Set<GuildConfig>().Add(config = new()
{ {
GuildId = guildId, GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist, Permissions = Permissionv2.GetDefaultPermlist,
@@ -120,16 +120,18 @@ public static class GuildConfigExtensions
// .First(x => x.GuildId == guildId); // .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() var logSetting = ctx.Set<LogSetting>()
.AsQueryable()
.Include(x => x.LogIgnores) .Include(x => x.LogIgnores)
.Where(x => x.GuildId == guildId) .Where(x => x.GuildId == guildId)
.FirstOrDefault(); .FirstOrDefault();
if (logSetting is null) if (logSetting is null)
{ {
ctx.LogSettings.Add(logSetting = new() ctx.Set<LogSetting>()
.Add(logSetting = new()
{ {
GuildId = guildId GuildId = guildId
}); });
@@ -146,16 +148,16 @@ public static class GuildConfigExtensions
return query.ToList(); 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() var config = ctx.Set<GuildConfig>().AsQueryable()
.Where(gc => gc.GuildId == guildId) .Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions) .Include(gc => gc.Permissions)
.FirstOrDefault(); .FirstOrDefault();
if (config is null) // if there is no guildconfig, create new one if (config is null) // if there is no guildconfig, create new one
{ {
ctx.GuildConfigs.Add(config = new() ctx.Set<GuildConfig>().Add(config = new()
{ {
GuildId = guildId, GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist Permissions = Permissionv2.GetDefaultPermlist
@@ -191,7 +193,7 @@ public static class GuildConfigExtensions
conf.CleverbotEnabled = cleverbotEnabled; 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, var gc = ctx.GuildConfigsForId(guildId,
set => set.Include(x => x.XpSettings) set => set.Include(x => x.XpSettings)

View File

@@ -0,0 +1,7 @@
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Db;
public abstract class NadekoBaseContext : DbContext
{
}

View File

@@ -1,5 +1,6 @@
using LinqToDB; using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using NadekoBot.Db.Models;
namespace NadekoBot.Modules.Gambling.Bank; namespace NadekoBot.Modules.Gambling.Bank;
@@ -20,8 +21,7 @@ public sealed class BankService : IBankService, INService
throw new ArgumentOutOfRangeException(nameof(amount)); throw new ArgumentOutOfRangeException(nameof(amount));
await using var ctx = _db.GetDbContext(); await using var ctx = _db.GetDbContext();
await ctx.BankUsers await ctx.GetTable<BankUser>()
.ToLinqToDBTable()
.InsertOrUpdateAsync(() => new() .InsertOrUpdateAsync(() => new()
{ {
UserId = userId, UserId = userId,