mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Migrate to database storage in UrlLimitations
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Extensions
|
||||
{
|
||||
@@ -43,5 +45,67 @@ namespace Kruzya.TelegramBot.Core.Extensions
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Core-context repository extensions
|
||||
|
||||
#region BotUserValue
|
||||
|
||||
#region FindOption
|
||||
|
||||
public static async Task<BotUserValue> FindOption(this DbSet<BotUserValue> repository, long chatId,
|
||||
string option) => await repository.FindOption(chatId, 0, option);
|
||||
|
||||
public static async Task<BotUserValue> FindOption(this DbSet<BotUserValue> repository, BotUser user,
|
||||
string option) => await repository.FindOption(user.ChatId, user.UserId, option);
|
||||
|
||||
public static async Task<BotUserValue> FindOption(this DbSet<BotUserValue> repository, long chatId, int userId,
|
||||
string option) => await repository.SingleOrDefaultAsync(e =>
|
||||
e.BotUser.ChatId == chatId && e.BotUser.UserId == userId && e.Name == option);
|
||||
|
||||
#endregion
|
||||
|
||||
#region FindOrCreateOption
|
||||
|
||||
public static async Task<BotUserValue> FindOrCreateOption(this DbSet<BotUserValue> repository, long chatId,
|
||||
string option) => await repository.FindOrCreateOption(chatId, 0, option);
|
||||
|
||||
public static async Task<BotUserValue> FindOrCreateOption(this DbSet<BotUserValue> repository, BotUser user,
|
||||
string option) => await repository.FindOrCreateOption(user.ChatId, user.UserId, option);
|
||||
|
||||
public static async Task<BotUserValue> FindOrCreateOption(this DbSet<BotUserValue> repository, long chatId,
|
||||
int userId, string option)
|
||||
{
|
||||
var opt = await repository.FindOption(chatId, userId, option);
|
||||
if (opt == null)
|
||||
{
|
||||
opt = repository.Create();
|
||||
}
|
||||
|
||||
return opt;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetOptionValue
|
||||
|
||||
public static async Task SetOptionValue<T>(this DbSet<BotUserValue> repository, long chatId, string option,
|
||||
T val) => await repository.SetOptionValue(chatId, 0, option, val);
|
||||
|
||||
public static async Task SetOptionValue<T>(this DbSet<BotUserValue> repository, BotUser user, string option,
|
||||
T val) => await repository.SetOptionValue(user.ChatId, user.UserId, option, val);
|
||||
|
||||
public static async Task SetOptionValue<T>(this DbSet<BotUserValue> repository, long chatId, int userId,
|
||||
string option, T val)
|
||||
{
|
||||
var opt = await repository.FindOrCreateOption(chatId, userId, option);
|
||||
opt.SetValue(val);
|
||||
repository.Update(opt);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user