diff --git a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs index 1ecded3ea..f94abffde 100644 --- a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs +++ b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs @@ -17,6 +17,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using NadekoBot.Common.Yml; using NadekoBot.Core.Common; +using NadekoBot.Db; using Serilog; using YamlDotNet.Serialization; @@ -222,7 +223,7 @@ namespace NadekoBot.Modules.CustomReactions.Services using (var uow = _db.GetDbContext()) { - uow.CustomReactions.Add(cr); + uow._context.CustomReactions.Add(cr); await uow.SaveChangesAsync(); } @@ -234,7 +235,7 @@ namespace NadekoBot.Modules.CustomReactions.Services public async Task EditAsync(ulong? guildId, int id, string message) { using var uow = _db.GetDbContext(); - var cr = uow.CustomReactions.GetById(id); + var cr = uow._context.CustomReactions.GetById(id); if (cr == null || cr.GuildId != guildId) return null; @@ -262,14 +263,14 @@ namespace NadekoBot.Modules.CustomReactions.Services public async Task DeleteAsync(ulong? guildId, int id) { using var uow = _db.GetDbContext(); - var toDelete = uow.CustomReactions.GetById(id); + var toDelete = uow._context.CustomReactions.GetById(id); if (toDelete is null) return null; if ((toDelete.IsGlobal() && guildId == null) || (guildId == toDelete.GuildId)) { - uow.CustomReactions.Remove(toDelete); + uow._context.CustomReactions.Remove(toDelete); await uow.SaveChangesAsync(); await DeleteInternalAsync(guildId, id); return toDelete; @@ -468,7 +469,7 @@ namespace NadekoBot.Modules.CustomReactions.Services { CustomReaction cr; using var uow = _db.GetDbContext(); - cr = uow.CustomReactions.GetById(id); + cr = uow._context.CustomReactions.GetById(id); if (cr is null) return; @@ -586,7 +587,7 @@ namespace NadekoBot.Modules.CustomReactions.Services CustomReaction cr; using (var uow = _db.GetDbContext()) { - cr = uow.CustomReactions.GetById(id); + cr = uow._context.CustomReactions.GetById(id); if (cr is null) return; @@ -604,7 +605,7 @@ namespace NadekoBot.Modules.CustomReactions.Services CustomReaction cr; using (var uow = _db.GetDbContext()) { - cr = uow.CustomReactions.GetById(id); + cr = uow._context.CustomReactions.GetById(id); if (cr is null) return (false, false); if (field == CrField.AutoDelete) @@ -627,7 +628,7 @@ namespace NadekoBot.Modules.CustomReactions.Services public CustomReaction GetCustomReaction(ulong? guildId, int id) { using var uow = _db.GetDbContext(); - var cr = uow.CustomReactions.GetById(id); + var cr = uow._context.CustomReactions.GetById(id); if (cr == null || cr.GuildId != guildId) return null; @@ -637,7 +638,7 @@ namespace NadekoBot.Modules.CustomReactions.Services public int DeleteAllCustomReactions(ulong guildId) { using var uow = _db.GetDbContext(); - var count = uow.CustomReactions.ClearFromGuild(guildId); + var count = uow._context.CustomReactions.ClearFromGuild(guildId); uow.SaveChanges(); _newGuildReactions.TryRemove(guildId, out _); @@ -648,7 +649,7 @@ namespace NadekoBot.Modules.CustomReactions.Services public bool ReactionExists(ulong? guildId, string input) { using var uow = _db.GetDbContext(); - var cr = uow.CustomReactions.GetByGuildIdAndInput(guildId, input); + var cr = uow._context.CustomReactions.GetByGuildIdAndInput(guildId, input); return cr != null; } diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index b8ed3fadb..0c4d6a2e2 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -10,7 +10,6 @@ namespace NadekoBot.Core.Services.Database IQuoteRepository Quotes { get; } IGuildConfigRepository GuildConfigs { get; } - ICustomReactionRepository CustomReactions { get; } IWaifuRepository Waifus { get; } IDiscordUserRepository DiscordUsers { get; } IXpRepository Xp { get; } diff --git a/src/NadekoBot/Services/Database/Repositories/ICustomReactionRepository.cs b/src/NadekoBot/Services/Database/Repositories/ICustomReactionRepository.cs deleted file mode 100644 index 4142942d4..000000000 --- a/src/NadekoBot/Services/Database/Repositories/ICustomReactionRepository.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; -using NadekoBot.Core.Services.Database.Models; - -namespace NadekoBot.Core.Services.Database.Repositories -{ - public interface ICustomReactionRepository : IRepository - { - IEnumerable ForId(ulong id); - int ClearFromGuild(ulong id); - CustomReaction GetByGuildIdAndInput(ulong? guildId, string input); - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionRepository.cs deleted file mode 100644 index 5e074b2c3..000000000 --- a/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionRepository.cs +++ /dev/null @@ -1,33 +0,0 @@ -using NadekoBot.Core.Services.Database.Models; -using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; -using System.Linq; - -namespace NadekoBot.Core.Services.Database.Repositories.Impl -{ - public class CustomReactionsRepository : Repository, ICustomReactionRepository - { - public CustomReactionsRepository(DbContext context) : base(context) - { - } - - public int ClearFromGuild(ulong id) - { - return _context.Database.ExecuteSqlInterpolated($"DELETE FROM CustomReactions WHERE GuildId={id};"); - } - - public IEnumerable ForId(ulong id) - { - return _set - .AsNoTracking() - .AsQueryable() - .Where(x => x.GuildId == id) - .ToArray(); - } - - public CustomReaction GetByGuildIdAndInput(ulong? guildId, string input) - { - return _set.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input); - } - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionsExtensions.cs b/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionsExtensions.cs new file mode 100644 index 000000000..5c060df76 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/CustomReactionsExtensions.cs @@ -0,0 +1,30 @@ +using NadekoBot.Core.Services.Database.Models; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using LinqToDB; + +namespace NadekoBot.Modules.CustomReactions +{ + public static class CustomReactionsExtensions + { + public static int ClearFromGuild(this DbSet crs, ulong guildId) + { + return crs.Delete(x => x.GuildId == guildId); + } + + public static IEnumerable ForId(this DbSet crs, ulong id) + { + return crs + .AsNoTracking() + .AsQueryable() + .Where(x => x.GuildId == id) + .ToArray(); + } + + public static CustomReaction GetByGuildIdAndInput(this DbSet crs, ulong? guildId, string input) + { + return crs.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input); + } + } +} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/DbExtensions.cs b/src/NadekoBot/Services/Database/Repositories/Impl/DbExtensions.cs new file mode 100644 index 000000000..5609ee3ef --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/DbExtensions.cs @@ -0,0 +1,12 @@ +using System.Linq; +using Microsoft.EntityFrameworkCore; +using NadekoBot.Core.Services.Database.Models; + +namespace NadekoBot.Db +{ + public static class DbExtensions + { + public static T GetById(this DbSet set, int id) where T: DbEntity + => set.FirstOrDefault(x => x.Id == id); + } +} \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 156b35f93..3d61bc93d 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -15,9 +15,6 @@ namespace NadekoBot.Core.Services.Database private IGuildConfigRepository _guildConfigs; public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context)); - private ICustomReactionRepository _customReactions; - public ICustomReactionRepository CustomReactions => _customReactions ?? (_customReactions = new CustomReactionsRepository(_context)); - private IWaifuRepository _waifus; public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context));