UnitOfWork compltely removed. GetDbContext now returns a NadekoContext. Changed every access to contect via uow._context to uow

This commit is contained in:
Kwoth
2021-06-19 05:16:27 +02:00
parent 51a4499809
commit c127dcd1e3
81 changed files with 404 additions and 512 deletions

View File

@@ -46,7 +46,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var conf = uow._context.GuildConfigsForId(guildId,
var conf = uow.GuildConfigsForId(guildId,
set => set.Include(x => x.DelMsgOnCmdChannels));
return (conf.DeleteMessageOnCommand, conf.DelMsgOnCmdChannels);
@@ -84,7 +84,7 @@ namespace NadekoBot.Modules.Administration.Services
bool enabled;
using (var uow = _db.GetDbContext())
{
var conf = uow._context.GuildConfigsForId(guildId, set => set);
var conf = uow.GuildConfigsForId(guildId, set => set);
enabled = conf.DeleteMessageOnCommand = !conf.DeleteMessageOnCommand;
uow.SaveChanges();
@@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var conf = uow._context.GuildConfigsForId(guildId,
var conf = uow.GuildConfigsForId(guildId,
set => set.Include(x => x.DelMsgOnCmdChannels));
var old = conf.DelMsgOnCmdChannels.FirstOrDefault(x => x.ChannelId == chId);
@@ -105,7 +105,7 @@ namespace NadekoBot.Modules.Administration.Services
if (!(old is null))
{
conf.DelMsgOnCmdChannels.Remove(old);
uow._context.Remove(old);
uow.Remove(old);
}
}
else

View File

@@ -108,7 +108,7 @@ namespace NadekoBot.Modules.Administration.Services
public async Task<IReadOnlyList<ulong>> ToggleAarAsync(ulong guildId, ulong roleId)
{
using var uow = _db.GetDbContext();
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
var roles = gc.GetAutoAssignableRoles();
if(!roles.Remove(roleId) && roles.Count < 3)
roles.Add(roleId);
@@ -128,7 +128,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using var uow = _db.GetDbContext();
await uow._context
await uow
.GuildConfigs
.AsNoTracking()
.Where(x => x.GuildId == guildId)
@@ -143,7 +143,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using var uow = _db.GetDbContext();
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
gc.SetAutoAssignableRoles(newRoles);
await uow.SaveChangesAsync();

View File

@@ -46,7 +46,7 @@ DELETE FROM Clubs;";
int res;
using (var uow = _db.GetDbContext())
{
res = await uow._context.Database.ExecuteSqlRawAsync(sql);
res = await uow.Database.ExecuteSqlRawAsync(sql);
}
return res;
}
@@ -67,7 +67,7 @@ DELETE FROM Clubs;";
using (var uow = _db.GetDbContext())
{
var conn = uow._context.Database.GetDbConnection();
var conn = uow.Database.GetDbConnection();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
@@ -97,31 +97,31 @@ DELETE FROM Clubs;";
using var uow = _db.GetDbContext();
// get waifu info
var wi = await uow._context.Set<WaifuInfo>()
var wi = await uow.Set<WaifuInfo>()
.FirstOrDefaultAsyncEF(x => x.Waifu.UserId == userId);
// if it exists, delete waifu related things
if (!(wi is null))
{
// remove updates which have new or old as this waifu
await uow._context
await uow
.WaifuUpdates
.DeleteAsync(wu => wu.New.UserId == userId || wu.Old.UserId == userId);
// delete all items this waifu owns
await uow._context
await uow
.Set<WaifuItem>()
.DeleteAsync(x => x.WaifuInfoId == wi.Id);
// all waifus this waifu claims are released
await uow._context
await uow
.Set<WaifuInfo>()
.AsQueryable()
.Where(x => x.Claimer.UserId == userId)
.UpdateAsync(x => new WaifuInfo() {ClaimerId = null});
// all affinities set to this waifu are reset
await uow._context
await uow
.Set<WaifuInfo>()
.AsQueryable()
.Where(x => x.Affinity.UserId == userId)
@@ -129,16 +129,16 @@ DELETE FROM Clubs;";
}
// delete guild xp
await uow._context
await uow
.UserXpStats
.DeleteAsync(x => x.UserId == userId);
// delete currency transactions
await uow._context.Set<CurrencyTransaction>()
await uow.Set<CurrencyTransaction>()
.DeleteAsync(x => x.UserId == userId);
// delete user, currency, and clubs go away with it
await uow._context.DiscordUser
await uow.DiscordUser
.DeleteAsync(u => u.UserId == userId);
}
}

View File

@@ -28,7 +28,7 @@ namespace NadekoBot.Modules.Administration.Services
_db = db;
_services = services;
using var uow = _db.GetDbContext();
_overrides = uow._context.DiscordPermOverrides
_overrides = uow.DiscordPermOverrides
.AsNoTracking()
.AsEnumerable()
.ToDictionary(o => (o.GuildId ?? 0, o.Command), o => o)
@@ -60,14 +60,14 @@ namespace NadekoBot.Modules.Administration.Services
commandName = commandName.ToLowerInvariant();
using (var uow = _db.GetDbContext())
{
var over = await uow._context
var over = await uow
.Set<DiscordPermOverride>()
.AsQueryable()
.FirstOrDefaultAsync(x => x.GuildId == guildId && commandName == x.Command);
if (over is null)
{
uow._context.Set<DiscordPermOverride>()
uow.Set<DiscordPermOverride>()
.Add(over = new DiscordPermOverride()
{
Command = commandName,
@@ -90,14 +90,14 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var overrides = await uow._context
var overrides = await uow
.Set<DiscordPermOverride>()
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId)
.ToListAsync();
uow._context.RemoveRange(overrides);
uow.RemoveRange(overrides);
await uow.SaveChangesAsync();
foreach (var over in overrides)
@@ -113,7 +113,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = _db.GetDbContext())
{
var over = await uow._context
var over = await uow
.Set<DiscordPermOverride>()
.AsQueryable()
.AsNoTracking()
@@ -122,7 +122,7 @@ namespace NadekoBot.Modules.Administration.Services
if (over is null)
return;
uow._context.Remove(over);
uow.Remove(over);
await uow.SaveChangesAsync();
_overrides.TryRemove((guildId, commandName), out _);
@@ -133,7 +133,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
return uow._context
return uow
.Set<DiscordPermOverride>()
.AsQueryable()
.AsNoTracking()

View File

@@ -64,7 +64,7 @@ namespace NadekoBot.Modules.Administration.Services
ulong? id;
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
if (gc.GameVoiceChannel == vchId)
{

View File

@@ -67,7 +67,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
gc.TimeZoneId = tz?.Id;
uow.SaveChanges();

View File

@@ -48,7 +48,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = db.GetDbContext())
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow._context
var configs = uow
.Set<GuildConfig>()
.AsQueryable()
.Include(gc => gc.LogSetting)
@@ -124,7 +124,7 @@ namespace NadekoBot.Modules.Administration.Services
int removed = 0;
using (var uow = _db.GetDbContext())
{
var config = uow._context.LogSettingsFor(gid);
var config = uow.LogSettingsFor(gid);
LogSetting logSetting = GuildLogSettings.GetOrAdd(gid, (id) => config.LogSetting);
removed = logSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == cid);
config.LogSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == cid);
@@ -167,7 +167,7 @@ namespace NadekoBot.Modules.Administration.Services
LogSetting logSetting;
using (var uow = _db.GetDbContext())
{
logSetting = uow._context.LogSettingsFor(guildId).LogSetting;
logSetting = uow.LogSettingsFor(guildId).LogSetting;
GuildLogSettings.AddOrUpdate(guildId, (id) => logSetting, (id, old) => logSetting);
logSetting.LogOtherId =
logSetting.MessageUpdatedId =
@@ -256,7 +256,7 @@ namespace NadekoBot.Modules.Administration.Services
ulong? channelId = null;
using (var uow = _db.GetDbContext())
{
var logSetting = uow._context.LogSettingsFor(gid).LogSetting;
var logSetting = uow.LogSettingsFor(gid).LogSetting;
GuildLogSettings.AddOrUpdate(gid, (id) => logSetting, (id, old) => logSetting);
switch (type)
{
@@ -1220,7 +1220,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var newLogSetting = uow._context.LogSettingsFor(guildId).LogSetting;
var newLogSetting = uow.LogSettingsFor(guildId).LogSetting;
switch (logChannelType)
{
case LogType.Other:

View File

@@ -47,7 +47,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = db.GetDbContext())
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow._context.Set<GuildConfig>().AsQueryable()
var configs = uow.Set<GuildConfig>().AsQueryable()
.Include(x => x.MutedUsers)
.Include(x => x.UnbanTimer)
.Include(x => x.UnmuteTimers)
@@ -173,7 +173,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, set => set);
var config = uow.GuildConfigsForId(guildId, set => set);
config.MuteRoleName = name;
GuildMuteRoles.AddOrUpdate(guildId, name, (id, old) => name);
await uow.SaveChangesAsync();
@@ -191,7 +191,7 @@ namespace NadekoBot.Modules.Administration.Services
StopTimer(usr.GuildId, usr.Id, TimerType.Mute);
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(usr.Guild.Id,
var config = uow.GuildConfigsForId(usr.Guild.Id,
set => set.Include(gc => gc.MutedUsers)
.Include(gc => gc.UnmuteTimers));
config.MutedUsers.Add(new MutedUserId()
@@ -231,7 +231,7 @@ namespace NadekoBot.Modules.Administration.Services
StopTimer(guildId, usrId, TimerType.Mute);
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, set => set.Include(gc => gc.MutedUsers)
var config = uow.GuildConfigsForId(guildId, set => set.Include(gc => gc.MutedUsers)
.Include(gc => gc.UnmuteTimers));
var match = new MutedUserId()
{
@@ -240,7 +240,7 @@ namespace NadekoBot.Modules.Administration.Services
var toRemove = config.MutedUsers.FirstOrDefault(x => x.Equals(match));
if (toRemove != null)
{
uow._context.Remove(toRemove);
uow.Remove(toRemove);
}
if (MutedUsers.TryGetValue(guildId, out ConcurrentHashSet<ulong> muted))
muted.TryRemove(usrId);
@@ -326,7 +326,7 @@ namespace NadekoBot.Modules.Administration.Services
await MuteUser(user, mod, muteType, reason).ConfigureAwait(false); // mute the user. This will also remove any previous unmute timers
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnmuteTimers));
var config = uow.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnmuteTimers));
config.UnmuteTimers.Add(new UnmuteTimer()
{
UserId = user.Id,
@@ -343,7 +343,7 @@ namespace NadekoBot.Modules.Administration.Services
await guild.AddBanAsync(user.Id, 0, reason).ConfigureAwait(false);
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer));
var config = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.UnbanTimer));
config.UnbanTimer.Add(new UnbanTimer()
{
UserId = user.Id,
@@ -360,7 +360,7 @@ namespace NadekoBot.Modules.Administration.Services
await user.AddRoleAsync(role).ConfigureAwait(false);
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnroleTimer));
var config = uow.GuildConfigsForId(user.GuildId, set => set.Include(x => x.UnroleTimer));
config.UnroleTimer.Add(new UnroleTimer()
{
UserId = user.Id,
@@ -458,17 +458,17 @@ namespace NadekoBot.Modules.Administration.Services
object toDelete;
if (type == TimerType.Mute)
{
var config = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.UnmuteTimers));
var config = uow.GuildConfigsForId(guildId, set => set.Include(x => x.UnmuteTimers));
toDelete = config.UnmuteTimers.FirstOrDefault(x => x.UserId == userId);
}
else
{
var config = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.UnbanTimer));
var config = uow.GuildConfigsForId(guildId, set => set.Include(x => x.UnbanTimer));
toDelete = config.UnbanTimer.FirstOrDefault(x => x.UserId == userId);
}
if (toDelete != null)
{
uow._context.Remove(toDelete);
uow.Remove(toDelete);
}
uow.SaveChanges();
}

View File

@@ -56,7 +56,7 @@ namespace NadekoBot.Modules.Administration.Services
IReadOnlyList<RotatingPlayingStatus> rotatingStatuses;
using (var uow = _db.GetDbContext())
{
rotatingStatuses = uow._context.RotatingStatus
rotatingStatuses = uow.RotatingStatus
.AsNoTracking()
.OrderBy(x => x.Id)
.ToList();
@@ -84,7 +84,7 @@ namespace NadekoBot.Modules.Administration.Services
throw new ArgumentOutOfRangeException(nameof(index));
using var uow = _db.GetDbContext();
var toRemove = await uow._context.RotatingStatus
var toRemove = await uow.RotatingStatus
.AsQueryable()
.AsNoTracking()
.Skip(index)
@@ -93,7 +93,7 @@ namespace NadekoBot.Modules.Administration.Services
if (toRemove is null)
return null;
uow._context.Remove(toRemove);
uow.Remove(toRemove);
await uow.SaveChangesAsync();
return toRemove.Status;
}
@@ -102,7 +102,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using var uow = _db.GetDbContext();
var toAdd = new RotatingPlayingStatus {Status = status, Type = t};
uow._context.Add(toAdd);
uow.Add(toAdd);
await uow.SaveChangesAsync();
}
@@ -116,7 +116,7 @@ namespace NadekoBot.Modules.Administration.Services
public IReadOnlyList<RotatingPlayingStatus> GetRotatingStatuses()
{
using var uow = _db.GetDbContext();
return uow._context.RotatingStatus.AsNoTracking().ToList();
return uow.RotatingStatus.AsNoTracking().ToList();
}
}
}

View File

@@ -52,7 +52,7 @@ namespace NadekoBot.Modules.Administration.Services
var ids = client.GetGuildIds();
using (var uow = db.GetDbContext())
{
var configs = uow._context.Set<GuildConfig>()
var configs = uow.Set<GuildConfig>()
.AsQueryable()
.Include(x => x.AntiRaidSetting)
.Include(x => x.AntiSpamSetting)
@@ -114,7 +114,7 @@ namespace NadekoBot.Modules.Administration.Services
private Task _bot_JoinedGuild(GuildConfig gc)
{
using var uow = _db.GetDbContext();
var gcWithData = uow._context.GuildConfigsForId(gc.GuildId,
var gcWithData = uow.GuildConfigsForId(gc.GuildId,
set => set
.Include(x => x.AntiRaidSetting)
.Include(x => x.AntiAltSetting)
@@ -304,7 +304,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiRaidSetting));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiRaidSetting));
gc.AntiRaidSetting = stats.AntiRaidSettings;
await uow.SaveChangesAsync();
@@ -319,7 +319,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiRaidSetting));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiRaidSetting));
gc.AntiRaidSetting = null;
uow.SaveChanges();
@@ -336,7 +336,7 @@ namespace NadekoBot.Modules.Administration.Services
removed.UserStats.ForEach(x => x.Value.Dispose());
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting)
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting)
.ThenInclude(x => x.IgnoredChannels));
gc.AntiSpamSetting = null;
@@ -375,7 +375,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting));
if (gc.AntiSpamSetting != null)
{
@@ -402,7 +402,7 @@ namespace NadekoBot.Modules.Administration.Services
bool added;
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting).ThenInclude(x => x.IgnoredChannels));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiSpamSetting).ThenInclude(x => x.IgnoredChannels));
var spam = gc.AntiSpamSetting;
if (spam is null)
{
@@ -418,7 +418,7 @@ namespace NadekoBot.Modules.Administration.Services
else
{
var toRemove = spam.IgnoredChannels.First(x => x.ChannelId == channelId);
uow._context.Set<AntiSpamIgnore>().Remove(toRemove); // remove from db
uow.Set<AntiSpamIgnore>().Remove(toRemove); // remove from db
if (_antiSpamGuilds.TryGetValue(guildId, out var temp))
{
temp.AntiSpamSettings.IgnoredChannels.Remove(toRemove); // remove from local cache
@@ -459,7 +459,7 @@ namespace NadekoBot.Modules.Administration.Services
int actionDurationMinutes = 0, ulong? roleId = null)
{
using var uow = _db.GetDbContext();
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiAltSetting));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiAltSetting));
gc.AntiAltSetting = new AntiAltSetting()
{
Action = action,
@@ -478,7 +478,7 @@ namespace NadekoBot.Modules.Administration.Services
return false;
using var uow = _db.GetDbContext();
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.AntiAltSetting));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.AntiAltSetting));
gc.AntiAltSetting = null;
await uow.SaveChangesAsync();
return true;

View File

@@ -156,7 +156,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(id, set => set
var gc = uow.GuildConfigsForId(id, set => set
.Include(x => x.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles));
if (gc.ReactionRoleMessages.Count >= 10)
@@ -174,10 +174,10 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(id,
var gc = uow.GuildConfigsForId(id,
set => set.Include(x => x.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles));
uow._context.Set<ReactionRole>()
uow.Set<ReactionRole>()
.RemoveRange(gc.ReactionRoleMessages[index].ReactionRoles);
gc.ReactionRoleMessages.RemoveAt(index);
_models.AddOrUpdate(id,

View File

@@ -43,13 +43,13 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId);
var roles = uow.SelfAssignableRoles.GetFromGuild(guildId);
if (roles.Any(s => s.RoleId == role.Id && s.GuildId == role.Guild.Id))
{
return false;
}
uow._context.SelfAssignableRoles.Add(new SelfAssignedRole
uow.SelfAssignableRoles.Add(new SelfAssignedRole
{
Group = group,
RoleId = role.Id,
@@ -65,7 +65,7 @@ namespace NadekoBot.Modules.Administration.Services
bool newval;
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, set => set);
var config = uow.GuildConfigsForId(guildId, set => set);
newval = config.AutoDeleteSelfAssignedRoleMessages = !config.AutoDeleteSelfAssignedRoleMessages;
uow.SaveChanges();
}
@@ -77,7 +77,7 @@ namespace NadekoBot.Modules.Administration.Services
LevelStats userLevelData;
using (var uow = _db.GetDbContext())
{
var stats = uow._context.GetOrCreateUserXpStats(guildUser.Guild.Id, guildUser.Id);
var stats = uow.GetOrCreateUserXpStats(guildUser.Guild.Id, guildUser.Id);
userLevelData = new LevelStats(stats.Xp + stats.AwardedXp);
}
@@ -139,7 +139,7 @@ namespace NadekoBot.Modules.Administration.Services
bool set = false;
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, y => y.Include(x => x.SelfAssignableRoleGroupNames));
var gc = uow.GuildConfigsForId(guildId, y => y.Include(x => x.SelfAssignableRoleGroupNames));
var toUpdate = gc.SelfAssignableRoleGroupNames.FirstOrDefault(x => x.Number == group);
if (string.IsNullOrWhiteSpace(name))
@@ -197,7 +197,7 @@ namespace NadekoBot.Modules.Administration.Services
bool success;
using (var uow = _db.GetDbContext())
{
success = uow._context.SelfAssignableRoles.DeleteByGuildAndRoleId(guildId, roleId);
success = uow.SelfAssignableRoles.DeleteByGuildAndRoleId(guildId, roleId);
uow.SaveChanges();
}
return success;
@@ -207,10 +207,10 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
var autoDelete = gc.AutoDeleteSelfAssignedRoleMessages;
var exclusive = gc.ExclusiveSelfAssignedRoles;
var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId);
var roles = uow.SelfAssignableRoles.GetFromGuild(guildId);
return (autoDelete, exclusive, roles);
}
@@ -220,7 +220,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId);
var roles = uow.SelfAssignableRoles.GetFromGuild(guildId);
var sar = roles.FirstOrDefault(x => x.RoleId == role.Id);
if (sar != null)
{
@@ -241,7 +241,7 @@ namespace NadekoBot.Modules.Administration.Services
bool areExclusive;
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, set => set);
var config = uow.GuildConfigsForId(guildId, set => set);
areExclusive = config.ExclusiveSelfAssignedRoles = !config.ExclusiveSelfAssignedRoles;
uow.SaveChanges();
@@ -257,13 +257,13 @@ namespace NadekoBot.Modules.Administration.Services
IDictionary<int, string> groupNames;
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guild.Id, set => set.Include(x => x.SelfAssignableRoleGroupNames));
var gc = uow.GuildConfigsForId(guild.Id, set => set.Include(x => x.SelfAssignableRoleGroupNames));
exclusive = gc.ExclusiveSelfAssignedRoles;
groupNames = gc.SelfAssignableRoleGroupNames.ToDictionary(x => x.Number, x => x.Name);
var roleModels = uow._context.SelfAssignableRoles.GetFromGuild(guild.Id);
var roleModels = uow.SelfAssignableRoles.GetFromGuild(guild.Id);
roles = roleModels
.Select(x => (Model: x, Role: guild.GetRole(x.RoleId)));
uow._context.SelfAssignableRoles.RemoveRange(roles.Where(x => x.Role == null).Select(x => x.Model).ToArray());
uow.SelfAssignableRoles.RemoveRange(roles.Where(x => x.Role == null).Select(x => x.Model).ToArray());
uow.SaveChanges();
}

View File

@@ -100,7 +100,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using var uow = _db.GetDbContext();
_autoCommands = uow._context
_autoCommands = uow
.AutoCommands
.AsNoTracking()
.Where(x => x.Interval >= 5)
@@ -111,7 +111,7 @@ namespace NadekoBot.Modules.Administration.Services
.ToConcurrent())
.ToConcurrent();
var startupCommands = uow._context.AutoCommands.AsNoTracking().Where(x => x.Interval == 0);
var startupCommands = uow.AutoCommands.AsNoTracking().Where(x => x.Interval == 0);
foreach (var cmd in startupCommands)
{
try
@@ -163,7 +163,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
uow._context.AutoCommands.Add(cmd);
uow.AutoCommands.Add(cmd);
uow.SaveChanges();
}
@@ -181,7 +181,7 @@ namespace NadekoBot.Modules.Administration.Services
public IEnumerable<AutoCommand> GetStartupCommands()
{
using var uow = _db.GetDbContext();
return uow._context
return uow
.AutoCommands
.AsNoTracking()
.Where(x => x.Interval == 0)
@@ -192,7 +192,7 @@ namespace NadekoBot.Modules.Administration.Services
public IEnumerable<AutoCommand> GetAutoCommands()
{
using var uow = _db.GetDbContext();
return uow._context
return uow
.AutoCommands
.AsNoTracking()
.Where(x => x.Interval >= 5)
@@ -297,7 +297,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
cmd = uow._context.AutoCommands
cmd = uow.AutoCommands
.AsNoTracking()
.Where(x => x.Interval == 0)
.Skip(index)
@@ -305,7 +305,7 @@ namespace NadekoBot.Modules.Administration.Services
if (cmd != null)
{
uow._context.Remove(cmd);
uow.Remove(cmd);
uow.SaveChanges();
return true;
}
@@ -318,7 +318,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
cmd = uow._context.AutoCommands
cmd = uow.AutoCommands
.AsNoTracking()
.Where(x => x.Interval >= 5)
.Skip(index)
@@ -326,7 +326,7 @@ namespace NadekoBot.Modules.Administration.Services
if (cmd != null)
{
uow._context.Remove(cmd);
uow.Remove(cmd);
if (_autoCommands.TryGetValue(cmd.GuildId, out var autos))
if (autos.TryRemove(cmd.Id, out var timer))
timer.Change(Timeout.Infinite, Timeout.Infinite);
@@ -369,12 +369,12 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var toRemove = uow._context
var toRemove = uow
.AutoCommands
.AsNoTracking()
.Where(x => x.Interval == 0);
uow._context.AutoCommands.RemoveRange(toRemove);
uow.AutoCommands.RemoveRange(toRemove);
uow.SaveChanges();
}
}

View File

@@ -61,16 +61,16 @@ namespace NadekoBot.Modules.Administration.Services
List<WarningPunishment> ps;
using (var uow = _db.GetDbContext())
{
ps = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments))
ps = uow.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments))
.WarnPunishments;
warnings += uow._context
warnings += uow
.Warnings
.ForId(guildId, userId)
.Where(w => !w.Forgiven && w.UserId == userId)
.Count();
uow._context.Warnings.Add(warn);
uow.Warnings.Add(warn);
uow.SaveChanges();
}
@@ -169,14 +169,14 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var cleared = await uow._context.Database.ExecuteSqlRawAsync($@"UPDATE Warnings
var cleared = await uow.Database.ExecuteSqlRawAsync($@"UPDATE Warnings
SET Forgiven = 1,
ForgivenBy = 'Expiry'
WHERE GuildId in (SELECT GuildId FROM GuildConfigs WHERE WarnExpireHours > 0 AND WarnExpireAction = 0)
AND Forgiven = 0
AND DateAdded < datetime('now', (SELECT '-' || WarnExpireHours || ' hours' FROM GuildConfigs as gc WHERE gc.GuildId = Warnings.GuildId));");
var deleted = await uow._context.Database.ExecuteSqlRawAsync($@"DELETE FROM Warnings
var deleted = await uow.Database.ExecuteSqlRawAsync($@"DELETE FROM Warnings
WHERE GuildId in (SELECT GuildId FROM GuildConfigs WHERE WarnExpireHours > 0 AND WarnExpireAction = 1)
AND DateAdded < datetime('now', (SELECT '-' || WarnExpireHours || ' hours' FROM GuildConfigs as gc WHERE gc.GuildId = Warnings.GuildId));");
@@ -191,7 +191,7 @@ WHERE GuildId in (SELECT GuildId FROM GuildConfigs WHERE WarnExpireHours > 0 AND
{
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, inc => inc);
var config = uow.GuildConfigsForId(guildId, inc => inc);
if (config.WarnExpireHours == 0)
return;
@@ -199,7 +199,7 @@ WHERE GuildId in (SELECT GuildId FROM GuildConfigs WHERE WarnExpireHours > 0 AND
var hours = $"{-config.WarnExpireHours} hours";
if (config.WarnExpireAction == WarnExpireAction.Clear)
{
await uow._context.Database.ExecuteSqlInterpolatedAsync($@"UPDATE warnings
await uow.Database.ExecuteSqlInterpolatedAsync($@"UPDATE warnings
SET Forgiven = 1,
ForgivenBy = 'Expiry'
WHERE GuildId={guildId}
@@ -208,7 +208,7 @@ WHERE GuildId={guildId}
}
else if (config.WarnExpireAction == WarnExpireAction.Delete)
{
await uow._context.Database.ExecuteSqlInterpolatedAsync($@"DELETE FROM warnings
await uow.Database.ExecuteSqlInterpolatedAsync($@"DELETE FROM warnings
WHERE GuildId={guildId}
AND DateAdded < datetime('now', {hours})");
}
@@ -220,7 +220,7 @@ WHERE GuildId={guildId}
public Task<int> GetWarnExpire(ulong guildId)
{
using var uow = _db.GetDbContext();
var config = uow._context.GuildConfigsForId(guildId, set => set);
var config = uow.GuildConfigsForId(guildId, set => set);
return Task.FromResult(config.WarnExpireHours / 24);
}
@@ -228,7 +228,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(guildId, inc => inc);
var config = uow.GuildConfigsForId(guildId, inc => inc);
config.WarnExpireHours = days * 24;
config.WarnExpireAction = delete ? WarnExpireAction.Delete : WarnExpireAction.Clear;
@@ -246,7 +246,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
return uow._context.Warnings.GetForGuild(gid).GroupBy(x => x.UserId).ToArray();
return uow.Warnings.GetForGuild(gid).GroupBy(x => x.UserId).ToArray();
}
}
@@ -254,7 +254,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
return uow._context.Warnings.ForId(gid, userId);
return uow.Warnings.ForId(gid, userId);
}
}
@@ -265,11 +265,11 @@ WHERE GuildId={guildId}
{
if (index == 0)
{
await uow._context.Warnings.ForgiveAll(guildId, userId, moderator);
await uow.Warnings.ForgiveAll(guildId, userId, moderator);
}
else
{
toReturn = uow._context.Warnings.Forgive(guildId, userId, moderator, index - 1);
toReturn = uow.Warnings.Forgive(guildId, userId, moderator, index - 1);
}
uow.SaveChanges();
}
@@ -286,10 +286,10 @@ WHERE GuildId={guildId}
using (var uow = _db.GetDbContext())
{
var ps = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments)).WarnPunishments;
var ps = uow.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments)).WarnPunishments;
var toDelete = ps.Where(x => x.Count == number);
uow._context.RemoveRange(toDelete);
uow.RemoveRange(toDelete);
ps.Add(new WarningPunishment()
{
@@ -310,12 +310,12 @@ WHERE GuildId={guildId}
using (var uow = _db.GetDbContext())
{
var ps = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments)).WarnPunishments;
var ps = uow.GuildConfigsForId(guildId, set => set.Include(x => x.WarnPunishments)).WarnPunishments;
var p = ps.FirstOrDefault(x => x.Count == number);
if (p != null)
{
uow._context.Remove(p);
uow.Remove(p);
uow.SaveChanges();
}
}
@@ -326,7 +326,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
return uow._context.GuildConfigsForId(guildId, gc => gc.Include(x => x.WarnPunishments))
return uow.GuildConfigsForId(guildId, gc => gc.Include(x => x.WarnPunishments))
.WarnPunishments
.OrderBy(x => x.Count)
.ToArray();
@@ -374,7 +374,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
var template = uow._context.BanTemplates
var template = uow.BanTemplates
.AsQueryable()
.FirstOrDefault(x => x.GuildId == guildId);
return template?.Text;
@@ -385,7 +385,7 @@ WHERE GuildId={guildId}
{
using (var uow = _db.GetDbContext())
{
var template = uow._context.BanTemplates
var template = uow.BanTemplates
.AsQueryable()
.FirstOrDefault(x => x.GuildId == guildId);
@@ -394,11 +394,11 @@ WHERE GuildId={guildId}
if (template is null)
return;
uow._context.Remove(template);
uow.Remove(template);
}
else if (template == null)
{
uow._context.BanTemplates.Add(new BanTemplate()
uow.BanTemplates.Add(new BanTemplate()
{
GuildId = guildId,
Text = text,

View File

@@ -33,7 +33,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = db.GetDbContext())
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow._context.Set<GuildConfig>()
var configs = uow.Set<GuildConfig>()
.AsQueryable()
.Include(x => x.VcRoleInfos)
.Where(x => guildIds.Contains(x.GuildId))
@@ -84,7 +84,7 @@ namespace NadekoBot.Modules.Administration.Services
// need to load new guildconfig with vc role included
using (var uow = _db.GetDbContext())
{
var configWithVcRole = uow._context.GuildConfigsForId(
var configWithVcRole = uow.GuildConfigsForId(
arg.GuildId,
set => set.Include(x => x.VcRoleInfos)
);
@@ -128,7 +128,7 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = _db.GetDbContext())
{
Log.Warning($"Removing {missingRoles.Count} missing roles from {nameof(VcRoleService)}");
uow._context.RemoveRange(missingRoles);
uow.RemoveRange(missingRoles);
await uow.SaveChangesAsync();
}
}
@@ -144,11 +144,11 @@ namespace NadekoBot.Modules.Administration.Services
guildVcRoles.AddOrUpdate(vcId, role, (key, old) => role);
using (var uow = _db.GetDbContext())
{
var conf = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.VcRoleInfos));
var conf = uow.GuildConfigsForId(guildId, set => set.Include(x => x.VcRoleInfos));
var toDelete = conf.VcRoleInfos.FirstOrDefault(x => x.VoiceChannelId == vcId); // remove old one
if(toDelete != null)
{
uow._context.Remove(toDelete);
uow.Remove(toDelete);
}
conf.VcRoleInfos.Add(new VcRoleInfo()
{
@@ -169,9 +169,9 @@ namespace NadekoBot.Modules.Administration.Services
using (var uow = _db.GetDbContext())
{
var conf = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.VcRoleInfos));
var conf = uow.GuildConfigsForId(guildId, set => set.Include(x => x.VcRoleInfos));
var toRemove = conf.VcRoleInfos.Where(x => x.VoiceChannelId == vcId).ToList();
uow._context.RemoveRange(toRemove);
uow.RemoveRange(toRemove);
uow.SaveChanges();
}