finish basic database
This commit is contained in:
parent
210f6aa61c
commit
e6d177cf15
|
@ -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
|
||||
'';
|
||||
|
||||
};
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -14,12 +14,14 @@
|
|||
<PackageReference Include="DotNext" Version="5.19.1" />
|
||||
<PackageReference Include="DotNext.Threading" Version="5.19.1" />
|
||||
<PackageReference Include="Honoo.IO.Hashing.Crc" Version="1.3.3" />
|
||||
<PackageReference Include="linq2db.AspNet" Version="5.4.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.OpenApi" Version="1.6.23" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="NLog" Version="5.4.0" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
|
||||
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 数据控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class Data : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 创建数据库表
|
||||
/// </summary>
|
||||
/// <returns>插入的记录数</returns>
|
||||
[EnableCors("Development")]
|
||||
[HttpPost("CreateTable")]
|
||||
public IResult CreateTables()
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
db.CreateAllTables();
|
||||
return TypedResults.Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据库表
|
||||
/// </summary>
|
||||
/// <returns>插入的记录数</returns>
|
||||
[EnableCors("Development")]
|
||||
[HttpDelete("DropTables")]
|
||||
public IResult DropTables()
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
db.DropAllTables();
|
||||
return TypedResults.Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有用户
|
||||
/// </summary>
|
||||
/// <returns>用户列表</returns>
|
||||
[HttpGet("AllUsers")]
|
||||
public IResult AllUsers()
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var ret = db.User.ToList();
|
||||
return TypedResults.Ok(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册新用户
|
||||
/// </summary>
|
||||
/// <param name="name">用户名</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志控制器
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
using LinqToDB;
|
||||
using LinqToDB.Data;
|
||||
using LinqToDB.Mapping;
|
||||
|
||||
namespace Database;
|
||||
|
||||
/// <summary>
|
||||
/// 用户类,表示用户信息
|
||||
/// </summary>
|
||||
public class User
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户的唯一标识符
|
||||
/// </summary>
|
||||
[PrimaryKey]
|
||||
public Guid ID { get; set; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// 用户的名称
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string Name { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FPGA 板子类,表示板子信息
|
||||
/// </summary>
|
||||
public class Board
|
||||
{
|
||||
/// <summary>
|
||||
/// FPGA 板子的唯一标识符
|
||||
/// </summary>
|
||||
[PrimaryKey]
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// FPGA 板子的名称
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string BoardName { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序数据连接类,用于与数据库交互
|
||||
/// </summary>
|
||||
public class AppDataConnection : DataConnection
|
||||
{
|
||||
static readonly LinqToDB.DataOptions options =
|
||||
new LinqToDB.DataOptions()
|
||||
.UseSQLite($"Data Source={Environment.CurrentDirectory}/Database.sqlite");
|
||||
|
||||
/// <summary>
|
||||
/// 初始化应用程序数据连接
|
||||
/// </summary>
|
||||
public AppDataConnection() : base(options) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 创建所有数据库表
|
||||
/// </summary>
|
||||
public void CreateAllTables()
|
||||
{
|
||||
this.CreateTable<User>();
|
||||
this.CreateTable<Board>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有数据库表
|
||||
/// </summary>
|
||||
public void DropAllTables()
|
||||
{
|
||||
this.DropTable<User>();
|
||||
this.DropTable<Board>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个新的用户到数据库
|
||||
/// </summary>
|
||||
/// <param name="name">用户的名称</param>
|
||||
/// <returns>插入的记录数</returns>
|
||||
public int AddUser(string name)
|
||||
{
|
||||
var user = new User()
|
||||
{
|
||||
Name = name
|
||||
};
|
||||
return this.Insert(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一块新的 FPGA 板子到数据库
|
||||
/// </summary>
|
||||
/// <param name="name">FPGA 板子的名称</param>
|
||||
/// <returns>插入的记录数</returns>
|
||||
public int AddBoard(string name)
|
||||
{
|
||||
var board = new Board()
|
||||
{
|
||||
BoardName = name
|
||||
};
|
||||
return this.Insert(board);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户表
|
||||
/// </summary>
|
||||
public ITable<User> User => this.GetTable<User>();
|
||||
|
||||
/// <summary>
|
||||
/// FPGA 板子表
|
||||
/// </summary>
|
||||
public ITable<Board> Board => this.GetTable<Board>();
|
||||
}
|
Loading…
Reference in New Issue