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,40 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Extensions;
|
||||
|
||||
public static class DbContextExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks the entity as created.
|
||||
/// </summary>
|
||||
/// <param name="dbContext"></param>
|
||||
/// <param name="entity">Entity for marking as created.</param>
|
||||
[Obsolete("DbContextExtension.MarkAsCreated is obsolete, use DbSet<T>.Add instead")]
|
||||
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>
|
||||
[Obsolete("DbContextExtension.MarkAsDeleted is obsolete, use DbSet<T>.Remove instead")]
|
||||
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>
|
||||
[Obsolete("DbContextExtension.MarkAsModified is obsolete, use DbContext.Update instead")]
|
||||
public static void MarkAsModified(this DbContext dbContext, object entity)
|
||||
{
|
||||
var entry = dbContext.Entry(entity);
|
||||
if (entry.State == EntityState.Added)
|
||||
return;
|
||||
|
||||
entry.State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -5,44 +5,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Extensions
|
||||
{
|
||||
public static class RepositoryExtension
|
||||
public static partial 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)
|
||||
{
|
||||
var entry = dbContext.Entry(entity);
|
||||
if (entry.State == EntityState.Added)
|
||||
return;
|
||||
|
||||
entry.State = EntityState.Modified;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Repository
|
||||
|
||||
public static TEntity Create<TEntity>(this DbSet<TEntity> repository) where TEntity : class, new()
|
||||
{
|
||||
var entity = new TEntity();
|
||||
@@ -50,88 +14,5 @@ namespace Kruzya.TelegramBot.Core.Extensions
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Core-context repository extensions
|
||||
|
||||
#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
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user