Removed clubs repository and moved functionality to an extension

This commit is contained in:
Kwoth
2021-06-18 07:33:42 +02:00
parent 9f6d4ab62f
commit dbf51a8a1b
6 changed files with 92 additions and 164 deletions

View File

@@ -1,11 +1,13 @@
using NadekoBot.Core.Services; using NadekoBot.Core.Services;
using System; using System;
using System.Collections.Generic;
using NadekoBot.Core.Services.Database.Models; using NadekoBot.Core.Services.Database.Models;
using Discord; using Discord;
using System.Linq; using System.Linq;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using NadekoBot.Modules.Xp.Common;
namespace NadekoBot.Modules.Xp.Services 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 //must be lvl 5 and must not be in a club already
club = null; 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); du.IsClubAdmin = true;
uow._context.SaveChanges(); du.Club = new ClubInfo()
var xp = new LevelStats(du.TotalXp);
if (xp.Level >= 5 && du.Club == null)
{ {
du.IsClubAdmin = true; Name = clubName,
du.Club = new ClubInfo() Discrim = uow._context.Clubs.GetNextDiscrim(clubName),
{ Owner = du,
Name = clubName, };
Discrim = uow.Clubs.GetNextDiscrim(clubName), uow._context.Clubs.Add(du.Club);
Owner = du, uow._context.SaveChanges();
};
uow.Clubs.Add(du.Club);
uow._context.SaveChanges();
}
else
return false;
uow._context.Set<ClubApplicants>()
.RemoveRange(uow._context.Set<ClubApplicants>()
.AsQueryable()
.Where(x => x.UserId == du.Id));
club = du.Club;
uow.SaveChanges();
} }
else
return false;
uow._context.Set<ClubApplicants>()
.RemoveRange(uow._context.Set<ClubApplicants>()
.AsQueryable()
.Where(x => x.UserId == du.Id));
club = du.Club;
uow.SaveChanges();
return true; return true;
} }
@@ -62,7 +62,7 @@ namespace NadekoBot.Modules.Xp.Services
ClubInfo club; ClubInfo club;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByOwner(from.Id); club = uow._context.Clubs.GetByOwner(from.Id);
var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner); var newOwnerUser = uow.DiscordUsers.GetOrCreate(newOwner);
if (club == null || if (club == null ||
@@ -83,7 +83,7 @@ namespace NadekoBot.Modules.Xp.Services
bool newState; bool newState;
using (var uow = _db.GetDbContext()) 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); var adminUser = uow.DiscordUsers.GetOrCreate(toAdmin);
if (club == null || club.Owner.UserId != owner.Id || if (club == null || club.Owner.UserId != owner.Id ||
@@ -101,10 +101,9 @@ namespace NadekoBot.Modules.Xp.Services
public ClubInfo GetClubByMember(IUser user) public ClubInfo GetClubByMember(IUser user)
{ {
using (var uow = _db.GetDbContext()) using var uow = _db.GetDbContext();
{ var member = uow._context.Clubs.GetByMember(user.Id);
return uow.Clubs.GetByMember(user.Id); return member;
}
} }
public async Task<bool> SetClubIcon(ulong ownerUserId, Uri url) public async Task<bool> SetClubIcon(ulong ownerUserId, Uri url)
@@ -121,7 +120,7 @@ namespace NadekoBot.Modules.Xp.Services
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
var club = uow.Clubs.GetByOwner(ownerUserId, set => set); var club = uow._context.Clubs.GetByOwner(ownerUserId);
if (club == null) if (club == null)
return false; return false;
@@ -148,7 +147,7 @@ namespace NadekoBot.Modules.Xp.Services
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByName(name, discrim); club = uow._context.Clubs.GetByName(name, discrim);
if (club == null) if (club == null)
return false; return false;
else else
@@ -191,7 +190,7 @@ namespace NadekoBot.Modules.Xp.Services
discordUser = null; discordUser = null;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
var club = uow.Clubs.GetByOwnerOrAdmin(clubOwnerUserId); var club = uow._context.Clubs.GetByOwnerOrAdmin(clubOwnerUserId);
if (club == null) if (club == null)
return false; return false;
@@ -219,7 +218,7 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) 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()) using (var uow = _db.GetDbContext())
{ {
var club = uow.Clubs.GetByOwner(userId); var club = uow._context.Clubs.GetByOwner(userId);
if (club == null) if (club == null)
return false; return false;
@@ -260,7 +259,7 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
var club = uow.Clubs.GetByOwner(userId); var club = uow._context.Clubs.GetByOwner(userId);
if (club == null) if (club == null)
return false; return false;
@@ -275,11 +274,11 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByOwner(userId); club = uow._context.Clubs.GetByOwner(userId);
if (club == null) if (club == null)
return false; return false;
uow.Clubs.Remove(club); uow._context.Clubs.Remove(club);
uow.SaveChanges(); uow.SaveChanges();
} }
return true; return true;
@@ -289,7 +288,7 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByOwnerOrAdmin(bannerId); club = uow._context.Clubs.GetByOwnerOrAdmin(bannerId);
if (club == null) if (club == null)
return false; return false;
@@ -322,7 +321,7 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByOwnerOrAdmin(ownerUserId); club = uow._context.Clubs.GetByOwnerOrAdmin(ownerUserId);
if (club == null) if (club == null)
return false; return false;
@@ -341,7 +340,7 @@ namespace NadekoBot.Modules.Xp.Services
{ {
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
club = uow.Clubs.GetByOwnerOrAdmin(kickerId); club = uow._context.Clubs.GetByOwnerOrAdmin(kickerId);
if (club == null) if (club == null)
return false; return false;
@@ -362,15 +361,13 @@ namespace NadekoBot.Modules.Xp.Services
return true; return true;
} }
public ClubInfo[] GetClubLeaderboardPage(int page) public List<ClubInfo> GetClubLeaderboardPage(int page)
{ {
if (page < 0) if (page < 0)
throw new ArgumentOutOfRangeException(nameof(page)); throw new ArgumentOutOfRangeException(nameof(page));
using (var uow = _db.GetDbContext()) using var uow = _db.GetDbContext();
{ return uow._context.Clubs.GetClubLeaderboardPage(page);
return uow.Clubs.GetClubLeaderboardPage(page);
}
} }
} }
} }

View File

@@ -18,7 +18,6 @@ namespace NadekoBot.Core.Services.Database
IDiscordUserRepository DiscordUsers { get; } IDiscordUserRepository DiscordUsers { get; }
IWarningsRepository Warnings { get; } IWarningsRepository Warnings { get; }
IXpRepository Xp { get; } IXpRepository Xp { get; }
IClubRepository Clubs { get; }
IPollsRepository Polls { get; } IPollsRepository Polls { get; }
IPlantedCurrencyRepository PlantedCurrency { get; } IPlantedCurrencyRepository PlantedCurrency { get; }

View File

@@ -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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -39,9 +39,6 @@ namespace NadekoBot.Core.Services.Database
private IXpRepository _xp; private IXpRepository _xp;
public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context)); public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context));
private IClubRepository _clubs;
public IClubRepository Clubs => _clubs ?? (_clubs = new ClubRepository(_context));
private IPollsRepository _polls; private IPollsRepository _polls;
public IPollsRepository Polls => _polls ?? (_polls = new PollsRepository(_context)); public IPollsRepository Polls => _polls ?? (_polls = new PollsRepository(_context));