mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Removed cr repostiory, started a general purpose db extensions file
This commit is contained in:
@@ -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<CustomReaction> 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<CustomReaction> 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;
|
||||
}
|
||||
|
||||
|
@@ -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; }
|
||||
|
@@ -1,12 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using NadekoBot.Core.Services.Database.Models;
|
||||
|
||||
namespace NadekoBot.Core.Services.Database.Repositories
|
||||
{
|
||||
public interface ICustomReactionRepository : IRepository<CustomReaction>
|
||||
{
|
||||
IEnumerable<CustomReaction> ForId(ulong id);
|
||||
int ClearFromGuild(ulong id);
|
||||
CustomReaction GetByGuildIdAndInput(ulong? guildId, string input);
|
||||
}
|
||||
}
|
@@ -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<CustomReaction>, ICustomReactionRepository
|
||||
{
|
||||
public CustomReactionsRepository(DbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public int ClearFromGuild(ulong id)
|
||||
{
|
||||
return _context.Database.ExecuteSqlInterpolated($"DELETE FROM CustomReactions WHERE GuildId={id};");
|
||||
}
|
||||
|
||||
public IEnumerable<CustomReaction> 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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -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<CustomReaction> crs, ulong guildId)
|
||||
{
|
||||
return crs.Delete(x => x.GuildId == guildId);
|
||||
}
|
||||
|
||||
public static IEnumerable<CustomReaction> ForId(this DbSet<CustomReaction> crs, ulong id)
|
||||
{
|
||||
return crs
|
||||
.AsNoTracking()
|
||||
.AsQueryable()
|
||||
.Where(x => x.GuildId == id)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public static CustomReaction GetByGuildIdAndInput(this DbSet<CustomReaction> crs, ulong? guildId, string input)
|
||||
{
|
||||
return crs.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input);
|
||||
}
|
||||
}
|
||||
}
|
@@ -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<T>(this DbSet<T> set, int id) where T: DbEntity
|
||||
=> set.FirstOrDefault(x => x.Id == id);
|
||||
}
|
||||
}
|
@@ -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));
|
||||
|
||||
|
Reference in New Issue
Block a user