mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
initial chat options implementation
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Options;
|
||||
|
||||
public abstract class AbstractOption<TOption, TValue> : IOption<TValue>
|
||||
where TOption: AbstractOption<TOption, TValue>
|
||||
{
|
||||
private readonly IOptionProvider _optionProvider;
|
||||
|
||||
protected AbstractOption(IOptionProvider optionProvider)
|
||||
{
|
||||
_optionProvider = optionProvider;
|
||||
}
|
||||
|
||||
public async Task<TValue> GetValueAsync(long chatId)
|
||||
{
|
||||
return await _optionProvider.GetValueAsync<TOption, TValue>(chatId);
|
||||
}
|
||||
|
||||
public async Task SetValueAsync(long chatId, TValue value)
|
||||
{
|
||||
await _optionProvider.SetValueAsync<TOption, TValue>(chatId, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<TValue> GetValueAsync<TOption, TValue>(long chatId)
|
||||
{
|
||||
return await GetValueAsync(
|
||||
chatId,
|
||||
GetOptionId<TOption>(),
|
||||
GetDefaultValue<TOption, TValue>()
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<TValue> GetValueAsync<TValue>(long chatId, string optionId, TValue defVal)
|
||||
{
|
||||
return (await _db.ChatOptionValues.FindOrCreateOption(chatId, optionId)).GetValue(defVal);
|
||||
}
|
||||
|
||||
public async Task SetValueAsync<TOption, TValue>(long chatId, TValue value)
|
||||
{
|
||||
var chatOption = await _db.ChatOptionValues.FindOrCreateOption(chatId, GetOptionId<TOption>());
|
||||
chatOption.SetValue(value);
|
||||
_db.AddOrUpdate(chatOption);
|
||||
}
|
||||
|
||||
protected OptionAttribute GetOptionAttribute<TOption>()
|
||||
{
|
||||
return typeof(TOption).GetCustomAttribute<OptionAttribute>();
|
||||
}
|
||||
|
||||
protected string GetOptionId<TOption>()
|
||||
{
|
||||
var optionId = GetOptionAttribute<TOption>()?.OptionId;
|
||||
if (optionId == null)
|
||||
{
|
||||
throw new ArgumentException("Option must implement OptionAttribute");
|
||||
}
|
||||
|
||||
return optionId;
|
||||
}
|
||||
|
||||
protected TValue GetDefaultValue<TOption, TValue>()
|
||||
{
|
||||
return (TValue) GetOptionAttribute<TOption>().DefaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Options;
|
||||
|
||||
public interface IOption<TValue>
|
||||
{
|
||||
public Task<TValue> GetValueAsync(long chatId);
|
||||
public Task SetValueAsync(long chatId, TValue value);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Options;
|
||||
|
||||
public interface IOptionProvider
|
||||
{
|
||||
public Task<TValue> GetValueAsync<TOption, TValue>(long chatId);
|
||||
public Task SetValueAsync<TOption, TValue>(long chatId, TValue value);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Options;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class OptionAttribute : Attribute
|
||||
{
|
||||
public string OptionId { get; }
|
||||
public object DefaultValue { get; }
|
||||
|
||||
public OptionAttribute(string optionId, object defaultValue = default)
|
||||
{
|
||||
OptionId = optionId;
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user