diff --git a/src/NadekoBot/Modules/Administration/Services/SelfAssignedRolesService.cs b/src/NadekoBot/Modules/Administration/Services/SelfAssignedRolesService.cs index 7b6cbd79e..bdf78b6c8 100644 --- a/src/NadekoBot/Modules/Administration/Services/SelfAssignedRolesService.cs +++ b/src/NadekoBot/Modules/Administration/Services/SelfAssignedRolesService.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using NadekoBot.Modules.Xp; +using NadekoBot.Modules.Administration.Common; namespace NadekoBot.Core.Modules.Administration.Services { @@ -40,13 +41,13 @@ namespace NadekoBot.Core.Modules.Administration.Services { using (var uow = _db.GetDbContext()) { - var roles = uow.SelfAssignedRoles.GetFromGuild(guildId); + var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId); if (roles.Any(s => s.RoleId == role.Id && s.GuildId == role.Guild.Id)) { return false; } - uow.SelfAssignedRoles.Add(new SelfAssignedRole + uow._context.SelfAssignableRoles.Add(new SelfAssignedRole { Group = group, RoleId = role.Id, @@ -194,7 +195,7 @@ namespace NadekoBot.Core.Modules.Administration.Services bool success; using (var uow = _db.GetDbContext()) { - success = uow.SelfAssignedRoles.DeleteByGuildAndRoleId(guildId, roleId); + success = uow._context.SelfAssignableRoles.DeleteByGuildAndRoleId(guildId, roleId); uow.SaveChanges(); } return success; @@ -207,7 +208,7 @@ namespace NadekoBot.Core.Modules.Administration.Services var gc = uow.GuildConfigs.ForId(guildId, set => set); var autoDelete = gc.AutoDeleteSelfAssignedRoleMessages; var exclusive = gc.ExclusiveSelfAssignedRoles; - var roles = uow.SelfAssignedRoles.GetFromGuild(guildId); + var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId); return (autoDelete, exclusive, roles); } @@ -217,7 +218,7 @@ namespace NadekoBot.Core.Modules.Administration.Services { using (var uow = _db.GetDbContext()) { - var roles = uow.SelfAssignedRoles.GetFromGuild(guildId); + var roles = uow._context.SelfAssignableRoles.GetFromGuild(guildId); var sar = roles.FirstOrDefault(x => x.RoleId == role.Id); if (sar != null) { @@ -257,10 +258,10 @@ namespace NadekoBot.Core.Modules.Administration.Services var gc = uow.GuildConfigs.ForId(guild.Id, set => set.Include(x => x.SelfAssignableRoleGroupNames)); exclusive = gc.ExclusiveSelfAssignedRoles; groupNames = gc.SelfAssignableRoleGroupNames.ToDictionary(x => x.Number, x => x.Name); - var roleModels = uow.SelfAssignedRoles.GetFromGuild(guild.Id); + var roleModels = uow._context.SelfAssignableRoles.GetFromGuild(guild.Id); roles = roleModels .Select(x => (Model: x, Role: guild.GetRole(x.RoleId))); - uow.SelfAssignedRoles.RemoveRange(roles.Where(x => x.Role == null).Select(x => x.Model).ToArray()); + uow._context.SelfAssignableRoles.RemoveRange(roles.Where(x => x.Role == null).Select(x => x.Model).ToArray()); uow.SaveChanges(); } diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index 8ccc7992d..b9efd584b 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -11,7 +11,6 @@ namespace NadekoBot.Core.Services.Database IQuoteRepository Quotes { get; } IGuildConfigRepository GuildConfigs { get; } IReminderRepository Reminders { get; } - ISelfAssignedRolesRepository SelfAssignedRoles { get; } ICustomReactionRepository CustomReactions { get; } IMusicPlaylistRepository MusicPlaylists { get; } IWaifuRepository Waifus { get; } diff --git a/src/NadekoBot/Services/Database/Repositories/ISelfAssignedRolesRepository.cs b/src/NadekoBot/Services/Database/Repositories/ISelfAssignedRolesRepository.cs deleted file mode 100644 index c33a84c8e..000000000 --- a/src/NadekoBot/Services/Database/Repositories/ISelfAssignedRolesRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Collections.Generic; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface ISelfAssignedRolesRepository : IRepository - { - bool DeleteByGuildAndRoleId(ulong guildId, ulong roleId); - IEnumerable GetFromGuild(ulong guildId); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignableRolesExtensions.cs b/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignableRolesExtensions.cs new file mode 100644 index 000000000..a78f27bc4 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignableRolesExtensions.cs @@ -0,0 +1,26 @@ +using NadekoBot.Core.Services.Database.Models; +using System.Collections.Generic; +using System.Linq; +using Microsoft.EntityFrameworkCore; + +namespace NadekoBot.Modules.Administration.Common +{ + public static class SelfAssignableRolesExtensions + { + public static bool DeleteByGuildAndRoleId(this DbSet roles, ulong guildId, ulong roleId) + { + var role = roles.FirstOrDefault(s => s.GuildId == guildId && s.RoleId == roleId); + + if (role == null) + return false; + + roles.Remove(role); + return true; + } + + public static IEnumerable GetFromGuild(this DbSet roles, ulong guildId) + => roles.AsQueryable() + .Where(s => s.GuildId == guildId) + .ToArray(); + } +} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs deleted file mode 100644 index 7e58f1456..000000000 --- a/src/NadekoBot/Services/Database/Repositories/Impl/SelfAssignedRolesRepository.cs +++ /dev/null @@ -1,30 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using System.Collections.Generic; -using System.Linq; -using Microsoft.EntityFrameworkCore; - -namespace NadekoBot.Core.Services.Database.Repositories.Impl -{ - public class SelfAssignedRolesRepository : Repository, 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 GetFromGuild(ulong guildId) - => _set.AsQueryable() - .Where(s => s.GuildId == guildId) - .ToArray(); - } -} diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 27476ab2e..d2847aa4b 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -18,9 +18,6 @@ namespace NadekoBot.Core.Services.Database private IReminderRepository _reminders; public IReminderRepository Reminders => _reminders ?? (_reminders = new ReminderRepository(_context)); - private ISelfAssignedRolesRepository _selfAssignedRoles; - public ISelfAssignedRolesRepository SelfAssignedRoles => _selfAssignedRoles ?? (_selfAssignedRoles = new SelfAssignedRolesRepository(_context)); - private IMusicPlaylistRepository _musicPlaylists; public IMusicPlaylistRepository MusicPlaylists => _musicPlaylists ?? (_musicPlaylists = new MusicPlaylistRepository(_context));