Seperate db from RequestContext

This commit is contained in:
Basique Evangelist 2019-12-09 08:53:19 +00:00
parent 000a0d8718
commit a4f94428ce
Signed by untrusted user: BasiqueEvangelist
GPG Key ID: B370219149301706
11 changed files with 41 additions and 23 deletions

View File

@ -6,6 +6,7 @@ using JetKarmaBot.Services.Handling;
using NLog;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
{
@ -16,7 +17,7 @@ namespace JetKarmaBot.Commands
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 awarder = ctx.EventArgs.Message.From;

View File

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

View File

@ -1,10 +1,10 @@
using System.Collections.Generic;
using Perfusion;
using JetKarmaBot.Services.Handling;
using Telegram.Bot.Types.Enums;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
{
@ -21,7 +21,7 @@ namespace JetKarmaBot.Commands
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];
await ctx.SendMessage(
currentLocale["jetkarmabot.currencies.listtext"] + "\n" + string.Join("\n",

View File

@ -3,6 +3,7 @@ using Perfusion;
using JetKarmaBot.Services.Handling;
using Telegram.Bot.Types.Enums;
using System.Threading.Tasks;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
{
@ -26,7 +27,7 @@ namespace JetKarmaBot.Commands
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 router = ctx.GetFeature<ChatCommandRouter.Feature>().Router;
if (ctx.Command.Parameters.Length < 1)

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Perfusion;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
{
@ -13,7 +14,7 @@ namespace JetKarmaBot.Commands
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 asker = ctx.EventArgs.Message.From;
var awardTypeName = ctx.Command.Parameters.FirstOrDefault();

View File

@ -4,6 +4,7 @@ using Perfusion;
using JetKarmaBot.Services.Handling;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
{
@ -13,7 +14,7 @@ namespace JetKarmaBot.Commands
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 asker = ctx.EventArgs.Message.From;
var awardTypeName = ctx.Command.Parameters.FirstOrDefault();

View File

@ -75,15 +75,8 @@ namespace JetKarmaBot
if (cmd.UserName != null && cmd.UserName != Commands.Me.Username)
return;
Task.Run(async () =>
{
using (KarmaContext db = Db.GetContext())
{
RequestContext ctx = new RequestContext(Client, args, cmd, db);
await Chain.Handle(ctx);
await db.SaveChangesAsync();
}
});
RequestContext ctx = new RequestContext(Client, args, cmd);
_ = Chain.Handle(ctx);
}
async Task InitCommands(IContainer c)
@ -105,6 +98,7 @@ namespace JetKarmaBot
void InitChain(IContainer c)
{
Chain = new RequestChain();
Chain.Add(c.GetInstance<DatabaseHandler>());
Chain.Add(Timeout);
Chain.Add(new SaveData());
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 MessageEventArgs EventArgs { get; }
public CommandString Command { get; }
public KarmaContext Database { get; }
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;
EventArgs = args;
Command = cmd;
Database = db;
}
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
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)
{
KarmaContext db = ctx.Database;
KarmaContext db = ctx.GetFeature<KarmaContext>();
await AddUserToDatabase(db, ctx.EventArgs.Message.From);
if (ctx.EventArgs.Message.ReplyToMessage != null)
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)
{
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);
if (debtLimit < TimeoutCache[uid].CooldownDate)
{
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"]);
TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate };
}
@ -90,7 +91,7 @@ namespace JetKarmaBot.Services.Handling
await next(ctx);
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)
{