diff --git a/flake.nix b/flake.nix index 542c239..b099c89 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,7 @@ sqlite sqls sql-studio + zlib # Backend (dotnetCorePackages.combinePackages [ dotnetCorePackages.sdk_9_0 @@ -37,7 +38,8 @@ typescript-language-server ]; shellHook = '' - export PATH=$PATH:$HOME/.bun/bin + export PATH=$PATH: + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.zlib}/lib ''; }; diff --git a/server/Program.cs b/server/Program.cs index a3f8d0f..0b3c10c 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -1,4 +1,3 @@ -using Honoo.IO.Hashing; using Microsoft.AspNetCore.Http.Features; using Newtonsoft.Json; using NLog; @@ -38,16 +37,17 @@ try }); // Add CORS policy - builder.Services.AddCors(options => + if (builder.Environment.IsDevelopment()) { - options.AddPolicy("Development", policy => + builder.Services.AddCors(options => { - policy + options.AddPolicy("Development", policy => policy .AllowAnyOrigin() .AllowAnyMethod() - .AllowAnyHeader(); + .AllowAnyHeader() + ); }); - }); + } // Add Swagger builder.Services.AddControllers(); @@ -105,12 +105,6 @@ try app.MapGet("/", () => Results.Redirect("/swagger")); app.MapControllers(); - // { - // var crc = Crc.Create(CrcName.CRC32_MPEG_2); - // var checkSum = crc.ComputeFinal(new byte[] { 0x1C, 0xDF, 0x44, 0x21 }); - // logger.Info($"CRC: 0x{checkSum.ToString().PadLeft(8, '0')}"); - // } - app.Run("http://localhost:5000"); } catch (Exception exception) diff --git a/server/server.csproj b/server/server.csproj index 2ef7bc9..a6dc9b5 100644 --- a/server/server.csproj +++ b/server/server.csproj @@ -14,12 +14,14 @@ + + diff --git a/server/src/Controllers.cs b/server/src/Controllers.cs index e7cfe00..a1d31cf 100644 --- a/server/src/Controllers.cs +++ b/server/src/Controllers.cs @@ -1,6 +1,7 @@ using System.Buffers.Binary; using System.Net; using Common; +using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using WebProtocol; @@ -478,6 +479,71 @@ public class RemoteUpdater : ControllerBase } } + +/// +/// 数据控制器 +/// +[ApiController] +[Route("api/[controller]")] +public class Data : ControllerBase +{ + private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + /// + /// 创建数据库表 + /// + /// 插入的记录数 + [EnableCors("Development")] + [HttpPost("CreateTable")] + public IResult CreateTables() + { + using var db = new Database.AppDataConnection(); + db.CreateAllTables(); + return TypedResults.Ok(); + } + + /// + /// 删除数据库表 + /// + /// 插入的记录数 + [EnableCors("Development")] + [HttpDelete("DropTables")] + public IResult DropTables() + { + using var db = new Database.AppDataConnection(); + db.DropAllTables(); + return TypedResults.Ok(); + } + + /// + /// 获取所有用户 + /// + /// 用户列表 + [HttpGet("AllUsers")] + public IResult AllUsers() + { + using var db = new Database.AppDataConnection(); + var ret = db.User.ToList(); + return TypedResults.Ok(ret); + } + + /// + /// 注册新用户 + /// + /// 用户名 + /// 操作结果 + [HttpPost("SignUpUser")] + public IResult SignUpUser(string name) + { + if (name.Length > 255) + return TypedResults.BadRequest("Name Couln't over 255 characters"); + + using var db = new Database.AppDataConnection(); + var ret = db.AddUser(name); + return TypedResults.Ok(ret); + } +} + /// /// 日志控制器 /// diff --git a/server/src/Database.cs b/server/src/Database.cs new file mode 100644 index 0000000..e54d831 --- /dev/null +++ b/server/src/Database.cs @@ -0,0 +1,113 @@ +using LinqToDB; +using LinqToDB.Data; +using LinqToDB.Mapping; + +namespace Database; + +/// +/// 用户类,表示用户信息 +/// +public class User +{ + /// + /// 用户的唯一标识符 + /// + [PrimaryKey] + public Guid ID { get; set; } = Guid.NewGuid(); + + /// + /// 用户的名称 + /// + [NotNull] + public required string Name { get; set; } +} + +/// +/// FPGA 板子类,表示板子信息 +/// +public class Board +{ + /// + /// FPGA 板子的唯一标识符 + /// + [PrimaryKey] + public Guid Id { get; set; } = Guid.NewGuid(); + + /// + /// FPGA 板子的名称 + /// + [NotNull] + public required string BoardName { get; set; } +} + +/// +/// 应用程序数据连接类,用于与数据库交互 +/// +public class AppDataConnection : DataConnection +{ + static readonly LinqToDB.DataOptions options = + new LinqToDB.DataOptions() + .UseSQLite($"Data Source={Environment.CurrentDirectory}/Database.sqlite"); + + /// + /// 初始化应用程序数据连接 + /// + public AppDataConnection() : base(options) { } + + + /// + /// 创建所有数据库表 + /// + public void CreateAllTables() + { + this.CreateTable(); + this.CreateTable(); + } + + /// + /// 删除所有数据库表 + /// + public void DropAllTables() + { + this.DropTable(); + this.DropTable(); + } + + /// + /// 添加一个新的用户到数据库 + /// + /// 用户的名称 + /// 插入的记录数 + public int AddUser(string name) + { + var user = new User() + { + Name = name + }; + return this.Insert(user); + } + + /// + /// 添加一块新的 FPGA 板子到数据库 + /// + /// FPGA 板子的名称 + /// 插入的记录数 + public int AddBoard(string name) + { + var board = new Board() + { + BoardName = name + }; + return this.Insert(board); + } + + /// + /// 用户表 + /// + public ITable User => this.GetTable(); + + /// + /// FPGA 板子表 + /// + public ITable Board => this.GetTable(); +}