mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	Removed clubs repository and moved functionality to an extension
This commit is contained in:
		@@ -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; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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<ClubInfo>
 | 
			
		||||
    {
 | 
			
		||||
        int GetNextDiscrim(string clubName);
 | 
			
		||||
        ClubInfo GetByName(string v, int discrim, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
 | 
			
		||||
        ClubInfo GetByOwner(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
 | 
			
		||||
        ClubInfo GetByOwnerOrAdmin(ulong userId);
 | 
			
		||||
        ClubInfo GetByMember(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> func = null);
 | 
			
		||||
        ClubInfo[] GetClubLeaderboardPage(int page);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<ClubInfo> Include(this DbSet<ClubInfo> 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<ClubInfo> clubs, ulong userId)
 | 
			
		||||
            => Include(clubs).FirstOrDefault(c => c.Owner.UserId == userId);
 | 
			
		||||
        
 | 
			
		||||
        public static ClubInfo GetByOwnerOrAdmin(this DbSet<ClubInfo> 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<ClubInfo> clubs, ulong userId)
 | 
			
		||||
            => Include(clubs).FirstOrDefault(c => c.Users.Any(u => u.UserId == userId));
 | 
			
		||||
 | 
			
		||||
        public static ClubInfo? GetByName(this DbSet<ClubInfo> clubs, string name, int discrim)
 | 
			
		||||
            => Include(clubs).FirstOrDefault(c => c.Name.ToUpper() == name.ToUpper() && c.Discrim == discrim);
 | 
			
		||||
 | 
			
		||||
        public static int GetNextDiscrim(this DbSet<ClubInfo> clubs, string name)
 | 
			
		||||
            => Include(clubs)
 | 
			
		||||
                .Where(x => x.Name.ToUpper() == name.ToUpper())
 | 
			
		||||
                .Select(x => x.Discrim)
 | 
			
		||||
                .DefaultIfEmpty()
 | 
			
		||||
                .Max() + 1;
 | 
			
		||||
 | 
			
		||||
        public static List<ClubInfo> GetClubLeaderboardPage(this DbSet<ClubInfo> clubs, int page)
 | 
			
		||||
        {
 | 
			
		||||
            return clubs
 | 
			
		||||
                .AsNoTracking()
 | 
			
		||||
                .OrderByDescending(x => x.Xp)
 | 
			
		||||
                .Skip(page * 9)
 | 
			
		||||
                .Take(9)
 | 
			
		||||
                .ToList();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<ClubInfo>, IClubRepository
 | 
			
		||||
    {
 | 
			
		||||
        public ClubRepository(DbContext context) : base(context)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ClubInfo GetByOwner(ulong userId, Func<DbSet<ClubInfo>, IQueryable<ClubInfo>> 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<DiscordUser>()
 | 
			
		||||
                .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<DbSet<ClubInfo>, IQueryable<ClubInfo>> 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<DbSet<ClubInfo>, IQueryable<ClubInfo>> 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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user