Massive cleanup

- Removed GuildConfigs repository, moved to extensions
- Moved StreamSettings extension to GuildConfig extensions
- namespace NadekoBot.Core has been simplified to NadekoBot in many places (more to come)
- Replaced some raw delete queries with simple linqtodb queries
This commit is contained in:
Kwoth
2021-06-19 03:32:48 +02:00
parent c7ff982ec1
commit 15dac7e3ed
191 changed files with 563 additions and 571 deletions

View File

@@ -17,6 +17,7 @@ using System.Threading;
using System.Threading.Tasks;
using NadekoBot.Core.Common.Configs;
using NadekoBot.Core.Services.Impl;
using NadekoBot.Modules.Administration;
using Serilog;
namespace NadekoBot.Core.Services
@@ -107,7 +108,7 @@ namespace NadekoBot.Core.Services
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigs.ForId(guild.Id, set => set);
var gc = uow._context.GuildConfigsForId(guild.Id, set => set);
gc.Prefix = prefix;
uow.SaveChanges();
}

View File

@@ -7,10 +7,8 @@ namespace NadekoBot.Core.Services.Database
public interface IUnitOfWork : IDisposable
{
NadekoContext _context { get; }
IGuildConfigRepository GuildConfigs { get; }
IWaifuRepository Waifus { get; }
IDiscordUserRepository DiscordUsers { get; }
IXpRepository Xp { get; }
int SaveChanges();
Task<int> SaveChangesAsync();

View File

@@ -1,7 +1,8 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Models
namespace NadekoBot.Services.Database.Models
{
public class ClubInfo : DbEntity
{

View File

@@ -1,6 +1,7 @@
using System;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Models
namespace NadekoBot.Services.Database.Models
{
public class DiscordUser : DbEntity
{

View File

@@ -1,7 +1,8 @@
using System;
using NadekoBot.Core.Modules.Searches.Common;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Modules.Searches.Common;
namespace NadekoBot.Core.Services.Database.Models
namespace NadekoBot.Services.Database.Models
{
public class FollowedStream : DbEntity
{

View File

@@ -1,6 +1,7 @@
using System;
using NadekoBot.Common.Collections;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Models
{

View File

@@ -1,6 +1,7 @@
using NadekoBot.Extensions;
using System.Collections.Generic;
using NadekoBot.Core.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Models
{

View File

@@ -1,4 +1,6 @@
namespace NadekoBot.Core.Services.Database.Models
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Models
{
public class WaifuUpdate : DbEntity
{

View File

@@ -6,6 +6,7 @@ using NadekoBot.Core.Services.Impl;
using System;
using System.IO;
using Microsoft.Extensions.Logging;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database
{

View File

@@ -4,6 +4,8 @@ using NadekoBot.Core.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
using NadekoBot.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database.Repositories
{

View File

@@ -1,28 +0,0 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NadekoBot.Core.Services.Database.Repositories
{
public interface IGuildConfigRepository : IRepository<GuildConfig>
{
GuildConfig ForId(ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes = null);
GuildConfig LogSettingsFor(ulong guildId);
IEnumerable<GuildConfig> GetAllGuildConfigs(List<ulong> availableGuilds);
IEnumerable<FollowedStream> GetFollowedStreams(List<ulong> included);
IEnumerable<FollowedStream> GetFollowedStreams();
void SetCleverbotEnabled(ulong id, bool cleverbotEnabled);
IEnumerable<GuildConfig> Permissionsv2ForAll(List<ulong> include);
GuildConfig GcWithPermissionsv2For(ulong guildId);
XpSettings XpSettingsFor(ulong guildId);
IEnumerable<GeneratingChannel> GetGeneratingChannels();
}
public class GeneratingChannel
{
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
}
}

View File

@@ -1,21 +1,11 @@
using System.Collections.Generic;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Repositories
namespace NadekoBot.Services.Database.Repositories
{
public interface IRepository<T> where T : DbEntity
{
T GetById(int id);
IEnumerable<T> GetAll();
void Add(T obj);
void AddRange(params T[] objs);
void Remove(int id);
void Remove(T obj);
void RemoveRange(params T[] objs);
void Update(T obj);
void UpdateRange(params T[] objs);
}
}

View File

@@ -3,6 +3,7 @@ using NadekoBot.Core.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database.Repositories
{

View File

@@ -1,15 +0,0 @@
using NadekoBot.Core.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Core.Services.Database.Repositories
{
public interface IXpRepository : IRepository<UserXpStats>
{
UserXpStats GetOrCreateUser(ulong guildId, ulong userId);
int GetUserGuildRanking(ulong userId, ulong guildId);
List<UserXpStats> GetUsersFor(ulong guildId, int page);
void ResetGuildUserXp(ulong userId, ulong guildId);
void ResetGuildXp(ulong guildId);
List<UserXpStats> GetTopUserXps(ulong guildId, int count);
}
}

View File

@@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Xp.Common
{

View File

@@ -1,8 +1,9 @@
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using LinqToDB;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Modules.CustomReactions
{

View File

@@ -1,11 +1,12 @@
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Discord;
using System.Collections.Generic;
using System;
using NadekoBot.Core.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class DiscordUserRepository : Repository<DiscordUser>, IDiscordUserRepository
{

View File

@@ -3,41 +3,38 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
using NadekoBot.Core.Services.Database;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
namespace NadekoBot.Modules.Administration
{
public static class MusicPlayerSettingsExtensions
public static class GuildConfigExtensions
{
public static async Task<MusicPlayerSettings> ForGuildAsync(this DbSet<MusicPlayerSettings> settings, ulong guildId)
public class GeneratingChannel
{
var toReturn = await settings
.AsQueryable()
.FirstOrDefaultAsync(x => x.GuildId == guildId);
if (toReturn is null)
{
var newSettings = new MusicPlayerSettings()
{
GuildId = guildId,
PlayerRepeat = PlayerRepeatType.Queue
};
await settings.AddAsync(newSettings);
return newSettings;
}
return toReturn;
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
}
}
public class GuildConfigRepository : Repository<GuildConfig>, IGuildConfigRepository
{
public GuildConfigRepository(DbContext context) : base(context)
/// <summary>
/// Gets full stream role settings for the guild with the specified id.
/// </summary>
/// <param name="ctx">Db Context</param>
/// <param name="guildId">Id of the guild to get stream role settings for.</param>
/// <returns>Guild'p stream role settings</returns>
public static StreamRoleSettings GetStreamRoleSettings(this NadekoContext ctx, ulong guildId)
{
}
var conf = ctx.GuildConfigsForId(guildId, set => set.Include(y => y.StreamRole)
.Include(y => y.StreamRole.Whitelist)
.Include(y => y.StreamRole.Blacklist));
private List<WarningPunishment> DefaultWarnPunishments =>
if (conf.StreamRole == null)
conf.StreamRole = new StreamRoleSettings();
return conf.StreamRole;
}
private static List<WarningPunishment> DefaultWarnPunishments =>
new List<WarningPunishment>() {
new WarningPunishment() {
Count = 3,
@@ -49,59 +46,62 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
}
};
public IEnumerable<GuildConfig> GetAllGuildConfigs(List<ulong> availableGuilds) =>
IncludeEverything()
.AsNoTracking()
.Where(x => availableGuilds.Contains(x.GuildId))
.ToList();
private IQueryable<GuildConfig> IncludeEverything()
private static IQueryable<GuildConfig> IncludeEverything(this DbSet<GuildConfig> configs)
{
return _set
.AsQueryable()
.Include(gc => gc.CommandCooldowns)
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole)
.Include(gc => gc.NsfwBlacklistedTags)
.Include(gc => gc.XpSettings)
return configs
.AsQueryable()
.Include(gc => gc.CommandCooldowns)
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole)
.Include(gc => gc.NsfwBlacklistedTags)
.Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList)
.Include(gc => gc.DelMsgOnCmdChannels)
.Include(gc => gc.ReactionRoleMessages)
.Include(gc => gc.DelMsgOnCmdChannels)
.Include(gc => gc.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles)
;
}
public static IEnumerable<GuildConfig> GetAllGuildConfigs(this DbSet<GuildConfig> configs, List<ulong> availableGuilds)
=> configs
.IncludeEverything()
.AsNoTracking()
.Where(x => availableGuilds.Contains(x.GuildId))
.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 ForId(ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes)
public static GuildConfig GuildConfigsForId(this NadekoContext ctx, ulong guildId, Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes = null)
{
GuildConfig config;
if (includes == null)
{
config = IncludeEverything()
config = ctx
.GuildConfigs
.IncludeEverything()
.FirstOrDefault(c => c.GuildId == guildId);
}
else
{
var set = includes(_set);
var set = includes(ctx.GuildConfigs);
config = set.FirstOrDefault(c => c.GuildId == guildId);
}
if (config == null)
{
_set.Add((config = new GuildConfig
ctx.GuildConfigs.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
WarningsInitialized = true,
WarnPunishments = DefaultWarnPunishments,
}));
_context.SaveChanges();
ctx.SaveChanges();
}
if (!config.WarningsInitialized)
@@ -113,9 +113,10 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return config;
}
public GuildConfig LogSettingsFor(ulong guildId)
public static GuildConfig LogSettingsFor(this NadekoContext ctx, ulong guildId)
{
var config = _set
var config = ctx
.GuildConfigs
.AsQueryable()
.Include(gc => gc.LogSetting)
.ThenInclude(gc => gc.IgnoredChannels)
@@ -123,14 +124,14 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
if (config == null)
{
_set.Add((config = new GuildConfig
ctx.GuildConfigs.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
WarningsInitialized = true,
WarnPunishments = DefaultWarnPunishments,
}));
_context.SaveChanges();
ctx.SaveChanges();
}
if (!config.WarningsInitialized)
@@ -141,61 +142,63 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return config;
}
public IEnumerable<GuildConfig> Permissionsv2ForAll(List<ulong> include)
public static IEnumerable<GuildConfig> Permissionsv2ForAll(this DbSet<GuildConfig> configs, List<ulong> include)
{
var query = _set.AsQueryable()
var query = configs.AsQueryable()
.Where(x => include.Contains(x.GuildId))
.Include(gc => gc.Permissions);
return query.ToList();
}
public GuildConfig GcWithPermissionsv2For(ulong guildId)
public static GuildConfig GcWithPermissionsv2For(this NadekoContext ctx, ulong guildId)
{
var config = _set.AsQueryable()
var config = ctx
.GuildConfigs
.AsQueryable()
.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
ctx.GuildConfigs.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist
}));
_context.SaveChanges();
ctx.SaveChanges();
}
else if (config.Permissions == null || !config.Permissions.Any()) // if no perms, add default ones
{
config.Permissions = Permissionv2.GetDefaultPermlist;
_context.SaveChanges();
ctx.SaveChanges();
}
return config;
}
public IEnumerable<FollowedStream> GetFollowedStreams()
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs)
{
return _set
return configs
.AsQueryable()
.Include(x => x.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToArray();
}
public IEnumerable<FollowedStream> GetFollowedStreams(List<ulong> included)
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs, List<ulong> included)
{
return _set.AsQueryable()
return configs.AsQueryable()
.Where(gc => included.Contains(gc.GuildId))
.Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToList();
}
public void SetCleverbotEnabled(ulong id, bool cleverbotEnabled)
public static void SetCleverbotEnabled(this DbSet<GuildConfig> configs, ulong id, bool cleverbotEnabled)
{
var conf = _set.FirstOrDefault(gc => gc.GuildId == id);
var conf = configs.FirstOrDefault(gc => gc.GuildId == id);
if (conf == null)
return;
@@ -203,9 +206,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
conf.CleverbotEnabled = cleverbotEnabled;
}
public XpSettings XpSettingsFor(ulong guildId)
public static XpSettings XpSettingsFor(this NadekoContext ctx, ulong guildId)
{
var gc = ForId(guildId,
var gc = ctx.GuildConfigsForId(guildId,
set => set.Include(x => x.XpSettings)
.ThenInclude(x => x.RoleRewards)
.Include(x => x.XpSettings)
@@ -219,9 +222,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return gc.XpSettings;
}
public IEnumerable<GeneratingChannel> GetGeneratingChannels()
public static IEnumerable<GeneratingChannel> GetGeneratingChannels(this DbSet<GuildConfig> configs)
{
return _set
return configs
.AsQueryable()
.Include(x => x.GenerateCurrencyChannelIds)
.Where(x => x.GenerateCurrencyChannelIds.Any())

View File

@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
{
public static class MusicPlayerSettingsExtensions
{
public static async Task<MusicPlayerSettings> ForGuildAsync(this DbSet<MusicPlayerSettings> settings, ulong guildId)
{
var toReturn = await settings
.AsQueryable()
.FirstOrDefaultAsync(x => x.GuildId == guildId);
if (toReturn is null)
{
var newSettings = new MusicPlayerSettings()
{
GuildId = guildId,
PlayerRepeat = PlayerRepeatType.Queue
};
await settings.AddAsync(newSettings);
return newSettings;
}
return toReturn;
}
}
}

View File

@@ -1,8 +1,9 @@
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Modules.Music
{

View File

@@ -1,8 +1,10 @@
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database;
namespace NadekoBot.Modules.Games.Common
{

View File

@@ -1,9 +1,10 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Core.Services.Database.Models;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
namespace NadekoBot.Services.Database.Repositories.Impl
{
public abstract class Repository<T> : IRepository<T> where T : DbEntity
{
@@ -18,29 +19,5 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
public void Add(T obj) =>
_set.Add(obj);
public void AddRange(params T[] objs) =>
_set.AddRange(objs);
public T GetById(int id) =>
_set.FirstOrDefault(e => e.Id == id);
public IEnumerable<T> GetAll() =>
_set.ToList();
public void Remove(int id) =>
_set.Remove(this.GetById(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);
}
}

View File

@@ -1,23 +1,23 @@
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using LinqToDB;
using NadekoBot.Core.Services.Database;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
namespace NadekoBot.Modules.Xp.Common
{
public class XpRepository : Repository<UserXpStats>, IXpRepository
public static class UserXpExtensions
{
public XpRepository(DbContext context) : base(context)
public static UserXpStats GetOrCreateUserXpStats(this NadekoContext ctx, ulong guildId, ulong userId)
{
}
public UserXpStats GetOrCreateUser(ulong guildId, ulong userId)
{
var usr = _set.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId);
var usr = ctx.UserXpStats.FirstOrDefault(x => x.UserId == userId && x.GuildId == guildId);
if (usr == null)
{
_context.Add(usr = new UserXpStats()
ctx.Add(usr = new UserXpStats()
{
Xp = 0,
UserId = userId,
@@ -29,9 +29,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
return usr;
}
public List<UserXpStats> GetUsersFor(ulong guildId, int page)
public static List<UserXpStats> GetUsersFor(this DbSet<UserXpStats> xps, ulong guildId, int page)
{
return _set
return xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId)
@@ -41,9 +41,9 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
.ToList();
}
public List<UserXpStats> GetTopUserXps(ulong guildId, int count)
public static List<UserXpStats> GetTopUserXps(this DbSet<UserXpStats> xps, ulong guildId, int count)
{
return _set
return xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId)
@@ -52,7 +52,7 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
.ToList();
}
public int GetUserGuildRanking(ulong userId, ulong guildId)
public static int GetUserGuildRanking(this DbSet<UserXpStats> xps, ulong userId, ulong guildId)
{
// @"SELECT COUNT(*) + 1
//FROM UserXpStats
@@ -61,11 +61,11 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
// WHERE UserId = @p2 AND GuildId = @p1
// LIMIT 1));";
return _set
return xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId && ((x.Xp + x.AwardedXp) >
(_set.AsQueryable()
(xps.AsQueryable()
.Where(y => y.UserId == userId && y.GuildId == guildId)
.Select(y => y.Xp + y.AwardedXp)
.FirstOrDefault())
@@ -73,14 +73,14 @@ namespace NadekoBot.Core.Services.Database.Repositories.Impl
.Count() + 1;
}
public void ResetGuildUserXp(ulong userId, ulong guildId)
public static void ResetGuildUserXp(this DbSet<UserXpStats> xps, ulong userId, ulong guildId)
{
_context.Database.ExecuteSqlInterpolated($"DELETE FROM UserXpStats WHERE UserId={userId} AND GuildId={guildId};");
xps.Delete(x => x.UserId == userId && x.GuildId == guildId);
}
public void ResetGuildXp(ulong guildId)
public static void ResetGuildXp(this DbSet<UserXpStats> xps, ulong guildId)
{
_context.Database.ExecuteSqlInterpolated($"DELETE FROM UserXpStats WHERE GuildId={guildId};");
xps.Delete(x => x.GuildId == guildId);
}
}
}

View File

@@ -1,10 +1,12 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Services.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database.Repositories.Impl
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class WaifuRepository : Repository<WaifuInfo>, IWaifuRepository
{

View File

@@ -1,26 +1,22 @@
using NadekoBot.Core.Services.Database.Repositories;
using NadekoBot.Core.Services.Database.Repositories.Impl;
using NadekoBot.Services.Database.Repositories;
using NadekoBot.Services.Database.Repositories.Impl;
using System;
using System.Threading.Tasks;
using NadekoBot.Core.Services.Database;
using NadekoBot.Core.Services.Database.Repositories;
namespace NadekoBot.Core.Services.Database
namespace NadekoBot.Services.Database
{
public sealed class UnitOfWork : IUnitOfWork
{
public NadekoContext _context { get; }
private IGuildConfigRepository _guildConfigs;
public IGuildConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new GuildConfigRepository(_context));
private IWaifuRepository _waifus;
public IWaifuRepository Waifus => _waifus ?? (_waifus = new WaifuRepository(_context));
private IDiscordUserRepository _discordUsers;
public IDiscordUserRepository DiscordUsers => _discordUsers ?? (_discordUsers = new DiscordUserRepository(_context));
private IXpRepository _xp;
public IXpRepository Xp => _xp ?? (_xp = new XpRepository(_context));
public UnitOfWork(NadekoContext context)
{
_context = context;

View File

@@ -4,6 +4,7 @@ using NadekoBot.Core.Services.Database;
using System;
using System.IO;
using System.Linq;
using NadekoBot.Services.Database;
namespace NadekoBot.Core.Services
{

View File

@@ -9,6 +9,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Modules.Administration;
using Serilog;
namespace NadekoBot.Core.Services
@@ -106,7 +107,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
return uow.GuildConfigs.ForId(id, set => set)?.DmGreetMessageText;
return uow._context.GuildConfigsForId(id, set => set)?.DmGreetMessageText;
}
}
@@ -114,7 +115,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
return uow.GuildConfigs.ForId(gid, set => set).ChannelGreetMessageText;
return uow._context.GuildConfigsForId(gid, set => set).ChannelGreetMessageText;
}
}
@@ -318,7 +319,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
return uow.GuildConfigs.ForId(gid, set => set).ChannelByeMessageText;
return uow._context.GuildConfigsForId(gid, set => set).ChannelByeMessageText;
}
}
@@ -330,7 +331,7 @@ namespace NadekoBot.Core.Services
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigs.ForId(guildId, set => set);
var gc = uow._context.GuildConfigsForId(guildId, set => set);
settings = GreetSettings.Create(gc);
}
@@ -350,7 +351,7 @@ namespace NadekoBot.Core.Services
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
conf.DmGreetMessageText = settings.DmGreetMessageText?.SanitizeMentions();
conf.ChannelGreetMessageText = settings.ChannelGreetMessageText?.SanitizeMentions();
conf.ChannelByeMessageText = settings.ChannelByeMessageText?.SanitizeMentions();
@@ -381,7 +382,7 @@ namespace NadekoBot.Core.Services
bool enabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
enabled = conf.SendChannelGreetMessage = value ?? !conf.SendChannelGreetMessage;
conf.GreetMessageChannelId = channelId;
@@ -403,7 +404,7 @@ namespace NadekoBot.Core.Services
bool greetMsgEnabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
conf.ChannelGreetMessageText = message;
greetMsgEnabled = conf.SendChannelGreetMessage;
@@ -420,7 +421,7 @@ namespace NadekoBot.Core.Services
bool enabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
enabled = conf.SendDmGreetMessage = value ?? !conf.SendDmGreetMessage;
var toAdd = GreetSettings.Create(conf);
@@ -436,7 +437,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
return conf.SendDmGreetMessage;
}
}
@@ -445,7 +446,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
return conf.SendChannelGreetMessage;
}
}
@@ -454,7 +455,7 @@ namespace NadekoBot.Core.Services
{
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
return conf.SendChannelByeMessage;
}
}
@@ -491,7 +492,7 @@ namespace NadekoBot.Core.Services
bool greetMsgEnabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
conf.DmGreetMessageText = message;
greetMsgEnabled = conf.SendDmGreetMessage;
@@ -508,7 +509,7 @@ namespace NadekoBot.Core.Services
bool enabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
enabled = conf.SendChannelByeMessage = value ?? !conf.SendChannelByeMessage;
conf.ByeMessageChannelId = channelId;
@@ -530,7 +531,7 @@ namespace NadekoBot.Core.Services
bool byeMsgEnabled;
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
conf.ChannelByeMessageText = message;
byeMsgEnabled = conf.SendChannelByeMessage;
@@ -549,7 +550,7 @@ namespace NadekoBot.Core.Services
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(guildId, set => set);
var conf = uow._context.GuildConfigsForId(guildId, set => set);
conf.AutoDeleteByeMessagesTimer = timer;
var toAdd = GreetSettings.Create(conf);
@@ -566,7 +567,7 @@ namespace NadekoBot.Core.Services
using (var uow = _db.GetDbContext())
{
var conf = uow.GuildConfigs.ForId(id, set => set);
var conf = uow._context.GuildConfigsForId(id, set => set);
conf.AutoDeleteGreetMessagesTimer = timer;
var toAdd = GreetSettings.Create(conf);

View File

@@ -7,7 +7,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Core.Modules.Gambling.Services;
using NadekoBot.Modules.Gambling.Services;
namespace NadekoBot.Core.Services
{

View File

@@ -6,6 +6,7 @@ using Discord;
using NadekoBot.Common;
using Newtonsoft.Json;
using System.IO;
using NadekoBot.Modules.Administration;
namespace NadekoBot.Core.Services.Impl
{
@@ -55,7 +56,7 @@ namespace NadekoBot.Core.Services.Impl
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigs.ForId(guildId, set => set);
var gc = uow._context.GuildConfigsForId(guildId, set => set);
gc.Locale = ci.Name;
uow.SaveChanges();
}
@@ -73,7 +74,7 @@ namespace NadekoBot.Core.Services.Impl
{
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigs.ForId(guildId, set => set);
var gc = uow._context.GuildConfigsForId(guildId, set => set);
gc.Locale = null;
uow.SaveChanges();
}

View File

@@ -25,7 +25,8 @@ using LinqToDB.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Core.Common;
using NadekoBot.Core.Common.Configs;
using NadekoBot.Core.Modules.Gambling.Services;
using NadekoBot.Modules.Administration;
using NadekoBot.Modules.Gambling.Services;
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Modules.CustomReactions.Services;
using NadekoBot.Modules.Utility.Services;
@@ -135,7 +136,7 @@ namespace NadekoBot
public IEnumerable<GuildConfig> GetCurrentGuildConfigs()
{
using var uow = _db.GetDbContext();
return uow.GuildConfigs.GetAllGuildConfigs(GetCurrentGuildIds()).ToImmutableArray();
return uow._context.GuildConfigs.GetAllGuildConfigs(GetCurrentGuildIds()).ToImmutableArray();
}
private void AddServices()
@@ -147,7 +148,7 @@ namespace NadekoBot
using (var uow = _db.GetDbContext())
{
uow.DiscordUsers.EnsureCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
AllGuildConfigs = uow._context.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
}
var s = new ServiceCollection()
@@ -307,13 +308,13 @@ namespace NadekoBot
private Task Client_JoinedGuild(SocketGuild arg)
{
Log.Information($"Joined server: {0} [{1}]", arg?.Name, arg?.Id);
Log.Information($"Joined server: {0} [{1}]", arg.Name, arg.Id);
var _ = Task.Run(async () =>
{
GuildConfig gc;
using (var uow = _db.GetDbContext())
{
gc = uow.GuildConfigs.ForId(arg.Id);
gc = uow._context.GuildConfigsForId(arg.Id);
}
await JoinedGuild.Invoke(gc).ConfigureAwait(false);
});

View File

@@ -1,8 +1,8 @@
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Modules.Gambling.Common;
using NadekoBot.Core.Modules.Gambling.Services;
using NadekoBot.Modules.Gambling.Common;
using NadekoBot.Modules.Gambling.Services;
using Serilog;
namespace NadekoBot.Core.Services