finish basic database
This commit is contained in:
113
server/src/Database.cs
Normal file
113
server/src/Database.cs
Normal file
@@ -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>();
|
||||
}
|
||||
Reference in New Issue
Block a user