Removed cr repostiory, started a general purpose db extensions file

This commit is contained in:
Kwoth
2021-06-18 22:32:41 +02:00
parent a6c7f1a629
commit 56f28568d1
7 changed files with 53 additions and 59 deletions

View File

@@ -17,6 +17,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.Yml; using NadekoBot.Common.Yml;
using NadekoBot.Core.Common; using NadekoBot.Core.Common;
using NadekoBot.Db;
using Serilog; using Serilog;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
@@ -222,7 +223,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
uow.CustomReactions.Add(cr); uow._context.CustomReactions.Add(cr);
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
} }
@@ -234,7 +235,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
public async Task<CustomReaction> EditAsync(ulong? guildId, int id, string message) public async Task<CustomReaction> EditAsync(ulong? guildId, int id, string message)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var cr = uow.CustomReactions.GetById(id); var cr = uow._context.CustomReactions.GetById(id);
if (cr == null || cr.GuildId != guildId) if (cr == null || cr.GuildId != guildId)
return null; return null;
@@ -262,14 +263,14 @@ namespace NadekoBot.Modules.CustomReactions.Services
public async Task<CustomReaction> DeleteAsync(ulong? guildId, int id) public async Task<CustomReaction> DeleteAsync(ulong? guildId, int id)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var toDelete = uow.CustomReactions.GetById(id); var toDelete = uow._context.CustomReactions.GetById(id);
if (toDelete is null) if (toDelete is null)
return null; return null;
if ((toDelete.IsGlobal() && guildId == null) || (guildId == toDelete.GuildId)) if ((toDelete.IsGlobal() && guildId == null) || (guildId == toDelete.GuildId))
{ {
uow.CustomReactions.Remove(toDelete); uow._context.CustomReactions.Remove(toDelete);
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
await DeleteInternalAsync(guildId, id); await DeleteInternalAsync(guildId, id);
return toDelete; return toDelete;
@@ -468,7 +469,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
{ {
CustomReaction cr; CustomReaction cr;
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
cr = uow.CustomReactions.GetById(id); cr = uow._context.CustomReactions.GetById(id);
if (cr is null) if (cr is null)
return; return;
@@ -586,7 +587,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
CustomReaction cr; CustomReaction cr;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
cr = uow.CustomReactions.GetById(id); cr = uow._context.CustomReactions.GetById(id);
if (cr is null) if (cr is null)
return; return;
@@ -604,7 +605,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
CustomReaction cr; CustomReaction cr;
using (var uow = _db.GetDbContext()) using (var uow = _db.GetDbContext())
{ {
cr = uow.CustomReactions.GetById(id); cr = uow._context.CustomReactions.GetById(id);
if (cr is null) if (cr is null)
return (false, false); return (false, false);
if (field == CrField.AutoDelete) if (field == CrField.AutoDelete)
@@ -627,7 +628,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
public CustomReaction GetCustomReaction(ulong? guildId, int id) public CustomReaction GetCustomReaction(ulong? guildId, int id)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var cr = uow.CustomReactions.GetById(id); var cr = uow._context.CustomReactions.GetById(id);
if (cr == null || cr.GuildId != guildId) if (cr == null || cr.GuildId != guildId)
return null; return null;
@@ -637,7 +638,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
public int DeleteAllCustomReactions(ulong guildId) public int DeleteAllCustomReactions(ulong guildId)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var count = uow.CustomReactions.ClearFromGuild(guildId); var count = uow._context.CustomReactions.ClearFromGuild(guildId);
uow.SaveChanges(); uow.SaveChanges();
_newGuildReactions.TryRemove(guildId, out _); _newGuildReactions.TryRemove(guildId, out _);
@@ -648,7 +649,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
public bool ReactionExists(ulong? guildId, string input) public bool ReactionExists(ulong? guildId, string input)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
var cr = uow.CustomReactions.GetByGuildIdAndInput(guildId, input); var cr = uow._context.CustomReactions.GetByGuildIdAndInput(guildId, input);
return cr != null; return cr != null;
} }

View File

@@ -10,7 +10,6 @@ namespace NadekoBot.Core.Services.Database
IQuoteRepository Quotes { get; } IQuoteRepository Quotes { get; }
IGuildConfigRepository GuildConfigs { get; } IGuildConfigRepository GuildConfigs { get; }
ICustomReactionRepository CustomReactions { get; }
IWaifuRepository Waifus { get; } IWaifuRepository Waifus { get; }
IDiscordUserRepository DiscordUsers { get; } IDiscordUserRepository DiscordUsers { get; }
IXpRepository Xp { get; } IXpRepository Xp { get; }

View File

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

View File

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

View File

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

View File

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

View File

@@ -15,9 +15,6 @@ namespace NadekoBot.Core.Services.Database
private IGuildConfigRepository _guildConfigs; private IGuildConfigRepository _guildConfigs;
public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context)); public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));
private ICustomReactionRepository _customReactions;
public ICustomReactionRepository CustomReactions => _customReactions ?? (_customReactions = new CustomReactionsRepository(_context));
private IWaifuRepository _waifus; private IWaifuRepository _waifus;
public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context)); public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context));