From 8681c7cf690df69851d1bd0dfcd9957df0e60a2a Mon Sep 17 00:00:00 2001 From: Kruzya Date: Mon, 9 Mar 2020 19:31:16 +0400 Subject: [PATCH] Scratch for running next callbacks in queue --- Core/Data/CoreContext.cs | 13 +++++++++++++ Core/Extensions/RepositoryExtension.cs | 19 +++++++++++++++++++ Core/GeneralHandler.cs | 2 ++ 3 files changed, 34 insertions(+) diff --git a/Core/Data/CoreContext.cs b/Core/Data/CoreContext.cs index 6b5f44c..900e0a4 100644 --- a/Core/Data/CoreContext.cs +++ b/Core/Data/CoreContext.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; namespace Kruzya.TelegramBot.Core.Data @@ -11,6 +12,18 @@ namespace Kruzya.TelegramBot.Core.Data public DbSet UserValues { get; set; } + public override void Dispose() + { + SaveChanges(); + base.Dispose(); + } + + public override ValueTask DisposeAsync() + { + SaveChanges(); + return base.DisposeAsync(); + } + public CoreContext(DbContextOptions ctx) : base(ctx) { Database.Migrate(); diff --git a/Core/Extensions/RepositoryExtension.cs b/Core/Extensions/RepositoryExtension.cs index 3b9c4f7..dc53846 100644 --- a/Core/Extensions/RepositoryExtension.cs +++ b/Core/Extensions/RepositoryExtension.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Kruzya.TelegramBot.Core.Data; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; namespace Kruzya.TelegramBot.Core.Extensions { @@ -48,6 +49,23 @@ namespace Kruzya.TelegramBot.Core.Extensions #region Core-context repository extensions + #region BotUser + + public static async Task FindOrCreate(this DbSet repository, long chatId, int userId) + { + var user = await repository.SingleOrDefaultAsync(e => e.ChatId == chatId && e.UserId == userId); + if (user == null) + { + user = repository.Create(); + user.ChatId = chatId; + user.UserId = userId; + } + + return user; + } + + #endregion + #region BotUserValue #region FindOption @@ -79,6 +97,7 @@ namespace Kruzya.TelegramBot.Core.Extensions if (opt == null) { opt = repository.Create(); + opt.BotUser = await repository.GetService().Users.FindOrCreate(chatId, userId); } return opt; diff --git a/Core/GeneralHandler.cs b/Core/GeneralHandler.cs index 0a8c4cd..72bd5af 100644 --- a/Core/GeneralHandler.cs +++ b/Core/GeneralHandler.cs @@ -39,6 +39,8 @@ namespace Kruzya.TelegramBot.Core await VerifyChatPair(message); } + + throw new ArgumentException(); } public async Task VerifyChatPair(Message message)