Converted many raw sql queries to their linq2db equivalents

This commit is contained in:
Kwoth
2022-03-20 19:31:04 +01:00
parent fd032d3e91
commit 86e728b753
6 changed files with 36 additions and 97 deletions

View File

@@ -102,17 +102,16 @@ public class GamblingService : INService, IReadyExecutor
if (maxDecay == 0)
maxDecay = int.MaxValue;
await uow.Database.ExecuteSqlInterpolatedAsync($@"
UPDATE DiscordUser
SET CurrencyAmount=
CASE WHEN
{maxDecay} > ROUND(CurrencyAmount * {config.Decay.Percent} - 0.5)
THEN
CurrencyAmount - ROUND(CurrencyAmount * {config.Decay.Percent} - 0.5)
ELSE
CurrencyAmount - {maxDecay}
END
WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentUser.Id};");
var decay = (double)config.Decay.Percent;
await uow.DiscordUser
.Where(x => x.CurrencyAmount > config.Decay.MinThreshold && x.UserId != _client.CurrentUser.Id)
.UpdateAsync(old => new()
{
CurrencyAmount =
maxDecay > Sql.Round((old.CurrencyAmount * decay) - 0.5)
? (long)(old.CurrencyAmount - Sql.Round((old.CurrencyAmount * decay) - 0.5))
: old.CurrencyAmount - maxDecay
});
_cache.SetLastCurrencyDecay();
await uow.SaveChangesAsync();

View File

@@ -236,9 +236,9 @@ public partial class Gambling
public partial Task WaifuInfo(ulong targetId)
=> InternalWaifuInfo(targetId);
private Task InternalWaifuInfo(ulong targetId, string name = null)
private async Task InternalWaifuInfo(ulong targetId, string name = null)
{
var wi = _service.GetFullWaifuInfoAsync(targetId);
var wi = await _service.GetFullWaifuInfoAsync(targetId);
var affInfo = _service.GetAffinityTitle(wi.AffinityCount);
var waifuItems = _service.GetWaifuItems().ToDictionary(x => x.ItemEmoji, x => x);
@@ -280,7 +280,7 @@ public partial class Gambling
true)
.AddField(GetText(strs.gifts), itemsStr, true);
return ctx.Channel.EmbedAsync(embed);
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]

View File

@@ -385,10 +385,10 @@ public class WaifuService : INService, IReadyExecutor
return true;
}
public WaifuInfoStats GetFullWaifuInfoAsync(ulong targetId)
public async Task<WaifuInfoStats> GetFullWaifuInfoAsync(ulong targetId)
{
using var uow = _db.GetDbContext();
var wi = uow.GetWaifuInfo(targetId);
await using var uow = _db.GetDbContext();
var wi = await uow.GetWaifuInfoAsync(targetId);
if (wi is null)
{
wi = new()
@@ -409,12 +409,12 @@ public class WaifuService : INService, IReadyExecutor
return wi;
}
public WaifuInfoStats GetFullWaifuInfoAsync(IGuildUser target)
public async Task<WaifuInfoStats> GetFullWaifuInfoAsync(IGuildUser target)
{
using var uow = _db.GetDbContext();
await using var uow = _db.GetDbContext();
_ = uow.GetOrCreateUser(target);
return GetFullWaifuInfoAsync(target.Id);
return await GetFullWaifuInfoAsync(target.Id);
}
public string GetClaimTitle(int count)

View File

@@ -78,8 +78,6 @@ public class RemindService : INService
.ToLinqToDBTable()
.Where(x => x.ServerId / 4194304 % (ulong)_creds.TotalShards == (ulong)_client.ShardId
&& x.When < now)
// .FromSqlInterpolated(
// $"select * from reminders where ((serverid >> 22) % {_creds.TotalShards}) == {_client.ShardId} and \"when\" < {now};")
.ToListAsyncLinqToDB();
}