diff --git a/src/NadekoBot/Modules/Xp/Services/ClubService.cs b/src/NadekoBot/Modules/Xp/Services/ClubService.cs index d13ba1d6f..1a1f6f253 100644 --- a/src/NadekoBot/Modules/Xp/Services/ClubService.cs +++ b/src/NadekoBot/Modules/Xp/Services/ClubService.cs @@ -1,11 +1,13 @@ using NadekoBot.Core.Services; using System; +using System.Collections.Generic; using NadekoBot.Core.Services.Database.Models; using Discord; using System.Linq; using NadekoBot.Extensions; using System.Net.Http; using System.Threading.Tasks; +using NadekoBot.Modules.Xp.Common; namespace NadekoBot.Modules.Xp.Services { @@ -25,34 +27,32 @@ namespace NadekoBot.Modules.Xp.Services //must be lvl 5 and must not be in a club already club = null; - using (var uow = _db.GetDbContext()) + using var uow = _db.GetDbContext(); + var du = uow.DiscordUsers.GetOrCreate(user); + uow._context.SaveChanges(); + var xp = new LevelStats(du.TotalXp); + + if (xp.Level >= 5 && du.Club == null) { - var du = uow.DiscordUsers.GetOrCreate(user); - uow._context.SaveChanges(); - var xp = new LevelStats(du.TotalXp); - - if (xp.Level >= 5 && du.Club == null) + du.IsClubAdmin = true; + du.Club = new ClubInfo() { - du.IsClubAdmin = true; - du.Club = new ClubInfo() - { - Name = clubName, - Discrim = uow.Clubs.GetNextDiscrim(clubName), - Owner = du, - }; - uow.Clubs.Add(du.Club); - uow._context.SaveChanges(); - } - else - return false; - - uow._context.Set() - .RemoveRange(uow._context.Set() - .AsQueryable() - .Where(x => x.UserId == du.Id)); - club = du.Club; - uow.SaveChanges(); + Name = clubName, + Discrim = uow._context.Clubs.GetNextDiscrim(clubName), + Owner = du, + }; + uow._context.Clubs.Add(du.Club); + uow._context.SaveChanges(); } + else + return false; + + uow._context.Set() + .RemoveRange(uow._context.Set() + .AsQueryable() + .Where(x => x.UserId == du.Id)); + club = du.Club; + uow.SaveChanges(); return true; } @@ -62,7 +62,7 @@ namespace NadekoBot.Modules.Xp.Services ClubInfo club; using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByOwner(from.Id); + club = uow._context.Clubs.GetByOwner(from.Id); var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner); if (club == null || @@ -83,7 +83,7 @@ namespace NadekoBot.Modules.Xp.Services bool newState; using (var uow = _db.GetDbContext()) { - var club = uow.Clubs.GetByOwner(owner.Id); + var club = uow._context.Clubs.GetByOwner(owner.Id); var adminUser = uow.DiscordUsers.GetOrCreate(toAdmin); if (club == null || club.Owner.UserId != owner.Id || @@ -101,10 +101,9 @@ namespace NadekoBot.Modules.Xp.Services public ClubInfo GetClubByMember(IUser user) { - using (var uow = _db.GetDbContext()) - { - return uow.Clubs.GetByMember(user.Id); - } + using var uow = _db.GetDbContext(); + var member = uow._context.Clubs.GetByMember(user.Id); + return member; } public async Task SetClubIcon(ulong ownerUserId, Uri url) @@ -121,7 +120,7 @@ namespace NadekoBot.Modules.Xp.Services using (var uow = _db.GetDbContext()) { - var club = uow.Clubs.GetByOwner(ownerUserId, set => set); + var club = uow._context.Clubs.GetByOwner(ownerUserId); if (club == null) return false; @@ -148,7 +147,7 @@ namespace NadekoBot.Modules.Xp.Services using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByName(name, discrim); + club = uow._context.Clubs.GetByName(name, discrim); if (club == null) return false; else @@ -191,7 +190,7 @@ namespace NadekoBot.Modules.Xp.Services discordUser = null; using (var uow = _db.GetDbContext()) { - var club = uow.Clubs.GetByOwnerOrAdmin(clubOwnerUserId); + var club = uow._context.Clubs.GetByOwnerOrAdmin(clubOwnerUserId); if (club == null) return false; @@ -219,7 +218,7 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - return uow.Clubs.GetByOwnerOrAdmin(ownerUserId); + return uow._context.Clubs.GetByOwnerOrAdmin(ownerUserId); } } @@ -245,7 +244,7 @@ namespace NadekoBot.Modules.Xp.Services using (var uow = _db.GetDbContext()) { - var club = uow.Clubs.GetByOwner(userId); + var club = uow._context.Clubs.GetByOwner(userId); if (club == null) return false; @@ -260,7 +259,7 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - var club = uow.Clubs.GetByOwner(userId); + var club = uow._context.Clubs.GetByOwner(userId); if (club == null) return false; @@ -275,11 +274,11 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByOwner(userId); + club = uow._context.Clubs.GetByOwner(userId); if (club == null) return false; - uow.Clubs.Remove(club); + uow._context.Clubs.Remove(club); uow.SaveChanges(); } return true; @@ -289,7 +288,7 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByOwnerOrAdmin(bannerId); + club = uow._context.Clubs.GetByOwnerOrAdmin(bannerId); if (club == null) return false; @@ -322,7 +321,7 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByOwnerOrAdmin(ownerUserId); + club = uow._context.Clubs.GetByOwnerOrAdmin(ownerUserId); if (club == null) return false; @@ -341,7 +340,7 @@ namespace NadekoBot.Modules.Xp.Services { using (var uow = _db.GetDbContext()) { - club = uow.Clubs.GetByOwnerOrAdmin(kickerId); + club = uow._context.Clubs.GetByOwnerOrAdmin(kickerId); if (club == null) return false; @@ -362,15 +361,13 @@ namespace NadekoBot.Modules.Xp.Services return true; } - public ClubInfo[] GetClubLeaderboardPage(int page) + public List GetClubLeaderboardPage(int page) { if (page < 0) throw new ArgumentOutOfRangeException(nameof(page)); - using (var uow = _db.GetDbContext()) - { - return uow.Clubs.GetClubLeaderboardPage(page); - } + using var uow = _db.GetDbContext(); + return uow._context.Clubs.GetClubLeaderboardPage(page); } } } diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index b980d8645..5b1cd6a36 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -18,7 +18,6 @@ namespace NadekoBot.Core.Services.Database IDiscordUserRepository DiscordUsers { get; } IWarningsRepository Warnings { get; } IXpRepository Xp { get; } - IClubRepository Clubs { get; } IPollsRepository Polls { get; } IPlantedCurrencyRepository PlantedCurrency { get; } diff --git a/src/NadekoBot/Services/Database/Repositories/IClubRepository.cs b/src/NadekoBot/Services/Database/Repositories/IClubRepository.cs deleted file mode 100644 index 5b00dcef9..000000000 --- a/src/NadekoBot/Services/Database/Repositories/IClubRepository.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using NadekoBot.Core.Services.Database.Models; -using System; -using System.Linq; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface IClubRepository : IRepository - { - int GetNextDiscrim(string clubName); - ClubInfo GetByName(string v, int discrim, Func, IQueryable> func = null); - ClubInfo GetByOwner(ulong userId, Func, IQueryable> func = null); - ClubInfo GetByOwnerOrAdmin(ulong userId); - ClubInfo GetByMember(ulong userId, Func, IQueryable> func = null); - ClubInfo[] GetClubLeaderboardPage(int page); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs b/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs new file mode 100644 index 000000000..a9f55ce99 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/ClubExtensions.cs @@ -0,0 +1,48 @@ +using NadekoBot.Core.Services.Database.Models; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; + +namespace NadekoBot.Modules.Xp.Common +{ + public static class ClubExtensions + { + private static IQueryable Include(this DbSet clubs) + => clubs.Include(x => x.Owner) + .Include(x => x.Applicants) + .ThenInclude(x => x.User) + .Include(x => x.Bans) + .ThenInclude(x => x.User) + .Include(x => x.Users) + .AsQueryable(); + public static ClubInfo GetByOwner(this DbSet clubs, ulong userId) + => Include(clubs).FirstOrDefault(c => c.Owner.UserId == userId); + + public static ClubInfo GetByOwnerOrAdmin(this DbSet clubs, ulong userId) + => Include(clubs).FirstOrDefault(c => c.Owner.UserId == userId + || c.Users.Any(u => u.UserId == userId && u.IsClubAdmin)); + + public static ClubInfo GetByMember(this DbSet clubs, ulong userId) + => Include(clubs).FirstOrDefault(c => c.Users.Any(u => u.UserId == userId)); + + public static ClubInfo? GetByName(this DbSet clubs, string name, int discrim) + => Include(clubs).FirstOrDefault(c => c.Name.ToUpper() == name.ToUpper() && c.Discrim == discrim); + + public static int GetNextDiscrim(this DbSet clubs, string name) + => Include(clubs) + .Where(x => x.Name.ToUpper() == name.ToUpper()) + .Select(x => x.Discrim) + .DefaultIfEmpty() + .Max() + 1; + + public static List GetClubLeaderboardPage(this DbSet clubs, int page) + { + return clubs + .AsNoTracking() + .OrderByDescending(x => x.Xp) + .Skip(page * 9) + .Take(9) + .ToList(); + } + } +} \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/ClubRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/ClubRepository.cs deleted file mode 100644 index 28655ead0..000000000 --- a/src/NadekoBot/Services/Database/Repositories/Impl/ClubRepository.cs +++ /dev/null @@ -1,96 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Linq; -using Microsoft.EntityFrameworkCore; -using System; - -namespace NadekoBot.Core.Services.Database.Repositories.Impl -{ - public class ClubRepository : Repository, IClubRepository - { - public ClubRepository(DbContext context) : base(context) - { - } - - public ClubInfo GetByOwner(ulong userId, Func, IQueryable> func = null) - { - if (func == null) - return _set - .Include(x => x.Bans) - .Include(x => x.Applicants) - .Include(x => x.Users) - .Include(x => x.Owner) - .FirstOrDefault(x => x.Owner.UserId == userId); - - return func(_set).FirstOrDefault(x => x.Owner.UserId == userId); - } - - public ClubInfo GetByOwnerOrAdmin(ulong userId) - { - return _set - .Include(x => x.Bans) - .ThenInclude(x => x.User) - .Include(x => x.Applicants) - .ThenInclude(x => x.User) - .Include(x => x.Owner) - .Include(x => x.Users) - .FirstOrDefault(x => x.Owner.UserId == userId) ?? - _context.Set() - .Include(x => x.Club) - .ThenInclude(x => x.Users) - .Include(x => x.Club) - .ThenInclude(x => x.Bans) - .ThenInclude(x => x.User) - .Include(x => x.Club) - .ThenInclude(x => x.Applicants) - .ThenInclude(x => x.User) - .Include(x => x.Club) - .ThenInclude(x => x.Owner) - .FirstOrDefault(x => x.UserId == userId && x.IsClubAdmin) - ?.Club; - } - - public ClubInfo GetByName(string name, int discrim, Func, IQueryable> func = null) - { - if (func == null) - return _set.AsQueryable() - .Where(x => x.Name == name && x.Discrim == discrim) - .Include(x => x.Users) - .Include(x => x.Bans) - .Include(x => x.Applicants) - .FirstOrDefault(); - - return func(_set).FirstOrDefault(x => x.Name == name && x.Discrim == discrim); - } - - public int GetNextDiscrim(string clubName) - { - return _set.AsQueryable() - .Where(x => x.Name.ToUpper() == clubName.ToUpper()) - .Select(x => x.Discrim) - .ToList() - .DefaultIfEmpty() - .Max() + 1; - } - - public ClubInfo GetByMember(ulong userId, Func, IQueryable> func = null) - { - if (func == null) - return _set - .Include(x => x.Users) - .Include(x => x.Bans) - .Include(x => x.Applicants) - .FirstOrDefault(x => x.Users.Any(y => y.UserId == userId)); - - return func(_set).FirstOrDefault(x => x.Users.Any(y => y.UserId == userId)); - } - - public ClubInfo[] GetClubLeaderboardPage(int page) - { - return _set.AsQueryable() - .OrderByDescending(x => x.Xp) - .Skip(page * 9) - .Take(9) - .ToArray(); - } - } -} diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index bfe97a056..db82d33fe 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -39,9 +39,6 @@ namespace NadekoBot.Core.Services.Database private IXpRepository _xp; public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context)); - private IClubRepository _clubs; - public IClubRepository Clubs => _clubs ?? (_clubs = new ClubRepository(_context)); - private IPollsRepository _polls; public IPollsRepository Polls => _polls ?? (_polls = new PollsRepository(_context));