Files

27 lines
797 B
C#
Raw Permalink Normal View History

2023-07-21 17:14:11 +03:00
using System.Threading.Tasks;
2023-07-20 20:24:16 +03:00
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
namespace Kruzya.TelegramBot.Core.Options;
public class DbOptionProvider : IOptionProvider
{
private readonly CoreContext _db;
public DbOptionProvider(CoreContext db)
{
_db = db;
}
2023-07-21 17:14:11 +03:00
2023-07-20 20:24:16 +03:00
public async Task<TValue> GetValueAsync<TValue>(long chatId, string optionId, TValue defVal)
{
return (await _db.ChatOptionValues.FindOrCreateOption(chatId, optionId)).GetValue(defVal);
}
2023-07-21 17:14:11 +03:00
public async Task SetValueAsync<TValue>(long chatId, string optionId, TValue value)
2023-07-20 20:24:16 +03:00
{
2023-07-21 17:14:11 +03:00
var chatOption = await _db.ChatOptionValues.FindOrCreateOption(chatId, optionId);
2023-07-20 20:24:16 +03:00
chatOption.SetValue(value);
_db.AddOrUpdate(chatOption);
}
}