mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using BotFramework;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Kruzya.TelegramBot.Core;
|
|
|
|
/// <summary>
|
|
/// Resolves the GUIDs from database to entities.
|
|
/// </summary>
|
|
/// <typeparam name="TResolvedEntity">Entity type</typeparam>
|
|
/// <typeparam name="TAppContext">Database context where entity should be finded</typeparam>
|
|
public class DbResolverParameter<TResolvedEntity, TAppContext> : IParameterParser<TResolvedEntity>
|
|
where TAppContext : DbContext
|
|
where TResolvedEntity : class
|
|
{
|
|
private TAppContext _dbContext;
|
|
|
|
public DbResolverParameter(TAppContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public TResolvedEntity DefaultInstance()
|
|
{
|
|
return default(TResolvedEntity);
|
|
}
|
|
|
|
public bool TryGetValue(string text, out TResolvedEntity result)
|
|
{
|
|
result = null;
|
|
Guid id;
|
|
if (Guid.TryParse(text, out id))
|
|
{
|
|
result = _dbContext.Find<TResolvedEntity>(id);
|
|
return (result != null);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |