Revert "Re-use the database connection everywhere"
This reverts commit 77b3d612f2
.
This commit is contained in:
parent
77b3d612f2
commit
2a616f8c5d
2 changed files with 21 additions and 17 deletions
|
@ -9,6 +9,7 @@ using Discord.Rest;
|
|||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
using Geekbot.net.Lib.AlmostRedis;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.Lib.ReactionListener;
|
||||
|
@ -22,20 +23,24 @@ namespace Geekbot.net
|
|||
private readonly DatabaseContext _database;
|
||||
private readonly IDiscordClient _client;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly IAlmostRedis _redis;
|
||||
private readonly IServiceProvider _servicesProvider;
|
||||
private readonly CommandService _commands;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IReactionListener _reactionListener;
|
||||
private readonly DatabaseContext _messageCounterDatabaseContext;
|
||||
private readonly RestApplication _applicationInfo;
|
||||
private readonly List<ulong> _ignoredServers;
|
||||
|
||||
public Handlers(DatabaseContext database, IDiscordClient client, IGeekbotLogger logger,
|
||||
public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis,
|
||||
IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository,
|
||||
IReactionListener reactionListener, RestApplication applicationInfo)
|
||||
{
|
||||
_database = database;
|
||||
_database = databaseInitializer.Initialize();
|
||||
_messageCounterDatabaseContext = databaseInitializer.Initialize();
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
_redis = redis;
|
||||
_servicesProvider = servicesProvider;
|
||||
_commands = commands;
|
||||
_userRepository = userRepository;
|
||||
|
@ -124,7 +129,7 @@ namespace Geekbot.net
|
|||
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
|
||||
var rowId = await _database.Database.ExecuteSqlRawAsync(
|
||||
var rowId = await _messageCounterDatabaseContext.Database.ExecuteSqlRawAsync(
|
||||
"UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}",
|
||||
channel.Guild.Id.AsLong(),
|
||||
message.Author.Id.AsLong()
|
||||
|
@ -132,13 +137,13 @@ namespace Geekbot.net
|
|||
|
||||
if (rowId == 0)
|
||||
{
|
||||
await _database.Messages.AddAsync(new MessagesModel
|
||||
_messageCounterDatabaseContext.Messages.Add(new MessagesModel
|
||||
{
|
||||
UserId = message.Author.Id.AsLong(),
|
||||
GuildId = channel.Guild.Id.AsLong(),
|
||||
MessageCount = 1
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
_messageCounterDatabaseContext.SaveChanges();
|
||||
}
|
||||
|
||||
if (message.Author.IsBot) return;
|
||||
|
|
|
@ -41,7 +41,6 @@ namespace Geekbot.net
|
|||
private IUserRepository _userRepository;
|
||||
private RunParameters _runParameters;
|
||||
private IAlmostRedis _redis;
|
||||
private DatabaseContext _database;
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
|
@ -88,11 +87,11 @@ namespace Geekbot.net
|
|||
_commands = new CommandService();
|
||||
|
||||
_databaseInitializer = new DatabaseInitializer(runParameters, logger);
|
||||
_database = _databaseInitializer.Initialize();
|
||||
_database.Database.EnsureCreated();
|
||||
if(!_runParameters.InMemory) _database.Database.Migrate();
|
||||
var database = _databaseInitializer.Initialize();
|
||||
database.Database.EnsureCreated();
|
||||
if(!_runParameters.InMemory) database.Database.Migrate();
|
||||
|
||||
_globalSettings = new GlobalSettings(_database);
|
||||
_globalSettings = new GlobalSettings(database);
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -117,7 +116,7 @@ namespace Geekbot.net
|
|||
|
||||
_services = new ServiceCollection();
|
||||
|
||||
_userRepository = new UserRepository(_database, logger);
|
||||
_userRepository = new UserRepository(_databaseInitializer.Initialize(), logger);
|
||||
var fortunes = new FortunesProvider(logger);
|
||||
var mediaProvider = new MediaProvider(logger);
|
||||
var malClient = new MalClient(_globalSettings, logger);
|
||||
|
@ -138,9 +137,9 @@ namespace Geekbot.net
|
|||
_services.AddSingleton<IMtgManaConverter>(mtgManaConverter);
|
||||
_services.AddSingleton<IWikipediaClient>(wikipediaClient);
|
||||
_services.AddSingleton<IRandomNumberGenerator>(randomNumberGenerator);
|
||||
_services.AddSingleton<DatabaseContext>(_database);
|
||||
_services.AddSingleton(_globalSettings);
|
||||
_services.AddTransient<IHighscoreManager>(e => new HighscoreManager(_database, _userRepository));
|
||||
_services.AddTransient<IHighscoreManager>(e => new HighscoreManager(_databaseInitializer.Initialize(), _userRepository));
|
||||
_services.AddTransient(e => _databaseInitializer.Initialize());
|
||||
|
||||
logger.Information(LogSource.Geekbot, "Connecting to Discord");
|
||||
|
||||
|
@ -163,7 +162,7 @@ namespace Geekbot.net
|
|||
_logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers");
|
||||
|
||||
_logger.Information(LogSource.Geekbot, "Registering Stuff");
|
||||
var translationHandler = new TranslationHandler(_database, _logger);
|
||||
var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger);
|
||||
var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors);
|
||||
var reactionListener = new ReactionListener(_redis.Db);
|
||||
_services.AddSingleton<IErrorHandler>(errorHandler);
|
||||
|
@ -173,7 +172,7 @@ namespace Geekbot.net
|
|||
_servicesProvider = _services.BuildServiceProvider();
|
||||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider);
|
||||
|
||||
var handlers = new Handlers(_database, _client, _logger, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo);
|
||||
var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, applicationInfo);
|
||||
|
||||
_client.MessageReceived += handlers.RunCommand;
|
||||
_client.MessageDeleted += handlers.MessageDeleted;
|
||||
|
@ -208,8 +207,8 @@ namespace Geekbot.net
|
|||
private Task StartWebApi()
|
||||
{
|
||||
_logger.Information(LogSource.Api, "Starting Webserver");
|
||||
var highscoreManager = new HighscoreManager(_database, _userRepository);
|
||||
WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _database, _client, _globalSettings, highscoreManager);
|
||||
var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository);
|
||||
WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, highscoreManager);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue