2020-03-08 12:38:00 +04:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-03-09 19:31:16 +04:00
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
2020-03-02 00:00:44 +04:00
|
|
|
|
|
|
|
|
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>
|
2021-12-25 23:52:09 +04:00
|
|
|
public static void MarkAsModified(this DbContext dbContext, object entity)
|
|
|
|
|
{
|
|
|
|
|
var entry = dbContext.Entry(entity);
|
|
|
|
|
if (entry.State == EntityState.Added)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
entry.State = EntityState.Modified;
|
|
|
|
|
}
|
2020-03-02 00:00:44 +04:00
|
|
|
|
|
|
|
|
#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
|
2020-03-08 12:38:00 +04:00
|
|
|
|
|
|
|
|
#region Core-context repository extensions
|
|
|
|
|
|
2020-03-09 19:31:16 +04:00
|
|
|
#region BotUser
|
|
|
|
|
|
2021-12-22 01:27:56 +04:00
|
|
|
public static async Task<BotUser> FindOrCreate(this DbSet<BotUser> repository, long chatId, long userId)
|
2020-03-09 19:31:16 +04:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
2020-03-08 12:38:00 +04:00
|
|
|
#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);
|
|
|
|
|
|
2021-12-22 01:27:56 +04:00
|
|
|
public static async Task<BotUserValue> FindOption(this DbSet<BotUserValue> repository, long chatId, long userId,
|
2020-03-08 12:38:00 +04:00
|
|
|
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,
|
2021-12-22 01:27:56 +04:00
|
|
|
long userId, string option)
|
2020-03-08 12:38:00 +04:00
|
|
|
{
|
|
|
|
|
var opt = await repository.FindOption(chatId, userId, option);
|
|
|
|
|
if (opt == null)
|
|
|
|
|
{
|
|
|
|
|
opt = repository.Create();
|
2021-12-25 23:52:09 +04:00
|
|
|
opt.Name = option;
|
2020-03-09 19:31:16 +04:00
|
|
|
opt.BotUser = await repository.GetService<CoreContext>().Users.FindOrCreate(chatId, userId);
|
2020-03-08 12:38:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-12-22 01:27:56 +04:00
|
|
|
public static async Task SetOptionValue<T>(this DbSet<BotUserValue> repository, long chatId, long userId,
|
2020-03-08 12:38:00 +04:00
|
|
|
string option, T val)
|
|
|
|
|
{
|
|
|
|
|
var opt = await repository.FindOrCreateOption(chatId, userId, option);
|
|
|
|
|
opt.SetValue(val);
|
|
|
|
|
repository.Update(opt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
2020-03-02 00:00:44 +04:00
|
|
|
}
|
|
|
|
|
}
|