2020-03-08 01:55:50 +04:00
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2022-01-23 01:49:58 +03:00
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2021-12-25 23:52:09 +04:00
|
|
|
using System.Text.Json;
|
2022-01-22 17:13:38 +03:00
|
|
|
using System.Text.Json.Serialization;
|
2020-03-08 01:55:50 +04:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
namespace Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
|
|
|
|
|
public class BotUserValue
|
2020-03-08 01:55:50 +04:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
|
|
|
[Key]
|
|
|
|
|
public Guid BotUserValueId { get; set; }
|
2020-03-08 01:55:50 +04:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
public BotUser BotUser { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
2020-03-08 01:55:50 +04:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
public string ValueType { get; set; }
|
2021-12-25 23:52:09 +04:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
public byte[] Value { get; set; }
|
2021-12-25 23:52:09 +04:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
public T GetValue<T>(T defVal = default)
|
|
|
|
|
{
|
|
|
|
|
try
|
2020-03-08 01:55:50 +04:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(Value));
|
2020-03-08 01:55:50 +04:00
|
|
|
}
|
2023-07-29 15:14:52 +03:00
|
|
|
catch (Exception)
|
2021-12-25 23:52:09 +04:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
return defVal;
|
2021-12-25 23:52:09 +04:00
|
|
|
}
|
2020-03-08 01:55:50 +04:00
|
|
|
}
|
2023-07-29 15:14:52 +03:00
|
|
|
|
|
|
|
|
public void SetValue<T>(T value)
|
|
|
|
|
{
|
|
|
|
|
Value = JsonSerializer.SerializeToUtf8Bytes(value, new JsonSerializerOptions { WriteIndented = false,
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
|
|
|
|
|
ValueType = value.GetType().FullName;
|
|
|
|
|
}
|
2020-03-08 01:55:50 +04:00
|
|
|
}
|