mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
111 lines
4.1 KiB
C#
111 lines
4.1 KiB
C#
using System.Threading.Tasks;
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Kruzya.TelegramBot.Core.Extensions
|
|
{
|
|
public static class RepositoryExtension
|
|
{
|
|
#region DbContext
|
|
|
|
/// <summary>
|
|
/// Marks the entity as created.
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="entity">Entity for marking as created.</param>
|
|
public static void MarkAsCreated(this DbContext dbContext, object entity) =>
|
|
dbContext.Entry(entity).State = EntityState.Added;
|
|
|
|
/// <summary>
|
|
/// Marks the entity as deleted.
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="entity">Entity for marking as deleted.</param>
|
|
public static void MarkAsDeleted(this DbContext dbContext, object entity) =>
|
|
dbContext.Entry(entity).State = EntityState.Deleted;
|
|
|
|
/// <summary>
|
|
/// Marks the entity as modified.
|
|
/// </summary>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="entity">Entity for marking as modified.</param>
|
|
public static void MarkAsModified(this DbContext dbContext, object entity) =>
|
|
dbContext.Entry(entity).State = EntityState.Modified;
|
|
|
|
#endregion
|
|
|
|
#region Repository
|
|
|
|
public static TEntity Create<TEntity>(this DbSet<TEntity> repository) where TEntity : class, new()
|
|
{
|
|
var entity = new TEntity();
|
|
repository.Add(entity);
|
|
|
|
return entity;
|
|
}
|
|
|
|
#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
|
|
}
|
|
} |