mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Code cleanup
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using System.Threading.Tasks;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Extensions
|
||||
{
|
||||
public static partial class RepositoryExtension
|
||||
{
|
||||
#region BotUser
|
||||
|
||||
public static async Task<BotUser> FindOrCreate(this DbSet<BotUser> repository, long chatId, long 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
|
||||
|
||||
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, long 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,
|
||||
long userId, string option)
|
||||
{
|
||||
var opt = await repository.FindOption(chatId, userId, option);
|
||||
if (opt == null)
|
||||
{
|
||||
opt = repository.Create();
|
||||
opt.Name = option;
|
||||
opt.BotUser = await repository.GetService<CoreContext>().Users.FindOrCreate(chatId, userId);
|
||||
}
|
||||
|
||||
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, long userId,
|
||||
string option, T val)
|
||||
{
|
||||
var opt = await repository.FindOrCreateOption(chatId, userId, option);
|
||||
opt.SetValue(val);
|
||||
repository.Update(opt);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user