1
This commit is contained in:
101
Data/OneClickRegister.cs
Normal file
101
Data/OneClickRegister.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text.RegularExpressions;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using IDIS.Models;
|
||||
using IDIS.Services;
|
||||
using IDIS.Services.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace IDIS.Client.Web.Data;
|
||||
|
||||
public record OneClickRegisterData
|
||||
{
|
||||
public string Name;
|
||||
public string Prefix;
|
||||
public Dictionary<string,string> Dictionary;
|
||||
}
|
||||
|
||||
public class OneClickRegister:DbContext
|
||||
{
|
||||
[Keyless]
|
||||
public class Info
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Json { get; set; }
|
||||
}
|
||||
[Keyless]
|
||||
public class SavedInfo:Info
|
||||
{
|
||||
|
||||
}
|
||||
private readonly IDIS_Service _service;
|
||||
|
||||
public OneClickRegister(IDIS_Service service)
|
||||
{
|
||||
_service = service;
|
||||
}
|
||||
public DbSet<Info> oneClickTemplates { get; set; }
|
||||
public DbSet<SavedInfo> oneClickRegisters { get; set; }
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var connectionString = BITKit.Data.Get<string>("DefaultConnection");
|
||||
|
||||
optionsBuilder.UseMySQL(connectionString);
|
||||
}
|
||||
public async UniTask<Dictionary<string,OneClickRegisterData>> GetAllTemplate()
|
||||
{
|
||||
var array = await oneClickTemplates.ToArrayAsync();
|
||||
return array.ToDictionary(info => info.Name, info => JsonConvert.DeserializeObject<OneClickRegisterData>(info.Json)!);
|
||||
}
|
||||
public async UniTask CreateTemplate(string name,string prefix,Dictionary<string,string> dictionary)
|
||||
{
|
||||
var _all =await GetAllTemplate();
|
||||
if(_all.ContainsKey(name))
|
||||
throw new ArgumentException("模板已存在");
|
||||
oneClickTemplates.Add(new Info{Name = name,Json = JsonConvert.SerializeObject(new OneClickRegisterData{Name = name,Prefix = prefix,Dictionary = dictionary})});
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
public async UniTask DeleteTemplate(string name)
|
||||
{
|
||||
oneClickTemplates.RemoveRange(oneClickTemplates.Where(x => x.Name == name));
|
||||
await SaveChangesAsync();
|
||||
}
|
||||
public async UniTask<OneClickRegisterData> Register(string templateName,string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
name = DateTime.Now.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
var _all =await GetAllTemplate();
|
||||
if(_all.TryGetValue(templateName,out var template) is false)
|
||||
throw new ArgumentException($"未找到模板:{templateName}");
|
||||
var tick = DateTime.Now.Ticks;
|
||||
var export = new OneClickRegisterData
|
||||
{
|
||||
Name = name,
|
||||
Prefix = template.Prefix,
|
||||
Dictionary = new(),
|
||||
};
|
||||
foreach (var (entryName,templateVersion) in template.Dictionary)
|
||||
{
|
||||
var handle = $"{template.Prefix}/{tick++}";
|
||||
await _service.RegisterAsync(handle,templateVersion,Array.Empty<IDIS_Register_Data.ValueInfo>());
|
||||
export.Dictionary.Add(entryName,handle);
|
||||
}
|
||||
|
||||
await oneClickRegisters.AddAsync(new SavedInfo{Name = name,Json = JsonConvert.SerializeObject(export)});
|
||||
await SaveChangesAsync();
|
||||
|
||||
return export;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user