Fix user option values on .NET 5+

This commit is contained in:
2021-12-25 23:52:09 +04:00
parent 4807296992
commit 5f93d60277
6 changed files with 145 additions and 25 deletions
+19 -19
View File
@@ -1,7 +1,10 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Xml.Serialization;
namespace Kruzya.TelegramBot.Core.Data
{
@@ -13,30 +16,27 @@ namespace Kruzya.TelegramBot.Core.Data
public BotUser BotUser { get; set; }
public string Name { get; set; }
public byte[] Value
{
get
{
var fmt = new BinaryFormatter();
using var memStream = new MemoryStream();
fmt.Serialize(memStream, data);
public string ValueType { get; set; }
return memStream.ToArray();
public byte[] Value { get; set; }
public T GetValue<T>(T defVal = default)
{
try
{
return JsonSerializer.Deserialize<T>(new ReadOnlySpan<byte>(Value));
}
set
catch (Exception)
{
var fmt = new BinaryFormatter();
using var memStream = new MemoryStream();
memStream.Write(value);
memStream.Position = 0;
data = fmt.Deserialize(memStream);
return defVal;
}
}
[NonSerialized] public object data;
public T GetValue<T>() => (T) data;
public void SetValue<T>(T value) => data = value;
public void SetValue<T>(T value)
{
Value = JsonSerializer.SerializeToUtf8Bytes(value, new JsonSerializerOptions { WriteIndented = false,
IgnoreNullValues = true });
ValueType = value.GetType().FullName;
}
}
}
+9 -2
View File
@@ -30,8 +30,14 @@ namespace Kruzya.TelegramBot.Core.Extensions
/// </summary>
/// <param name="dbContext"></param>
/// <param name="entity">Entity for marking as modified.</param>
public static void MarkAsModified(this DbContext dbContext, object entity) =>
dbContext.Entry(entity).State = EntityState.Modified;
public static void MarkAsModified(this DbContext dbContext, object entity)
{
var entry = dbContext.Entry(entity);
if (entry.State == EntityState.Added)
return;
entry.State = EntityState.Modified;
}
#endregion
@@ -97,6 +103,7 @@ namespace Kruzya.TelegramBot.Core.Extensions
if (opt == null)
{
opt = repository.Create();
opt.Name = option;
opt.BotUser = await repository.GetService<CoreContext>().Users.FindOrCreate(chatId, userId);
}
+1
View File
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
+73
View File
@@ -0,0 +1,73 @@
// <auto-generated />
using System;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kruzya.TelegramBot.Core.Migrations
{
[DbContext(typeof(CoreContext))]
[Migration("20211225192324_AddValueType")]
partial class AddValueType
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUser", b =>
{
b.Property<Guid>("BotUserId")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("ChatId")
.HasColumnType("bigint");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("BotUserId");
b.ToTable("Users");
});
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b =>
{
b.Property<Guid>("BotUserValueId")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<Guid?>("BotUserId")
.HasColumnType("char(36)");
b.Property<string>("Name")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<byte[]>("Value")
.HasColumnType("longblob");
b.Property<string>("ValueType")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("BotUserValueId");
b.HasIndex("BotUserId");
b.ToTable("UserValues");
});
modelBuilder.Entity("Kruzya.TelegramBot.Core.Data.BotUserValue", b =>
{
b.HasOne("Kruzya.TelegramBot.Core.Data.BotUser", "BotUser")
.WithMany()
.HasForeignKey("BotUserId");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace Kruzya.TelegramBot.Core.Migrations
{
public partial class AddValueType : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ValueType",
table: "UserValues",
nullable: true);
migrationBuilder.AlterColumn<long>(
name: "UserId",
table: "Users",
nullable: false,
oldClrType: typeof(int),
oldType: "int");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ValueType",
table: "UserValues");
migrationBuilder.AlterColumn<int>(
name: "UserId",
table: "Users",
type: "int",
nullable: false,
oldClrType: typeof(long));
}
}
}
+5 -2
View File
@@ -26,8 +26,8 @@ namespace Kruzya.TelegramBot.Core.Migrations
b.Property<long>("ChatId")
.HasColumnType("bigint");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<long>("UserId")
.HasColumnType("bigint");
b.HasKey("BotUserId");
@@ -49,6 +49,9 @@ namespace Kruzya.TelegramBot.Core.Migrations
b.Property<byte[]>("Value")
.HasColumnType("longblob");
b.Property<string>("ValueType")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("BotUserValueId");
b.HasIndex("BotUserId");