diff --git a/src/NadekoBot/Modules/Gambling/Services/GamblingService.cs b/src/NadekoBot/Modules/Gambling/Services/GamblingService.cs index 2d225188c..722c5faa1 100644 --- a/src/NadekoBot/Modules/Gambling/Services/GamblingService.cs +++ b/src/NadekoBot/Modules/Gambling/Services/GamblingService.cs @@ -138,7 +138,7 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU cash = uow.DiscordUsers.GetTotalCurrency(); onePercent = uow.DiscordUsers.GetTopOnePercentCurrency(_client.CurrentUser.Id); planted = uow._context.PlantedCurrency.AsQueryable().Sum(x => x.Amount); - waifus = uow.Waifus.GetTotalValue(); + waifus = uow._context.WaifuInfo.GetTotalValue(); bot = uow.DiscordUsers.GetUserCurrency(_client.CurrentUser.Id); } diff --git a/src/NadekoBot/Modules/Gambling/Services/WaifuService.cs b/src/NadekoBot/Modules/Gambling/Services/WaifuService.cs index 7a97b93ce..76008f7c6 100644 --- a/src/NadekoBot/Modules/Gambling/Services/WaifuService.cs +++ b/src/NadekoBot/Modules/Gambling/Services/WaifuService.cs @@ -46,7 +46,7 @@ namespace NadekoBot.Modules.Gambling.Services using (var uow = _db.GetDbContext()) { - var waifu = uow.Waifus.ByWaifuUserId(waifuId); + var waifu = uow._context.WaifuInfo.ByWaifuUserId(waifuId); var ownerUser = uow.DiscordUsers.GetOrCreate(owner); // owner has to be the owner of the waifu @@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Gambling.Services var settings = _gss.Data; using (var uow = _db.GetDbContext()) { - var waifu = uow.Waifus.ByWaifuUserId(user.Id); + var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id); if (waifu == null) return settings.Waifu.MinPrice; @@ -143,7 +143,7 @@ namespace NadekoBot.Modules.Gambling.Services uow._context.WaifuUpdates.RemoveRange(affs); //reset divorces to 0 uow._context.WaifuUpdates.RemoveRange(divorces); - var waifu = uow.Waifus.ByWaifuUserId(user.Id); + var waifu = uow._context.WaifuInfo.ByWaifuUserId(user.Id); //reset price, remove items //remove owner, remove affinity waifu.Price = 50; @@ -167,7 +167,7 @@ namespace NadekoBot.Modules.Gambling.Services bool isAffinity; using (var uow = _db.GetDbContext()) { - w = uow.Waifus.ByWaifuUserId(target.Id); + w = uow._context.WaifuInfo.ByWaifuUserId(target.Id); isAffinity = (w?.Affinity?.UserId == user.Id); if (w == null) { @@ -179,7 +179,7 @@ namespace NadekoBot.Modules.Gambling.Services } else { - uow.Waifus.Add(w = new WaifuInfo() + uow._context.WaifuInfo.Add(w = new WaifuInfo() { Waifu = waifu, Claimer = claimer, @@ -257,7 +257,7 @@ namespace NadekoBot.Modules.Gambling.Services TimeSpan? remaining = null; using (var uow = _db.GetDbContext()) { - var w = uow.Waifus.ByWaifuUserId(user.Id); + var w = uow._context.WaifuInfo.ByWaifuUserId(user.Id); var newAff = target == null ? null : uow.DiscordUsers.GetOrCreate(target); if (w?.Affinity?.UserId == target?.Id) { @@ -268,7 +268,7 @@ namespace NadekoBot.Modules.Gambling.Services else if (w == null) { var thisUser = uow.DiscordUsers.GetOrCreate(user); - uow.Waifus.Add(new WaifuInfo() + uow._context.WaifuInfo.Add(new WaifuInfo() { Affinity = newAff, Waifu = thisUser, @@ -311,14 +311,14 @@ namespace NadekoBot.Modules.Gambling.Services { using (var uow = _db.GetDbContext()) { - return uow.Waifus.GetTop(9, page * 9); + return uow._context.WaifuInfo.GetTop(9, page * 9); } } public ulong GetWaifuUserId(ulong ownerId, string name) { using var uow = _db.GetDbContext(); - return uow.Waifus.GetWaifuUserId(ownerId, name); + return uow._context.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.Waifus.ByWaifuUserId(targetId); + w = uow._context.WaifuInfo.ByWaifuUserId(targetId); var now = DateTime.UtcNow; if (w?.Claimer == null || w.Claimer.UserId != user.Id) result = DivorceResult.NotYourWife; @@ -381,12 +381,12 @@ namespace NadekoBot.Modules.Gambling.Services using (var uow = _db.GetDbContext()) { - var w = uow.Waifus.ByWaifuUserId(giftedWaifu.Id, + var w = uow._context.WaifuInfo.ByWaifuUserId(giftedWaifu.Id, set => set.Include(x => x.Items) .Include(x => x.Claimer)); if (w == null) { - uow.Waifus.Add(w = new WaifuInfo() + uow._context.WaifuInfo.Add(w = new WaifuInfo() { Affinity = null, Claimer = null, @@ -420,7 +420,7 @@ namespace NadekoBot.Modules.Gambling.Services { using (var uow = _db.GetDbContext()) { - var wi = uow.Waifus.GetWaifuInfo(targetId); + var wi = uow._context.GetWaifuInfo(targetId); if (wi is null) { wi = new WaifuInfoStats diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index 9dff44478..4606aef20 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -7,7 +7,6 @@ namespace NadekoBot.Core.Services.Database public interface IUnitOfWork : IDisposable { NadekoContext _context { get; } - IWaifuRepository Waifus { get; } IDiscordUserRepository DiscordUsers { get; } int SaveChanges(); diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index bfbdf92a1..0dd56dc37 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -59,6 +59,7 @@ namespace NadekoBot.Core.Services.Database public DbSet MusicPlayerSettings { get; set; } public DbSet Repeaters { get; set; } public DbSet Poll { get; set; } + public DbSet WaifuInfo { get; set; } public NadekoContext(DbContextOptions options) : base(options) { diff --git a/src/NadekoBot/Services/Database/Repositories/IWaifuRepository.cs b/src/NadekoBot/Services/Database/Repositories/IWaifuRepository.cs deleted file mode 100644 index d810ffd75..000000000 --- a/src/NadekoBot/Services/Database/Repositories/IWaifuRepository.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using NadekoBot.Core.Services.Database.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using NadekoBot.Services.Database.Repositories; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface IWaifuRepository : IRepository - { - IEnumerable GetTop(int count, int skip = 0); - WaifuInfo ByWaifuUserId(ulong userId, Func, IQueryable> includes = null); - IEnumerable GetWaifuNames(ulong userId); - decimal GetTotalValue(); - int AffinityCount(ulong userId); - WaifuInfoStats GetWaifuInfo(ulong id); - public ulong GetWaifuUserId(ulong ownerId, string name); - } - - public class WaifuInfoStats - { - public string FullName { get; set; } - public int Price { get; set; } - public string ClaimerName { get; set; } - public string AffinityName { get; set; } - public int AffinityCount { get; set; } - public int DivorceCount { get; set; } - public int ClaimCount { get; set; } - public List Items { get; set; } - public List Claims { get; set; } - public List Fans { get; set; } - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/WaifuRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/WaifuExtensions.cs similarity index 61% rename from src/NadekoBot/Services/Database/Repositories/Impl/WaifuRepository.cs rename to src/NadekoBot/Services/Database/Repositories/Impl/WaifuExtensions.cs index 9875b5057..9571be76f 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/WaifuRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/WaifuExtensions.cs @@ -1,69 +1,55 @@ -using Microsoft.EntityFrameworkCore; -using NadekoBot.Services.Database.Models; + using System; using System.Collections.Generic; using System.Linq; +using Microsoft.EntityFrameworkCore; +using NadekoBot.Core.Services.Database; using NadekoBot.Core.Services.Database.Models; -using NadekoBot.Core.Services.Database.Repositories; +using NadekoBot.Migrations; +using NadekoBot.Services.Database.Models; -namespace NadekoBot.Services.Database.Repositories.Impl +namespace NadekoBot.Modules.Gambling { - public class WaifuRepository : Repository, IWaifuRepository + public class WaifuInfoStats { - public WaifuRepository(DbContext context) : base(context) - { - } - - public WaifuInfo ByWaifuUserId(ulong userId, Func, IQueryable> includes = null) + public string FullName { get; set; } + public int Price { get; set; } + public string ClaimerName { get; set; } + public string AffinityName { get; set; } + public int AffinityCount { get; set; } + public int DivorceCount { get; set; } + public int ClaimCount { get; set; } + public List Items { get; set; } + public List Claims { get; set; } + public List Fans { get; set; } + } + + public static class WaifuExtensions + { + public static WaifuInfo ByWaifuUserId(this DbSet waifus, ulong userId, Func, IQueryable> includes = null) { if (includes == null) { - return _set.Include(wi => wi.Waifu) + return waifus.Include(wi => wi.Waifu) .Include(wi => wi.Affinity) .Include(wi => wi.Claimer) .Include(wi => wi.Items) - .FirstOrDefault(wi => wi.WaifuId == _context.Set() - .AsQueryable() - .Where(x => x.UserId == userId) - .Select(x => x.Id) - .FirstOrDefault()); + .FirstOrDefault(wi => wi.Waifu.UserId == userId); } - return includes(_set) + return includes(waifus) .AsQueryable() - .FirstOrDefault(wi => wi.WaifuId == _context.Set() - .AsQueryable() - .Where(x => x.UserId == userId) - .Select(x => x.Id) - .FirstOrDefault()); + .FirstOrDefault(wi => wi.Waifu.UserId == userId); } - public IEnumerable GetWaifuNames(ulong userId) - { - var waifus = _set.AsQueryable().Where(x => x.ClaimerId != null && - x.ClaimerId == _context.Set() - .AsQueryable() - .Where(y => y.UserId == userId) - .Select(y => y.Id) - .FirstOrDefault()) - .Select(x => x.WaifuId); - - return _context.Set() - .AsQueryable() - .Where(x => waifus.Contains(x.Id)) - .Select(x => x.Username + "#" + x.Discriminator) - .ToList(); - - } - - public IEnumerable GetTop(int count, int skip = 0) + public static IEnumerable GetTop(this DbSet waifus, int count, int skip = 0) { if (count < 0) throw new ArgumentOutOfRangeException(nameof(count)); if (count == 0) return new List(); - return _set.Include(wi => wi.Waifu) + return waifus.Include(wi => wi.Waifu) .Include(wi => wi.Affinity) .Include(wi => wi.Claimer) .OrderByDescending(wi => wi.Price) @@ -83,22 +69,17 @@ namespace NadekoBot.Services.Database.Repositories.Impl } - public decimal GetTotalValue() + public static decimal GetTotalValue(this DbSet waifus) { - return _set + return waifus .AsQueryable() .Where(x => x.ClaimerId != null) .Sum(x => x.Price); } - public int AffinityCount(ulong userId) + public static int AffinityCount(this DbSet updates, ulong userId) { - //return _context.Set() - // .Count(w => w.User.UserId == userId && - // w.UpdateType == WaifuUpdateType.AffinityChanged && - // w.New != null)); - - return _context.Set() + return updates .FromSqlInterpolated($@"SELECT 1 FROM WaifuUpdates WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND @@ -107,9 +88,9 @@ WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND .Count(); } - public ulong GetWaifuUserId(ulong ownerId, string name) + public static ulong GetWaifuUserId(this DbSet waifus, ulong ownerId, string name) { - return _set + return waifus .AsQueryable() .AsNoTracking() .Where(x => x.Claimer.UserId == ownerId @@ -118,48 +99,49 @@ WHERE UserId = (SELECT Id from DiscordUser WHERE UserId={userId}) AND .FirstOrDefault(); } - public WaifuInfoStats GetWaifuInfo(ulong userId) + public static WaifuInfoStats GetWaifuInfo(this NadekoContext ctx, ulong userId) { - _context.Database.ExecuteSqlInterpolated($@" + ctx.Database.ExecuteSqlInterpolated($@" INSERT OR IGNORE INTO WaifuInfo (AffinityId, ClaimerId, Price, WaifuId) VALUES ({null}, {null}, {1}, (SELECT Id FROM DiscordUser WHERE UserId={userId}));"); - var toReturn = _set.AsQueryable() - .Where(w => w.WaifuId == _context.Set() + var toReturn = ctx.WaifuInfo + .AsQueryable() + .Where(w => w.WaifuId == ctx.Set() .AsQueryable() .Where(u => u.UserId == userId) .Select(u => u.Id).FirstOrDefault()) .Select(w => new WaifuInfoStats { - FullName = _context.Set() + FullName = ctx.Set() .AsQueryable() .Where(u => u.UserId == userId) .Select(u => u.Username + "#" + u.Discriminator) .FirstOrDefault(), - AffinityCount = _context.Set() + AffinityCount = ctx.Set() .AsQueryable() .Count(x => x.UserId == w.WaifuId && x.UpdateType == WaifuUpdateType.AffinityChanged && x.NewId != null), - AffinityName = _context.Set() + AffinityName = ctx.Set() .AsQueryable() .Where(u => u.Id == w.AffinityId) .Select(u => u.Username + "#" + u.Discriminator) .FirstOrDefault(), - ClaimCount = _set + ClaimCount = ctx.WaifuInfo .AsQueryable() .Count(x => x.ClaimerId == w.WaifuId), - ClaimerName = _context.Set() + ClaimerName = ctx.Set() .AsQueryable() .Where(u => u.Id == w.ClaimerId) .Select(u => u.Username + "#" + u.Discriminator) .FirstOrDefault(), - DivorceCount = _context + DivorceCount = ctx .Set() .AsQueryable() .Count(x => x.OldId == w.WaifuId && @@ -168,14 +150,14 @@ VALUES ({null}, {null}, {1}, (SELECT Id FROM DiscordUser WHERE UserId={userId})) Price = w.Price, - Claims = _set + Claims = ctx.WaifuInfo .AsQueryable() .Include(x => x.Waifu) .Where(x => x.ClaimerId == w.WaifuId) .Select(x => x.Waifu.Username + "#" + x.Waifu.Discriminator) .ToList(), - Fans = _set + Fans = ctx.WaifuInfo .AsQueryable() .Include(x => x.Waifu) .Where(x => x.AffinityId == w.WaifuId) diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index a11d16f03..e82b1fb54 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -11,9 +11,6 @@ namespace NadekoBot.Services.Database { public NadekoContext _context { get; } - private IWaifuRepository _waifus; - public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context)); - private IDiscordUserRepository _discordUsers; public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context));