Initial split of the modules
This commit is contained in:
		| @@ -0,0 +1,40 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class BotConfigRepository : Repository<BotConfig>, IBotConfigRepository | ||||
|     { | ||||
|         public BotConfigRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public BotConfig GetOrCreate(Func<DbSet<BotConfig>, IQueryable<BotConfig>> includes = null) | ||||
|         { | ||||
|             BotConfig config; | ||||
|  | ||||
|             if (includes == null) | ||||
|                 config = _set.Include(bc => bc.RotatingStatusMessages) | ||||
|                              .Include(bc => bc.RaceAnimals) | ||||
|                              .Include(bc => bc.Blacklist) | ||||
|                              .Include(bc => bc.EightBallResponses) | ||||
|                              .Include(bc => bc.StartupCommands) | ||||
|                              .Include(bc => bc.BlockedCommands) | ||||
|                              .Include(bc => bc.BlockedModules) | ||||
|                              .Include(bc => bc.Blacklist) | ||||
|                              //.Include(bc => bc.CommandCosts) | ||||
|                              .FirstOrDefault(); | ||||
|             else | ||||
|                 config = includes(_set).FirstOrDefault(); | ||||
|  | ||||
|             if (config == null) | ||||
|             { | ||||
|                 _set.Add(config = new BotConfig()); | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|             return config; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,24 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class ClashOfClansRepository : Repository<ClashWar>, IClashOfClansRepository | ||||
|     { | ||||
|         public ClashOfClansRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<ClashWar> GetAllWars(List<long> guilds) | ||||
|         { | ||||
|             var toReturn =  _set | ||||
|                 .Where(cw => guilds.Contains((long)cw.GuildId)) | ||||
|                 .Include(cw => cw.Bases) | ||||
|                         .ToList(); | ||||
|             toReturn.ForEach(cw => cw.Bases = cw.Bases.Where(w => w.SequenceNumber != null).OrderBy(w => w.SequenceNumber).ToList()); | ||||
|             return toReturn; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,95 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
|  | ||||
| namespace NadekoBot.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 | ||||
|                     .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 | ||||
|                 .Where(x => x.Name.ToLowerInvariant() == clubName.ToLowerInvariant()) | ||||
|                 .Select(x => x.Discrim) | ||||
|                 .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 | ||||
|                 .OrderByDescending(x => x.Xp) | ||||
|                 .Skip(page * 9) | ||||
|                 .Take(9) | ||||
|                 .ToArray(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,57 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class CurrencyRepository : Repository<Currency>, ICurrencyRepository | ||||
|     { | ||||
|         public CurrencyRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public Currency GetOrCreate(ulong userId) | ||||
|         { | ||||
|             var cur = _set.FirstOrDefault(c => c.UserId == userId); | ||||
|  | ||||
|             if (cur == null) | ||||
|             { | ||||
|                 _set.Add(cur = new Currency() | ||||
|                 { | ||||
|                     UserId = userId, | ||||
|                     Amount = 0 | ||||
|                 }); | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|             return cur; | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<Currency> GetTopRichest(int count, int skip = 0) => | ||||
|             _set.OrderByDescending(c => c.Amount).Skip(skip).Take(count).ToList(); | ||||
|  | ||||
|         public long GetUserCurrency(ulong userId) =>  | ||||
|             GetOrCreate(userId).Amount; | ||||
|  | ||||
|         public bool TryUpdateState(ulong userId, long change) | ||||
|         { | ||||
|             var cur = GetOrCreate(userId); | ||||
|  | ||||
|             if (change == 0) | ||||
|                 return true; | ||||
|  | ||||
|             if (change > 0) | ||||
|             { | ||||
|                 cur.Amount += change; | ||||
|                 return true; | ||||
|             } | ||||
|             //change is negative | ||||
|             if (cur.Amount + change >= 0) | ||||
|             { | ||||
|                 cur.Amount += change; | ||||
|                 return true; | ||||
|             } | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class CurrencyTransactionsRepository : Repository<CurrencyTransaction>, ICurrencyTransactionsRepository | ||||
|     { | ||||
|         public CurrencyTransactionsRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class CustomReactionsRepository : Repository<CustomReaction>, ICustomReactionRepository | ||||
|     { | ||||
|         public CustomReactionsRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,63 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Discord; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class DiscordUserRepository : Repository<DiscordUser>, IDiscordUserRepository | ||||
|     { | ||||
|         public DiscordUserRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public DiscordUser GetOrCreate(IUser original) | ||||
|         { | ||||
|             DiscordUser toReturn; | ||||
|  | ||||
|             toReturn = _set.Include(x => x.Club) | ||||
|                 .FirstOrDefault(u => u.UserId == original.Id); | ||||
|  | ||||
|             if (toReturn != null) | ||||
|             { | ||||
|                 toReturn.AvatarId = original.AvatarId; | ||||
|                 toReturn.Username = original.Username; | ||||
|                 toReturn.Discriminator = original.Discriminator; | ||||
|             } | ||||
|  | ||||
|             if (toReturn == null) | ||||
|                 _set.Add(toReturn = new DiscordUser() | ||||
|                 { | ||||
|                     AvatarId = original.AvatarId, | ||||
|                     Discriminator = original.Discriminator, | ||||
|                     UserId = original.Id, | ||||
|                     Username = original.Username, | ||||
|                     Club = null, | ||||
|                 }); | ||||
|  | ||||
|             return toReturn; | ||||
|         } | ||||
|  | ||||
|         public int GetUserGlobalRanking(ulong id) | ||||
|         { | ||||
|             if (!_set.Where(y => y.UserId == id).Any()) | ||||
|             { | ||||
|                 return _set.Count() + 1; | ||||
|             } | ||||
|             return _set.Count(x => x.TotalXp >=  | ||||
|                 _set.Where(y => y.UserId == id) | ||||
|                     .DefaultIfEmpty() | ||||
|                     .Sum(y => y.TotalXp)); | ||||
|         } | ||||
|  | ||||
|         public DiscordUser[] GetUsersXpLeaderboardFor(int page) | ||||
|         { | ||||
|             return _set | ||||
|                 .OrderByDescending(x => x.TotalXp) | ||||
|                 .Skip(page * 9) | ||||
|                 .Take(9) | ||||
|                 .AsEnumerable() | ||||
|                 .ToArray(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,40 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using NadekoBot.Services.Database.Models; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class DonatorsRepository : Repository<Donator>, IDonatorsRepository | ||||
|     { | ||||
|         public DonatorsRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public Donator AddOrUpdateDonator(ulong userId, string name, int amount) | ||||
|         { | ||||
|             var donator = _set.Where(d => d.UserId == userId).FirstOrDefault(); | ||||
|  | ||||
|             if (donator == null) | ||||
|             { | ||||
|                 _set.Add(donator = new Donator | ||||
|                 { | ||||
|                     Amount = amount, | ||||
|                     UserId = userId, | ||||
|                     Name = name | ||||
|                 }); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 donator.Amount += amount; | ||||
|                 donator.Name = name; | ||||
|                 _set.Update(donator); | ||||
|             } | ||||
|  | ||||
|             return donator; | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<Donator> GetDonatorsOrdered() =>  | ||||
|             _set.OrderByDescending(d => d.Amount).ToList(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,211 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class GuildConfigRepository : Repository<GuildConfig>, IGuildConfigRepository | ||||
|     { | ||||
|         public GuildConfigRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         private List<WarningPunishment> DefaultWarnPunishments => | ||||
|             new List<WarningPunishment>() { | ||||
|                 new WarningPunishment() { | ||||
|                     Count = 3, | ||||
|                     Punishment = PunishmentAction.Kick | ||||
|                 }, | ||||
|                 new WarningPunishment() { | ||||
|                     Count = 5, | ||||
|                     Punishment = PunishmentAction.Ban | ||||
|                 } | ||||
|             }; | ||||
|  | ||||
|         public IEnumerable<GuildConfig> GetAllGuildConfigs(List<long> availableGuilds) => | ||||
|             _set | ||||
|                 .Where(gc => availableGuilds.Contains((long)gc.GuildId)) | ||||
|                 .Include(gc => gc.LogSetting) | ||||
|                     .ThenInclude(ls => ls.IgnoredChannels) | ||||
|                 .Include(gc => gc.MutedUsers) | ||||
|                 .Include(gc => gc.CommandAliases) | ||||
|                 .Include(gc => gc.UnmuteTimers) | ||||
|                 .Include(gc => gc.VcRoleInfos) | ||||
|                 .Include(gc => gc.GenerateCurrencyChannelIds) | ||||
|                 .Include(gc => gc.FilterInvitesChannelIds) | ||||
|                 .Include(gc => gc.FilterWordsChannelIds) | ||||
|                 .Include(gc => gc.FilteredWords) | ||||
|                 .Include(gc => gc.CommandCooldowns) | ||||
|                 .Include(gc => gc.GuildRepeaters) | ||||
|                 .Include(gc => gc.AntiRaidSetting) | ||||
|                 .Include(gc => gc.SlowmodeIgnoredRoles) | ||||
|                 .Include(gc => gc.SlowmodeIgnoredUsers) | ||||
|                 .Include(gc => gc.AntiSpamSetting) | ||||
|                     .ThenInclude(x => x.IgnoredChannels) | ||||
|                 .Include(gc => gc.FeedSubs) | ||||
|                     .ThenInclude(x => x.GuildConfig) | ||||
|                 .Include(gc => gc.FollowedStreams) | ||||
|                 .Include(gc => gc.StreamRole) | ||||
|                 .Include(gc => gc.NsfwBlacklistedTags) | ||||
|                 .Include(gc => gc.XpSettings) | ||||
|                     .ThenInclude(x => x.ExclusionList) | ||||
|                 .ToList(); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets and creates if it doesn't exist a config for a guild. | ||||
|         /// </summary> | ||||
|         /// <param name="guildId">For which guild</param> | ||||
|         /// <param name="includes">Use to manipulate the set however you want</param> | ||||
|         /// <returns>Config for the guild</returns> | ||||
|         public GuildConfig For(ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes = null) | ||||
|         { | ||||
|             GuildConfig config; | ||||
|  | ||||
|             if (includes == null) | ||||
|             { | ||||
|                 config = _set | ||||
|                     .Include(gc => gc.FollowedStreams) | ||||
|                     .Include(gc => gc.LogSetting) | ||||
|                         .ThenInclude(ls => ls.IgnoredChannels) | ||||
|                     .Include(gc => gc.FilterInvitesChannelIds) | ||||
|                     .Include(gc => gc.FilterWordsChannelIds) | ||||
|                     .Include(gc => gc.FilteredWords) | ||||
|                     .Include(gc => gc.GenerateCurrencyChannelIds) | ||||
|                     .Include(gc => gc.CommandCooldowns) | ||||
|                     .FirstOrDefault(c => c.GuildId == guildId); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 var set = includes(_set); | ||||
|                 config = set.FirstOrDefault(c => c.GuildId == guildId); | ||||
|             } | ||||
|  | ||||
|             if (config == null) | ||||
|             { | ||||
|                 _set.Add((config = new GuildConfig | ||||
|                 { | ||||
|                     GuildId = guildId, | ||||
|                     Permissions = Permissionv2.GetDefaultPermlist, | ||||
|                     WarningsInitialized = true, | ||||
|                     WarnPunishments = DefaultWarnPunishments, | ||||
|                 })); | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|  | ||||
|             if (!config.WarningsInitialized) | ||||
|             { | ||||
|                 config.WarningsInitialized = true; | ||||
|                 config.WarnPunishments = DefaultWarnPunishments; | ||||
|             } | ||||
|  | ||||
|             return config; | ||||
|         } | ||||
|  | ||||
|         public GuildConfig LogSettingsFor(ulong guildId) | ||||
|         { | ||||
|             var config = _set.Include(gc => gc.LogSetting) | ||||
|                             .ThenInclude(gc => gc.IgnoredChannels) | ||||
|                .FirstOrDefault(x => x.GuildId == guildId); | ||||
|  | ||||
|             if (config == null) | ||||
|             { | ||||
|                 _set.Add((config = new GuildConfig | ||||
|                 { | ||||
|                     GuildId = guildId, | ||||
|                     Permissions = Permissionv2.GetDefaultPermlist, | ||||
|                     WarningsInitialized = true, | ||||
|                     WarnPunishments = DefaultWarnPunishments, | ||||
|                 })); | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|  | ||||
|             if (!config.WarningsInitialized) | ||||
|             { | ||||
|                 config.WarningsInitialized = true; | ||||
|                 config.WarnPunishments = DefaultWarnPunishments; | ||||
|             } | ||||
|             return config; | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<GuildConfig> OldPermissionsForAll() | ||||
|         { | ||||
|             var query = _set | ||||
|                 .Where(gc => gc.RootPermission != null) | ||||
|                 .Include(gc => gc.RootPermission); | ||||
|  | ||||
|             for (int i = 0; i < 60; i++) | ||||
|             { | ||||
|                 query = query.ThenInclude(gc => gc.Next); | ||||
|             } | ||||
|  | ||||
|             return query.ToList(); | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<GuildConfig> Permissionsv2ForAll(List<long> include) | ||||
|         { | ||||
|             var query = _set | ||||
|                 .Where(x => include.Contains((long)x.GuildId)) | ||||
|                 .Include(gc => gc.Permissions); | ||||
|  | ||||
|             return query.ToList(); | ||||
|         } | ||||
|  | ||||
|         public GuildConfig GcWithPermissionsv2For(ulong guildId) | ||||
|         { | ||||
|             var config = _set | ||||
|                 .Where(gc => gc.GuildId == guildId) | ||||
|                 .Include(gc => gc.Permissions) | ||||
|                 .FirstOrDefault(); | ||||
|  | ||||
|             if (config == null) // if there is no guildconfig, create new one | ||||
|             { | ||||
|                 _set.Add((config = new GuildConfig | ||||
|                 { | ||||
|                     GuildId = guildId, | ||||
|                     Permissions = Permissionv2.GetDefaultPermlist | ||||
|                 })); | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|             else if (config.Permissions == null || !config.Permissions.Any()) // if no perms, add default ones | ||||
|             { | ||||
|                 config.Permissions = Permissionv2.GetDefaultPermlist; | ||||
|                 _context.SaveChanges(); | ||||
|             } | ||||
|  | ||||
|             return config; | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<FollowedStream> GetAllFollowedStreams(List<long> included) => | ||||
|             _set | ||||
|                 .Where(gc => included.Contains((long)gc.GuildId)) | ||||
|                 .Include(gc => gc.FollowedStreams) | ||||
|                 .SelectMany(gc => gc.FollowedStreams) | ||||
|                 .ToList(); | ||||
|  | ||||
|         public void SetCleverbotEnabled(ulong id, bool cleverbotEnabled) | ||||
|         { | ||||
|             var conf = _set.FirstOrDefault(gc => gc.GuildId == id); | ||||
|  | ||||
|             if (conf == null) | ||||
|                 return; | ||||
|  | ||||
|             conf.CleverbotEnabled = cleverbotEnabled; | ||||
|         } | ||||
|  | ||||
|         public XpSettings XpSettingsFor(ulong guildId) | ||||
|         { | ||||
|             var gc = For(guildId, | ||||
|                 set => set.Include(x => x.XpSettings) | ||||
|                           .ThenInclude(x => x.RoleRewards) | ||||
|                           .Include(x => x.XpSettings) | ||||
|                           .ThenInclude(x => x.ExclusionList)); | ||||
|  | ||||
|             if (gc.XpSettings == null) | ||||
|                 gc.XpSettings = new XpSettings(); | ||||
|  | ||||
|             return gc.XpSettings; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,30 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class MusicPlaylistRepository : Repository<MusicPlaylist>, IMusicPlaylistRepository | ||||
|     { | ||||
|         public MusicPlaylistRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public List<MusicPlaylist> GetPlaylistsOnPage(int num) | ||||
|         { | ||||
|             if (num < 1) | ||||
|                 throw new IndexOutOfRangeException(); | ||||
|  | ||||
|             return _set.Skip((num - 1) * 20) | ||||
|                 .Take(20) | ||||
|                 .Include(pl => pl.Songs) | ||||
|                 .ToList(); | ||||
|         } | ||||
|  | ||||
|         public MusicPlaylist GetWithSongs(int id) =>  | ||||
|             _set.Include(mpl => mpl.Songs) | ||||
|                 .FirstOrDefault(mpl => mpl.Id == id); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,20 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class PokeGameRepository : Repository<UserPokeTypes>, IPokeGameRepository | ||||
|     { | ||||
|         public PokeGameRepository(DbContext context) : base(context) | ||||
|         { | ||||
|  | ||||
|         } | ||||
|  | ||||
|         //List<UserPokeTypes> GetAllPokeTypes() | ||||
|         //{ | ||||
|         //    var toReturn = _set.Include(pt => pt.UserId).ToList(); | ||||
|         //    toReturn.ForEach(pt => pt.).ToList(); | ||||
|         //    return toReturn; | ||||
|         //} | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,39 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using NadekoBot.Extensions; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using NadekoBot.Common; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class QuoteRepository : Repository<Quote>, IQuoteRepository | ||||
|     { | ||||
|         public QuoteRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<Quote> GetAllQuotesByKeyword(ulong guildId, string keyword) =>  | ||||
|             _set.Where(q => q.GuildId == guildId && q.Keyword == keyword); | ||||
|  | ||||
|         public IEnumerable<Quote> GetGroup(ulong guildId, int skip, int take) =>  | ||||
|             _set.Where(q=>q.GuildId == guildId).OrderBy(q => q.Keyword).Skip(skip).Take(take).ToList(); | ||||
|  | ||||
|         public Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword) | ||||
|         { | ||||
|             var rng = new NadekoRandom(); | ||||
|             return _set.Where(q => q.GuildId == guildId && q.Keyword == keyword).OrderBy(q => rng.Next()).FirstOrDefaultAsync(); | ||||
|         } | ||||
|         public Task<Quote> SearchQuoteKeywordTextAsync(ulong guildId, string keyword, string text) | ||||
|         {      			 | ||||
|             var rngk = new NadekoRandom(); | ||||
|             return _set.Where(q => q.Text.ContainsNoCase(text, StringComparison.OrdinalIgnoreCase) && q.GuildId == guildId && q.Keyword == keyword).OrderBy(q => rngk.Next()).FirstOrDefaultAsync(); | ||||
|     } | ||||
|  | ||||
|         public void RemoveAllByKeyword(ulong guildId, string keyword) => | ||||
|             _set.RemoveRange(_set.Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword)); | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class ReminderRepository : Repository<Reminder>, IReminderRepository | ||||
|     { | ||||
|         public ReminderRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<Reminder> GetIncludedReminders(IEnumerable<long> guildIds) | ||||
|         { | ||||
|             return _set.Where(x => guildIds.Contains((long)x.ServerId)).ToList(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,46 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class Repository<T> : IRepository<T> where T : DbEntity | ||||
|     { | ||||
|         protected DbContext _context; | ||||
|         protected DbSet<T> _set; | ||||
|  | ||||
|         public Repository(DbContext context) | ||||
|         { | ||||
|             _context = context; | ||||
|             _set = context.Set<T>(); | ||||
|         } | ||||
|  | ||||
|         public void Add(T obj) => | ||||
|             _set.Add(obj); | ||||
|  | ||||
|         public void AddRange(params T[] objs) => | ||||
|             _set.AddRange(objs); | ||||
|  | ||||
|         public T Get(int id) => | ||||
|             _set.FirstOrDefault(e => e.Id == id); | ||||
|  | ||||
|         public IEnumerable<T> GetAll() => | ||||
|             _set.ToList(); | ||||
|  | ||||
|         public void Remove(int id) => | ||||
|             _set.Remove(this.Get(id)); | ||||
|  | ||||
|         public void Remove(T obj) => | ||||
|             _set.Remove(obj); | ||||
|  | ||||
|         public void RemoveRange(params T[] objs) => | ||||
|             _set.RemoveRange(objs); | ||||
|  | ||||
|         public void Update(T obj) => | ||||
|             _set.Update(obj); | ||||
|  | ||||
|         public void UpdateRange(params T[] objs) => | ||||
|             _set.UpdateRange(objs); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,28 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class SelfAssignedRolesRepository : Repository<SelfAssignedRole>, ISelfAssignedRolesRepository | ||||
|     { | ||||
|         public SelfAssignedRolesRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId) | ||||
|         { | ||||
|             var role = _set.FirstOrDefault(s => s.GuildId == guildId && s.RoleId == roleId); | ||||
|  | ||||
|             if (role == null) | ||||
|                 return false; | ||||
|  | ||||
|             _set.Remove(role); | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         public IEnumerable<SelfAssignedRole> GetFromGuild(ulong guildId) =>  | ||||
|             _set.Where(s => s.GuildId == guildId).ToList(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,26 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class UnitConverterRepository : Repository<ConvertUnit>, IUnitConverterRepository | ||||
|     { | ||||
|         public UnitConverterRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public void AddOrUpdate(Func<ConvertUnit, bool> check, ConvertUnit toAdd, Func<ConvertUnit, ConvertUnit> toUpdate) | ||||
|         { | ||||
|            var existing = _set.FirstOrDefault(check); | ||||
|             if (existing != null) | ||||
|             { | ||||
|                 existing = toUpdate.Invoke(existing); | ||||
|             } | ||||
|             else _set.Add(toAdd); | ||||
|         } | ||||
|  | ||||
|         public bool Empty() => !_set.Any(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,50 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class WaifuRepository : Repository<WaifuInfo>, IWaifuRepository | ||||
|     { | ||||
|         public WaifuRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public WaifuInfo ByWaifuUserId(ulong userId) | ||||
|         { | ||||
|             return _set.Include(wi => wi.Waifu) | ||||
|                         .Include(wi => wi.Affinity) | ||||
|                         .Include(wi => wi.Claimer) | ||||
|                         .Include(wi => wi.Items) | ||||
|                         .FirstOrDefault(wi => wi.Waifu.UserId == userId); | ||||
|         } | ||||
|  | ||||
|         public IList<WaifuInfo> ByClaimerUserId(ulong userId) | ||||
|         { | ||||
|             return _set.Include(wi => wi.Waifu) | ||||
|                         .Include(wi => wi.Affinity) | ||||
|                         .Include(wi => wi.Claimer) | ||||
|                         .Include(wi => wi.Items) | ||||
|                         .Where(wi => wi.Claimer != null && wi.Claimer.UserId == userId) | ||||
|                         .ToList(); | ||||
|         } | ||||
|  | ||||
|         public IList<WaifuInfo> GetTop(int count, int skip = 0) | ||||
|         { | ||||
|             if (count < 0) | ||||
|                 throw new ArgumentOutOfRangeException(nameof(count)); | ||||
|             if (count == 0) | ||||
|                 return new List<WaifuInfo>(); | ||||
|  | ||||
|             return _set.Include(wi => wi.Waifu) | ||||
|                         .Include(wi => wi.Affinity) | ||||
|                         .Include(wi => wi.Claimer) | ||||
|                     .OrderByDescending(wi => wi.Price) | ||||
|                     .Skip(skip) | ||||
|                     .Take(count) | ||||
|                     .ToList(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,41 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class WarningsRepository : Repository<Warning>, IWarningsRepository | ||||
|     { | ||||
|         public WarningsRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public Warning[] For(ulong guildId, ulong userId) | ||||
|         { | ||||
|             var query = _set.Where(x => x.GuildId == guildId && x.UserId == userId) | ||||
|                 .OrderByDescending(x => x.DateAdded); | ||||
|  | ||||
|             return query.ToArray(); | ||||
|         } | ||||
|  | ||||
|         public async Task ForgiveAll(ulong guildId, ulong userId, string mod) | ||||
|         { | ||||
|             await _set.Where(x => x.GuildId == guildId && x.UserId == userId) | ||||
|                 .ForEachAsync(x => | ||||
|                 { | ||||
|                     if (x.Forgiven != true) | ||||
|                     { | ||||
|                         x.Forgiven = true; | ||||
|                         x.ForgivenBy = mod; | ||||
|                     } | ||||
|                 }) | ||||
|                 .ConfigureAwait(false); | ||||
|         } | ||||
|  | ||||
|         public Warning[] GetForGuild(ulong id) | ||||
|         { | ||||
|             return _set.Where(x => x.GuildId == id).ToArray(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,60 @@ | ||||
| using NadekoBot.Services.Database.Models; | ||||
| using System.Linq; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace NadekoBot.Services.Database.Repositories.Impl | ||||
| { | ||||
|     public class XpRepository : Repository<UserXpStats>, IXpRepository | ||||
|     { | ||||
|         public XpRepository(DbContext context) : base(context) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public UserXpStats GetOrCreateUser(ulong guildId, ulong userId) | ||||
|         { | ||||
|             var usr = _set.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId); | ||||
|  | ||||
|             if (usr == null) | ||||
|             { | ||||
|                 _context.Add(usr = new UserXpStats() | ||||
|                 { | ||||
|                     Xp = 0, | ||||
|                     UserId = userId, | ||||
|                     NotifyOnLevelUp = XpNotificationType.None, | ||||
|                     GuildId = guildId, | ||||
|                 }); | ||||
|             } | ||||
|  | ||||
|             return usr; | ||||
|         } | ||||
|  | ||||
|         public UserXpStats[] GetUsersFor(ulong guildId, int page) | ||||
|         { | ||||
|             return _set.Where(x => x.GuildId == guildId) | ||||
|                 .OrderByDescending(x => x.Xp + x.AwardedXp) | ||||
|                 .Skip(page * 9) | ||||
|                 .Take(9) | ||||
|                 .ToArray(); | ||||
|         } | ||||
|  | ||||
|         public int GetUserGuildRanking(ulong userId, ulong guildId) | ||||
|         { | ||||
|             if (!_set.Where(x => x.GuildId == guildId && x.UserId == userId).Any()) | ||||
|             { | ||||
|                 var cnt = _set.Count(x => x.GuildId == guildId); | ||||
|                 if (cnt == 0) | ||||
|                     return 1; | ||||
|                 else | ||||
|                     return cnt; | ||||
|             } | ||||
|  | ||||
|             return _set | ||||
|                 .Where(x => x.GuildId == guildId) | ||||
|                 .Count(x => x.Xp > (_set | ||||
|                     .Where(y => y.UserId == userId && y.GuildId == guildId) | ||||
|                     .Select(y => y.Xp) | ||||
|                     .DefaultIfEmpty() | ||||
|                     .Sum())) + 1; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user