Files

40 lines
1.1 KiB
C#
Raw Permalink Normal View History

2020-03-02 00:00:44 +04:00
using System;
using BotFramework;
using Microsoft.EntityFrameworkCore;
2023-07-29 15:14:52 +03:00
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
2020-03-02 00:00:44 +04:00
{
2023-07-29 15:14:52 +03:00
private TAppContext _dbContext;
2020-03-02 00:00:44 +04:00
2023-07-29 15:14:52 +03:00
public DbResolverParameter(TAppContext dbContext)
{
_dbContext = dbContext;
}
2020-03-02 00:00:44 +04:00
2023-07-29 15:14:52 +03:00
public TResolvedEntity DefaultInstance()
{
return default(TResolvedEntity);
}
2020-03-02 00:00:44 +04:00
2023-07-29 15:14:52 +03:00
public bool TryGetValue(string text, out TResolvedEntity result)
{
result = null;
Guid id;
if (Guid.TryParse(text, out id))
2020-03-02 00:00:44 +04:00
{
2023-07-29 15:14:52 +03:00
result = _dbContext.Find<TResolvedEntity>(id);
return (result != null);
2020-03-02 00:00:44 +04:00
}
2023-07-29 15:14:52 +03:00
return false;
2020-03-02 00:00:44 +04:00
}
}