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

@@ -51,7 +51,7 @@ namespace NadekoBot.Modules.Gambling
{
using (var uow = _db.GetDbContext())
{
return n(uow._context.DiscordUser.GetUserCurrency(id));
return n(uow.DiscordUser.GetUserCurrency(id));
}
}
@@ -193,7 +193,7 @@ namespace NadekoBot.Modules.Gambling
var trs = new List<CurrencyTransaction>();
using (var uow = _db.GetDbContext())
{
trs = uow._context.CurrencyTransactions.GetPageFor(userId, page);
trs = uow.CurrencyTransactions.GetPageFor(userId, page);
}
var embed = new EmbedBuilder()
@@ -522,7 +522,7 @@ namespace NadekoBot.Modules.Gambling
using (var uow = _db.GetDbContext())
{
cleanRichest = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 10_000);
cleanRichest = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 10_000);
}
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);
@@ -536,7 +536,7 @@ namespace NadekoBot.Modules.Gambling
{
using (var uow = _db.GetDbContext())
{
cleanRichest = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
cleanRichest = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
}
}
@@ -551,7 +551,7 @@ namespace NadekoBot.Modules.Gambling
{
using (var uow = _db.GetDbContext())
{
toSend = uow._context.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, curPage);
toSend = uow.DiscordUser.GetTopRichest(_client.CurrentUser.Id, 9, curPage);
}
}
else

View File

@@ -63,7 +63,7 @@ namespace NadekoBot.Modules.Gambling.Services
if (maxDecay == 0)
maxDecay = int.MaxValue;
uow._context.Database.ExecuteSqlInterpolated($@"
uow.Database.ExecuteSqlInterpolated($@"
UPDATE DiscordUser
SET CurrencyAmount=
CASE WHEN
@@ -80,32 +80,6 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU
}
}, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
}
//using (var uow = _db.UnitOfWork)
//{
// //refund all of the currency users had at stake in gambling games
// //at the time bot was restarted
// var stakes = uow._context.Set<Stake>()
// .ToArray();
// var userIds = stakes.Select(x => x.UserId).ToArray();
// var reasons = stakes.Select(x => "Stake-" + x.Source).ToArray();
// var amounts = stakes.Select(x => x.Amount).ToArray();
// _cs.AddBulkAsync(userIds, reasons, amounts, gamble: true).ConfigureAwait(false);
// foreach (var s in stakes)
// {
// _cs.AddAsync(s.UserId, "Stake-" + s.Source, s.Amount, gamble: true)
// .GetAwaiter()
// .GetResult();
// }
// uow._context.Set<Stake>().RemoveRange(stakes);
// uow.Complete();
// Log.Information("Refunded {0} users' stakes.", stakes.Length);
//}
}
public struct EconomyResult
@@ -136,11 +110,11 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU
using (var uow = _db.GetDbContext())
{
cash = uow._context.DiscordUser.GetTotalCurrency();
onePercent = uow._context.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
planted = uow._context.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
waifus = uow._context.WaifuInfo.GetTotalValue();
bot = uow._context.DiscordUser.GetUserCurrency(_client.CurrentUser.Id);
cash = uow.DiscordUser.GetTotalCurrency();
onePercent = uow.DiscordUser.GetTopOnePercentCurrency(_client.CurrentUser.Id);
planted = uow.PlantedCurrency.AsQueryable().Sum(x => x.Amount);
waifus = uow.WaifuInfo.GetTotalValue();
bot = uow.DiscordUser.GetUserCurrency(_client.CurrentUser.Id);
}
var result = new EconomyResult

View File

@@ -19,8 +19,8 @@ namespace NadekoBot.Modules.Gambling.Services
_db = db;
}
private IndexedCollection<ShopEntry> GetEntriesInternal(IUnitOfWork uow, ulong guildId) =>
uow._context.GuildConfigsForId(
private IndexedCollection<ShopEntry> GetEntriesInternal(NadekoContext uow, ulong guildId) =>
uow.GuildConfigsForId(
guildId,
set => set.Include(x => x.ShopEntries).ThenInclude(x => x.Items)
)

View File

@@ -60,7 +60,7 @@ namespace NadekoBot.Modules.Gambling.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.GenerateCurrencyChannelIds)
.Where(x => guildIds.Contains(x.GuildId))
@@ -79,7 +79,7 @@ namespace NadekoBot.Modules.Gambling.Services
bool enabled;
using (var uow = _db.GetDbContext())
{
var guildConfig = uow._context.GuildConfigsForId(gid, set => set.Include(gc => gc.GenerateCurrencyChannelIds));
var guildConfig = uow.GuildConfigsForId(gid, set => set.Include(gc => gc.GenerateCurrencyChannelIds));
var toAdd = new GCChannelId() { ChannelId = cid };
if (!guildConfig.GenerateCurrencyChannelIds.Contains(toAdd))
@@ -93,7 +93,7 @@ namespace NadekoBot.Modules.Gambling.Services
var toDelete = guildConfig.GenerateCurrencyChannelIds.FirstOrDefault(x => x.Equals(toAdd));
if (toDelete != null)
{
uow._context.Remove(toDelete);
uow.Remove(toDelete);
}
_generationChannels.TryRemove(cid);
enabled = false;
@@ -107,7 +107,7 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
var chs = uow._context.GuildConfigs.GetGeneratingChannels();
var chs = uow.GuildConfigs.GetGeneratingChannels();
return chs;
}
}
@@ -270,7 +270,7 @@ namespace NadekoBot.Modules.Gambling.Services
pass = pass?.Trim().TrimTo(10, hideDots: true).ToUpperInvariant();
// gets all plants in this channel with the same password
var entries = uow._context.PlantedCurrency
var entries = uow.PlantedCurrency
.AsQueryable()
.Where(x => x.ChannelId == ch.Id && pass == x.Password)
.ToList();
@@ -278,7 +278,7 @@ namespace NadekoBot.Modules.Gambling.Services
amount = entries.Sum(x => x.Amount);
ids = entries.Select(x => x.MessageId).ToArray();
// remove them from the database
uow._context.RemoveRange(entries);
uow.RemoveRange(entries);
if (amount > 0)
@@ -369,7 +369,7 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
uow._context.PlantedCurrency.Add(new PlantedCurrency
uow.PlantedCurrency.Add(new PlantedCurrency
{
Amount = amount,
GuildId = gid,

View File

@@ -46,8 +46,8 @@ namespace NadekoBot.Modules.Gambling.Services
using (var uow = _db.GetDbContext())
{
var waifu = uow._context.WaifuInfo.ByWaifuUserId(waifuId);
var ownerUser = uow._context.GetOrCreateUser(owner);
var waifu = uow.WaifuInfo.ByWaifuUserId(waifuId);
var ownerUser = uow.GetOrCreateUser(owner);
// owner has to be the owner of the waifu
if (waifu == null || waifu.ClaimerId != ownerUser.Id)
@@ -82,7 +82,7 @@ namespace NadekoBot.Modules.Gambling.Services
}
//new claimerId is the id of the new owner
var newOwnerUser = uow._context.GetOrCreateUser(newOwner);
var newOwnerUser = uow.GetOrCreateUser(newOwner);
waifu.ClaimerId = newOwnerUser.Id;
await uow.SaveChangesAsync();
@@ -96,16 +96,16 @@ namespace NadekoBot.Modules.Gambling.Services
var settings = _gss.Data;
using (var uow = _db.GetDbContext())
{
var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
var waifu = uow.WaifuInfo.ByWaifuUserId(user.Id);
if (waifu == null)
return settings.Waifu.MinPrice;
var divorces = uow._context.WaifuUpdates.Count(x => x.Old != null &&
var divorces = uow.WaifuUpdates.Count(x => x.Old != null &&
x.Old.UserId == user.Id &&
x.UpdateType == WaifuUpdateType.Claimed &&
x.New == null);
var affs = uow._context.WaifuUpdates
var affs = uow.WaifuUpdates
.AsQueryable()
.Where(w => w.User.UserId == user.Id && w.UpdateType == WaifuUpdateType.AffinityChanged &&
w.New != null)
@@ -126,13 +126,13 @@ namespace NadekoBot.Modules.Gambling.Services
if (!await _cs.RemoveAsync(user.Id, "Waifu Reset", price, gamble: true))
return false;
var affs = uow._context.WaifuUpdates
var affs = uow.WaifuUpdates
.AsQueryable()
.Where(w => w.User.UserId == user.Id
&& w.UpdateType == WaifuUpdateType.AffinityChanged
&& w.New != null);
var divorces = uow._context.WaifuUpdates
var divorces = uow.WaifuUpdates
.AsQueryable()
.Where(x => x.Old != null &&
x.Old.UserId == user.Id &&
@@ -140,10 +140,10 @@ namespace NadekoBot.Modules.Gambling.Services
x.New == null);
//reset changes of heart to 0
uow._context.WaifuUpdates.RemoveRange(affs);
uow.WaifuUpdates.RemoveRange(affs);
//reset divorces to 0
uow._context.WaifuUpdates.RemoveRange(divorces);
var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
uow.WaifuUpdates.RemoveRange(divorces);
var waifu = uow.WaifuInfo.ByWaifuUserId(user.Id);
//reset price, remove items
//remove owner, remove affinity
waifu.Price = 50;
@@ -167,26 +167,26 @@ namespace NadekoBot.Modules.Gambling.Services
bool isAffinity;
using (var uow = _db.GetDbContext())
{
w = uow._context.WaifuInfo.ByWaifuUserId(target.Id);
w = uow.WaifuInfo.ByWaifuUserId(target.Id);
isAffinity = (w?.Affinity?.UserId == user.Id);
if (w == null)
{
var claimer = uow._context.GetOrCreateUser(user);
var waifu = uow._context.GetOrCreateUser(target);
var claimer = uow.GetOrCreateUser(user);
var waifu = uow.GetOrCreateUser(target);
if (!await _cs.RemoveAsync(user.Id, "Claimed Waifu", amount, gamble: true))
{
result = WaifuClaimResult.NotEnoughFunds;
}
else
{
uow._context.WaifuInfo.Add(w = new WaifuInfo()
uow.WaifuInfo.Add(w = new WaifuInfo()
{
Waifu = waifu,
Claimer = claimer,
Affinity = null,
Price = amount
});
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = waifu,
Old = null,
@@ -205,11 +205,11 @@ namespace NadekoBot.Modules.Gambling.Services
else
{
var oldClaimer = w.Claimer;
w.Claimer = uow._context.GetOrCreateUser(user);
w.Claimer = uow.GetOrCreateUser(user);
w.Price = amount + (amount / 4);
result = WaifuClaimResult.Success;
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = w.Waifu,
Old = oldClaimer,
@@ -227,11 +227,11 @@ namespace NadekoBot.Modules.Gambling.Services
else
{
var oldClaimer = w.Claimer;
w.Claimer = uow._context.GetOrCreateUser(user);
w.Claimer = uow.GetOrCreateUser(user);
w.Price = amount;
result = WaifuClaimResult.Success;
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = w.Waifu,
Old = oldClaimer,
@@ -257,8 +257,8 @@ namespace NadekoBot.Modules.Gambling.Services
TimeSpan? remaining = null;
using (var uow = _db.GetDbContext())
{
var w = uow._context.WaifuInfo.ByWaifuUserId(user.Id);
var newAff = target == null ? null : uow._context.GetOrCreateUser(target);
var w = uow.WaifuInfo.ByWaifuUserId(user.Id);
var newAff = target == null ? null : uow.GetOrCreateUser(target);
if (w?.Affinity?.UserId == target?.Id)
{
}
@@ -267,8 +267,8 @@ namespace NadekoBot.Modules.Gambling.Services
}
else if (w == null)
{
var thisUser = uow._context.GetOrCreateUser(user);
uow._context.WaifuInfo.Add(new WaifuInfo()
var thisUser = uow.GetOrCreateUser(user);
uow.WaifuInfo.Add(new WaifuInfo()
{
Affinity = newAff,
Waifu = thisUser,
@@ -277,7 +277,7 @@ namespace NadekoBot.Modules.Gambling.Services
});
success = true;
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = thisUser,
Old = null,
@@ -292,7 +292,7 @@ namespace NadekoBot.Modules.Gambling.Services
w.Affinity = newAff;
success = true;
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = w.Waifu,
Old = oldAff,
@@ -311,14 +311,14 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
return uow._context.WaifuInfo.GetTop(9, page * 9);
return uow.WaifuInfo.GetTop(9, page * 9);
}
}
public ulong GetWaifuUserId(ulong ownerId, string name)
{
using var uow = _db.GetDbContext();
return uow._context.WaifuInfo.GetWaifuUserId(ownerId, name);
return uow.WaifuInfo.GetWaifuUserId(ownerId, name);
}
public async Task<(WaifuInfo, DivorceResult, long, TimeSpan?)> DivorceWaifuAsync(IUser user, ulong targetId)
@@ -329,7 +329,7 @@ namespace NadekoBot.Modules.Gambling.Services
WaifuInfo w = null;
using (var uow = _db.GetDbContext())
{
w = uow._context.WaifuInfo.ByWaifuUserId(targetId);
w = uow.WaifuInfo.ByWaifuUserId(targetId);
var now = DateTime.UtcNow;
if (w?.Claimer == null || w.Claimer.UserId != user.Id)
result = DivorceResult.NotYourWife;
@@ -357,7 +357,7 @@ namespace NadekoBot.Modules.Gambling.Services
var oldClaimer = w.Claimer;
w.Claimer = null;
uow._context.WaifuUpdates.Add(new WaifuUpdate()
uow.WaifuUpdates.Add(new WaifuUpdate()
{
User = w.Waifu,
Old = oldClaimer,
@@ -381,17 +381,17 @@ namespace NadekoBot.Modules.Gambling.Services
using (var uow = _db.GetDbContext())
{
var w = uow._context.WaifuInfo.ByWaifuUserId(giftedWaifu.Id,
var w = uow.WaifuInfo.ByWaifuUserId(giftedWaifu.Id,
set => set.Include(x => x.Items)
.Include(x => x.Claimer));
if (w == null)
{
uow._context.WaifuInfo.Add(w = new WaifuInfo()
uow.WaifuInfo.Add(w = new WaifuInfo()
{
Affinity = null,
Claimer = null,
Price = 1,
Waifu = uow._context.GetOrCreateUser(giftedWaifu),
Waifu = uow.GetOrCreateUser(giftedWaifu),
});
}
@@ -420,7 +420,7 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
var wi = uow._context.GetWaifuInfo(targetId);
var wi = uow.GetWaifuInfo(targetId);
if (wi is null)
{
wi = new WaifuInfoStats
@@ -445,7 +445,7 @@ namespace NadekoBot.Modules.Gambling.Services
{
using (var uow = _db.GetDbContext())
{
var du = uow._context.GetOrCreateUser(target);
var du = uow.GetOrCreateUser(target);
return GetFullWaifuInfoAsync(target.Id);
}

View File

@@ -49,7 +49,7 @@ namespace NadekoBot.Modules.Gambling
throw new ArgumentOutOfRangeException(nameof(page));
using var uow = _db.GetDbContext();
var entries = uow._context.GuildConfigsForId(ctx.Guild.Id,
var entries = uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries
.ToIndexed();
@@ -95,7 +95,7 @@ namespace NadekoBot.Modules.Gambling
ShopEntry entry;
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(ctx.Guild.Id, set => set
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set
.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items));
var entries = new IndexedCollection<ShopEntry>(config.ShopEntries);
@@ -165,7 +165,7 @@ namespace NadekoBot.Modules.Gambling
{
using (var uow = _db.GetDbContext())
{
var x = uow._context.Set<ShopEntryItem>().Remove(item);
var x = uow.Set<ShopEntryItem>().Remove(item);
uow.SaveChanges();
}
try
@@ -189,7 +189,7 @@ namespace NadekoBot.Modules.Gambling
entry.Price).ConfigureAwait(false);
using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow._context.GuildConfigsForId(ctx.Guild.Id,
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries);
entry = entries.ElementAtOrDefault(index);
@@ -235,13 +235,13 @@ namespace NadekoBot.Modules.Gambling
};
using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow._context.GuildConfigsForId(ctx.Guild.Id,
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries)
{
entry
};
uow._context.GuildConfigsForId(ctx.Guild.Id, set => set).ShopEntries = entries;
uow.GuildConfigsForId(ctx.Guild.Id, set => set).ShopEntries = entries;
uow.SaveChanges();
}
await ctx.Channel.EmbedAsync(EntryToEmbed(entry)
@@ -263,13 +263,13 @@ namespace NadekoBot.Modules.Gambling
};
using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow._context.GuildConfigsForId(ctx.Guild.Id,
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries)
{
entry
};
uow._context.GuildConfigsForId(ctx.Guild.Id, set => set).ShopEntries = entries;
uow.GuildConfigsForId(ctx.Guild.Id, set => set).ShopEntries = entries;
uow.SaveChanges();
}
await ctx.Channel.EmbedAsync(EntryToEmbed(entry)
@@ -293,7 +293,7 @@ namespace NadekoBot.Modules.Gambling
bool added = false;
using (var uow = _db.GetDbContext())
{
var entries = new IndexedCollection<ShopEntry>(uow._context.GuildConfigsForId(ctx.Guild.Id,
var entries = new IndexedCollection<ShopEntry>(uow.GuildConfigsForId(ctx.Guild.Id,
set => set.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items)).ShopEntries);
entry = entries.ElementAtOrDefault(index);
@@ -326,7 +326,7 @@ namespace NadekoBot.Modules.Gambling
ShopEntry removed;
using (var uow = _db.GetDbContext())
{
var config = uow._context.GuildConfigsForId(ctx.Guild.Id, set => set
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set
.Include(x => x.ShopEntries)
.ThenInclude(x => x.Items));
@@ -334,8 +334,8 @@ namespace NadekoBot.Modules.Gambling
removed = entries.ElementAtOrDefault(index);
if (removed != null)
{
uow._context.RemoveRange(removed.Items);
uow._context.Remove(removed);
uow.RemoveRange(removed.Items);
uow.Remove(removed);
uow.SaveChanges();
}
}