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

@@ -63,7 +63,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var toAdd = new CommandAlias()
{
Mapping = mapping,
@@ -71,7 +71,7 @@ namespace NadekoBot.Modules.Utility
};
var tr = config.CommandAliases.FirstOrDefault(x => x.Trigger == trigger);
if (tr != null)
uow._context.Set<CommandAlias>().Remove(tr);
uow.Set<CommandAlias>().Remove(tr);
uow.SaveChanges();
}
@@ -82,7 +82,7 @@ namespace NadekoBot.Modules.Utility
{
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
config.CommandAliases.Add(new CommandAlias()
{
Mapping = mapping,
@@ -97,7 +97,7 @@ namespace NadekoBot.Modules.Utility
{
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var toAdd = new CommandAlias()
{
Mapping = mapping,
@@ -105,7 +105,7 @@ namespace NadekoBot.Modules.Utility
};
var toRemove = config.CommandAliases.Where(x => x.Trigger == trigger);
if (toRemove.Any())
uow._context.RemoveRange(toRemove.ToArray());
uow.RemoveRange(toRemove.ToArray());
config.CommandAliases.Add(toAdd);
uow.SaveChanges();
}

View File

@@ -12,7 +12,6 @@ using System.Threading.Tasks;
using NadekoBot.Core.Services;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Database.Repositories.Impl;
using NadekoBot.Services.Database.Repositories.Impl;
using NadekoBot.Db;
namespace NadekoBot.Modules.Utility
@@ -47,7 +46,7 @@ namespace NadekoBot.Modules.Utility
IEnumerable<Quote> quotes;
using (var uow = _db.GetDbContext())
{
quotes = uow._context.Quotes.GetGroup(ctx.Guild.Id, page, order);
quotes = uow.Quotes.GetGroup(ctx.Guild.Id, page, order);
}
if (quotes.Any())
@@ -70,7 +69,7 @@ namespace NadekoBot.Modules.Utility
Quote quote;
using (var uow = _db.GetDbContext())
{
quote = await uow._context.Quotes.GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword);
quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword);
//if (quote != null)
//{
// quote.UseCount += 1;
@@ -102,7 +101,7 @@ namespace NadekoBot.Modules.Utility
Quote quote;
using (var uow = _db.GetDbContext())
{
quote = uow._context.Quotes.GetById(id);
quote = uow.Quotes.GetById(id);
if (quote.GuildId != Context.Guild.Id)
quote = null;
}
@@ -141,7 +140,7 @@ namespace NadekoBot.Modules.Utility
Quote keywordquote;
using (var uow = _db.GetDbContext())
{
keywordquote = await uow._context.Quotes.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, text);
keywordquote = await uow.Quotes.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, text);
}
if (keywordquote == null)
@@ -166,7 +165,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext())
{
quote = uow._context.Quotes.GetById(id);
quote = uow.Quotes.GetById(id);
}
if (quote is null || quote.GuildId != ctx.Guild.Id)
@@ -203,7 +202,7 @@ namespace NadekoBot.Modules.Utility
Quote q;
using (var uow = _db.GetDbContext())
{
uow._context.Quotes.Add(q = new Quote
uow.Quotes.Add(q = new Quote
{
AuthorId = ctx.Message.Author.Id,
AuthorName = ctx.Message.Author.Username,
@@ -226,7 +225,7 @@ namespace NadekoBot.Modules.Utility
string response;
using (var uow = _db.GetDbContext())
{
var q = uow._context.Quotes.GetById(id);
var q = uow.Quotes.GetById(id);
if ((q?.GuildId != ctx.Guild.Id) || (!isAdmin && q.AuthorId != ctx.Message.Author.Id))
{
@@ -234,7 +233,7 @@ namespace NadekoBot.Modules.Utility
}
else
{
uow._context.Quotes.Remove(q);
uow.Quotes.Remove(q);
await uow.SaveChangesAsync();
success = true;
response = GetText("quote_deleted", id);
@@ -258,7 +257,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext())
{
uow._context.Quotes.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant());
uow.Quotes.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant());
await uow.SaveChangesAsync();
}

View File

@@ -95,7 +95,7 @@ namespace NadekoBot.Modules.Utility
List<Reminder> rems;
using (var uow = _db.GetDbContext())
{
rems = uow._context.Reminders.RemindersFor(ctx.User.Id, page)
rems = uow.Reminders.RemindersFor(ctx.User.Id, page)
.ToList();
}
@@ -133,13 +133,13 @@ namespace NadekoBot.Modules.Utility
Reminder rem = null;
using (var uow = _db.GetDbContext())
{
var rems = uow._context.Reminders.RemindersFor(ctx.User.Id, index / 10)
var rems = uow.Reminders.RemindersFor(ctx.User.Id, index / 10)
.ToList();
var pageIndex = index % 10;
if (rems.Count > pageIndex)
{
rem = rems[pageIndex];
uow._context.Reminders.Remove(rem);
uow.Reminders.Remove(rem);
uow.SaveChanges();
}
}
@@ -182,7 +182,7 @@ namespace NadekoBot.Modules.Utility
using (var uow = _db.GetDbContext())
{
uow._context.Reminders.Add(rem);
uow.Reminders.Add(rem);
await uow.SaveChangesAsync();
}

View File

@@ -30,7 +30,7 @@ namespace NadekoBot.Modules.Utility.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>()
.Include(gc => gc.CommandAliases)
.Where(x => guildIds.Contains(x.GuildId))
.ToList();
@@ -53,7 +53,7 @@ namespace NadekoBot.Modules.Utility.Services
int count;
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set.Include(x => x.CommandAliases));
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.CommandAliases));
count = gc.CommandAliases.Count;
gc.CommandAliases.Clear();
uow.SaveChanges();

View File

@@ -138,7 +138,7 @@ namespace NadekoBot.Modules.Utility.Services
using (var uow = _db.GetDbContext())
{
var users = uow._context.Set<RewardedUser>();
var users = uow.Set<RewardedUser>();
var usr = users.FirstOrDefault(x => x.PatreonUserId == data.User.id);
if (usr == null)

View File

@@ -65,7 +65,7 @@ namespace NadekoBot.Modules.Utility.Services
{
using (var uow = _db.GetDbContext())
{
uow._context.Set<Reminder>()
uow.Set<Reminder>()
.RemoveRange(reminders);
await uow.SaveChangesAsync();
@@ -76,7 +76,7 @@ namespace NadekoBot.Modules.Utility.Services
{
using (var uow = _db.GetDbContext())
{
return uow._context.Reminders
return uow.Reminders
.FromSqlInterpolated($"select * from reminders where ((serverid >> 22) % {_creds.TotalShards}) == {_client.ShardId} and \"when\" < {now};")
.ToListAsync();
}

View File

@@ -40,8 +40,7 @@ namespace NadekoBot.Modules.Utility.Services
_client = client;
var uow = _db.GetDbContext();
var shardRepeaters = uow
._context
var shardRepeaters = uow
.Set<Repeater>()
.FromSqlInterpolated($@"select * from repeaters
where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
@@ -133,7 +132,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
{
using var uow = _db.GetDbContext();
var toTrigger = await uow._context.Repeaters
var toTrigger = await uow.Repeaters
.AsNoTracking()
.Skip(index)
.FirstOrDefaultAsyncEF(x => x.GuildId == guildId);
@@ -290,7 +289,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
_noRedundant.TryRemove(r.Id);
using var uow = _db.GetDbContext();
await uow._context
await uow
.Repeaters
.DeleteAsync(x => x.Id == r.Id);
@@ -313,7 +312,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
private async Task SetRepeaterLastMessageInternal(int repeaterId, ulong lastMsgId)
{
using var uow = _db.GetDbContext();
await uow._context.Repeaters
await uow.Repeaters
.AsQueryable()
.Where(x => x.Id == repeaterId)
.UpdateAsync(rep => new Repeater()
@@ -345,8 +344,8 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
using var uow = _db.GetDbContext();
if (await uow._context.Repeaters.AsNoTracking().CountAsyncEF() < MAX_REPEATERS)
uow._context.Repeaters.Add(rep);
if (await uow.Repeaters.AsNoTracking().CountAsyncEF() < MAX_REPEATERS)
uow.Repeaters.Add(rep);
else
return null;
@@ -365,7 +364,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
throw new ArgumentOutOfRangeException(nameof(index));
using var uow = _db.GetDbContext();
var toRemove = await uow._context.Repeaters
var toRemove = await uow.Repeaters
.AsNoTracking()
.Skip(index)
.FirstOrDefaultAsyncEF(x => x.GuildId == guildId);
@@ -380,7 +379,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
return null;
_noRedundant.TryRemove(toRemove.Id);
uow._context.Repeaters.Remove(toRemove);
uow.Repeaters.Remove(toRemove);
await uow.SaveChangesAsync();
return removed;
}
@@ -396,7 +395,7 @@ where ((guildid >> 22) % {_creds.TotalShards}) == {_client.ShardId};")
public async Task<bool?> ToggleRedundantAsync(ulong guildId, int index)
{
using var uow = _db.GetDbContext();
var toToggle = await uow._context
var toToggle = await uow
.Repeaters
.AsQueryable()
.Skip(index)

View File

@@ -81,7 +81,7 @@ namespace NadekoBot.Modules.Utility.Services
bool success = false;
using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow._context.GetStreamRoleSettings(guild.Id);
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
if (listType == StreamRoleListType.Whitelist)
{
@@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Utility.Services
var toDelete = streamRoleSettings.Whitelist.FirstOrDefault(x => x.Equals(userObj));
if (toDelete != null)
{
uow._context.Remove(toDelete);
uow.Remove(toDelete);
success = true;
}
}
@@ -146,7 +146,7 @@ namespace NadekoBot.Modules.Utility.Services
using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow._context.GetStreamRoleSettings(guild.Id);
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
streamRoleSettings.Keyword = keyword;
UpdateCache(guild.Id, streamRoleSettings);
@@ -170,7 +170,7 @@ namespace NadekoBot.Modules.Utility.Services
StreamRoleSettings setting;
using (var uow = _db.GetDbContext())
{
setting = uow._context.GetStreamRoleSettings(guildId);
setting = uow.GetStreamRoleSettings(guildId);
}
UpdateCache(guildId, setting);
@@ -192,7 +192,7 @@ namespace NadekoBot.Modules.Utility.Services
StreamRoleSettings setting;
using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow._context.GetStreamRoleSettings(fromRole.Guild.Id);
var streamRoleSettings = uow.GetStreamRoleSettings(fromRole.Guild.Id);
streamRoleSettings.Enabled = true;
streamRoleSettings.AddRoleId = addRole.Id;
@@ -219,7 +219,7 @@ namespace NadekoBot.Modules.Utility.Services
{
using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow._context.GetStreamRoleSettings(guild.Id);
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
streamRoleSettings.Enabled = false;
streamRoleSettings.AddRoleId = 0;
streamRoleSettings.FromRoleId = 0;

View File

@@ -61,7 +61,7 @@ namespace NadekoBot.Modules.Utility.Services
{
using (var uow = _db.GetDbContext())
{
var gc = uow._context.GuildConfigsForId(guildId, set => set);
var gc = uow.GuildConfigsForId(guildId, set => set);
if (enabled==null) enabled = gc.VerboseErrors = !gc.VerboseErrors; // Old behaviour, now behind a condition
else gc.VerboseErrors = (bool)enabled; // New behaviour, just set it.