WiP storage for modules

This commit is contained in:
2020-03-08 01:55:50 +04:00
parent 768a0d064f
commit 3fd5042367
11 changed files with 375 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Kruzya.TelegramBot.Core.Data
{
public class BotUser
{
/// <summary>
/// The unique bot user identifier.
/// </summary>
[Key]
public Guid BotUserId { get; set; }
/// <summary>
/// The chat identifier where this user is related.
/// </summary>
public long ChatId { get; set; }
/// <summary>
/// The Telegram User identifier.
/// </summary>
public int UserId { get; set; }
}
}
+42
View File
@@ -0,0 +1,42 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Kruzya.TelegramBot.Core.Data
{
public class BotUserValue
{
[Key]
public Guid BotUserValueId { get; set; }
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);
return memStream.ToArray();
}
set
{
var fmt = new BinaryFormatter();
using var memStream = new MemoryStream();
memStream.Write(value);
memStream.Position = 0;
data = fmt.Deserialize(memStream);
}
}
[NonSerialized] public object data;
public T GetValue<T>() => (T) data;
public void SetValue<T>(T value) => data = value;
}
}
+19
View File
@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
namespace Kruzya.TelegramBot.Core.Data
{
public class CoreContext : DbContext
{
/// <summary>
/// The all known users.
/// </summary>
public DbSet<BotUser> Users { get; set; }
public DbSet<BotUserValue> UserValues { get; set; }
public CoreContext(DbContextOptions<CoreContext> ctx) : base(ctx)
{
Database.Migrate();
}
}
}