Seperate db from RequestContext

This commit is contained in:
Nikolay Kochulin 2019-12-09 08:53:19 +00:00
parent 7843a60fd9
commit 3f1800bb0c
11 changed files with 41 additions and 23 deletions

View File

@ -6,6 +6,7 @@ using JetKarmaBot.Services.Handling;
using NLog; using NLog;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -16,7 +17,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
var awarder = ctx.EventArgs.Message.From; var awarder = ctx.EventArgs.Message.From;

View File

@ -4,6 +4,7 @@ using JetKarmaBot.Services.Handling;
using NLog; using NLog;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -14,7 +15,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var cmd = ctx.Command; var cmd = ctx.Command;
var args = ctx.EventArgs; var args = ctx.EventArgs;

View File

@ -1,10 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using Perfusion; using Perfusion;
using JetKarmaBot.Services.Handling; using JetKarmaBot.Services.Handling;
using Telegram.Bot.Types.Enums;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -21,7 +21,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
await ctx.SendMessage( await ctx.SendMessage(
currentLocale["jetkarmabot.currencies.listtext"] + "\n" + string.Join("\n", currentLocale["jetkarmabot.currencies.listtext"] + "\n" + string.Join("\n",

View File

@ -3,6 +3,7 @@ using Perfusion;
using JetKarmaBot.Services.Handling; using JetKarmaBot.Services.Handling;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -26,7 +27,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
var router = ctx.GetFeature<ChatCommandRouter.Feature>().Router; var router = ctx.GetFeature<ChatCommandRouter.Feature>().Router;
if (ctx.Command.Parameters.Length < 1) if (ctx.Command.Parameters.Length < 1)

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Perfusion; using Perfusion;
using JetKarmaBot.Services.Handling; using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -13,7 +14,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
var asker = ctx.EventArgs.Message.From; var asker = ctx.EventArgs.Message.From;
var awardTypeName = ctx.Command.Parameters.FirstOrDefault(); var awardTypeName = ctx.Command.Parameters.FirstOrDefault();

View File

@ -4,6 +4,7 @@ using Perfusion;
using JetKarmaBot.Services.Handling; using JetKarmaBot.Services.Handling;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands namespace JetKarmaBot.Commands
{ {
@ -13,7 +14,7 @@ namespace JetKarmaBot.Commands
public async Task<bool> Execute(RequestContext ctx) public async Task<bool> Execute(RequestContext ctx)
{ {
var db = ctx.Database; var db = ctx.GetFeature<KarmaContext>();
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
var asker = ctx.EventArgs.Message.From; var asker = ctx.EventArgs.Message.From;
var awardTypeName = ctx.Command.Parameters.FirstOrDefault(); var awardTypeName = ctx.Command.Parameters.FirstOrDefault();

View File

@ -75,15 +75,8 @@ namespace JetKarmaBot
if (cmd.UserName != null && cmd.UserName != Commands.Me.Username) if (cmd.UserName != null && cmd.UserName != Commands.Me.Username)
return; return;
Task.Run(async () => RequestContext ctx = new RequestContext(Client, args, cmd);
{ _ = Chain.Handle(ctx);
using (KarmaContext db = Db.GetContext())
{
RequestContext ctx = new RequestContext(Client, args, cmd, db);
await Chain.Handle(ctx);
await db.SaveChangesAsync();
}
});
} }
async Task InitCommands(IContainer c) async Task InitCommands(IContainer c)
@ -105,6 +98,7 @@ namespace JetKarmaBot
void InitChain(IContainer c) void InitChain(IContainer c)
{ {
Chain = new RequestChain(); Chain = new RequestChain();
Chain.Add(c.GetInstance<DatabaseHandler>());
Chain.Add(Timeout); Chain.Add(Timeout);
Chain.Add(new SaveData()); Chain.Add(new SaveData());
Chain.Add(Commands); Chain.Add(Commands);

View File

@ -0,0 +1,20 @@
using System;
using System.Threading.Tasks;
using Perfusion;
namespace JetKarmaBot.Services.Handling
{
public class DatabaseHandler : IRequestHandler
{
[Inject] private KarmaContextFactory Db;
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
{
using (var db = Db.GetContext())
{
ctx.Features.Add(db);
await next(ctx);
await db.SaveChangesAsync();
}
}
}
}

View File

@ -14,14 +14,12 @@ namespace JetKarmaBot.Services.Handling
public ITelegramBotClient Client { get; } public ITelegramBotClient Client { get; }
public MessageEventArgs EventArgs { get; } public MessageEventArgs EventArgs { get; }
public CommandString Command { get; } public CommandString Command { get; }
public KarmaContext Database { get; }
public ICollection<object> Features { get; } = new List<object>(); public ICollection<object> Features { get; } = new List<object>();
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd, KarmaContext db) public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
{ {
Client = client; Client = client;
EventArgs = args; EventArgs = args;
Command = cmd; Command = cmd;
Database = db;
} }
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType); public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
public T GetFeature<T>() => (T)Features.First(x => x is T); public T GetFeature<T>() => (T)Features.First(x => x is T);

View File

@ -9,7 +9,7 @@ namespace JetKarmaBot.Services.Handling
{ {
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next) public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
{ {
KarmaContext db = ctx.Database; KarmaContext db = ctx.GetFeature<KarmaContext>();
await AddUserToDatabase(db, ctx.EventArgs.Message.From); await AddUserToDatabase(db, ctx.EventArgs.Message.From);
if (ctx.EventArgs.Message.ReplyToMessage != null) if (ctx.EventArgs.Message.ReplyToMessage != null)
await AddUserToDatabase(db, ctx.EventArgs.Message.ReplyToMessage.From); await AddUserToDatabase(db, ctx.EventArgs.Message.ReplyToMessage.From);

View File

@ -74,13 +74,14 @@ namespace JetKarmaBot.Services.Handling
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next) public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
{ {
int uid = ctx.EventArgs.Message.From.Id; int uid = ctx.EventArgs.Message.From.Id;
await PopulateStats(uid, ctx.Database); KarmaContext db = ctx.GetFeature<KarmaContext>();
await PopulateStats(uid, db);
DateTime debtLimit = DateTime.Now.AddSeconds(cfg.Timeout.DebtLimitSeconds); DateTime debtLimit = DateTime.Now.AddSeconds(cfg.Timeout.DebtLimitSeconds);
if (debtLimit < TimeoutCache[uid].CooldownDate) if (debtLimit < TimeoutCache[uid].CooldownDate)
{ {
if (!TimeoutCache[uid].TimeoutMessaged) if (!TimeoutCache[uid].TimeoutMessaged)
{ {
Locale currentLocale = Locale[(await ctx.Database.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale]; Locale currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]); await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]);
TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate }; TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate };
} }
@ -90,7 +91,7 @@ namespace JetKarmaBot.Services.Handling
await next(ctx); await next(ctx);
var routerFeature = ctx.GetFeature<ChatCommandRouter.Feature>(); var routerFeature = ctx.GetFeature<ChatCommandRouter.Feature>();
await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, ctx.Database); await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, db);
} }
private string getTypeName(Type t) private string getTypeName(Type t)
{ {