feat: backend add auth method

This commit is contained in:
2025-07-10 19:39:00 +08:00
parent c6c3f1cc41
commit d6167ac286
5 changed files with 235 additions and 51 deletions

View File

@@ -1,3 +1,4 @@
using DotNext;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.Mapping;
@@ -20,6 +21,46 @@ public class User
/// </summary>
[NotNull]
public required string Name { get; set; }
/// <summary>
/// 用户的电子邮箱
/// </summary>
[NotNull]
public required string EMail { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[NotNull]
public required string Password { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[NotNull]
public required UserPermission Permission { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[Nullable]
public Guid BoardID { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
public enum UserPermission
{
/// <summary>
/// [TODO:description]
/// </summary>
Admin,
/// <summary>
/// [TODO:description]
/// </summary>
Normal,
}
}
/// <summary>
@@ -31,13 +72,47 @@ public class Board
/// FPGA 板子的唯一标识符
/// </summary>
[PrimaryKey]
public Guid Id { get; set; } = Guid.NewGuid();
public Guid ID { get; set; } = Guid.NewGuid();
/// <summary>
/// FPGA 板子的名称
/// </summary>
[NotNull]
public required string BoardName { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[NotNull]
public required string IpAddr { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[NotNull]
public required int Port { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
[NotNull]
public required BoardStatus Status { get; set; }
/// <summary>
/// [TODO:description]
/// </summary>
public enum BoardStatus
{
/// <summary>
/// [TODO:description]
/// </summary>
Busy,
/// <summary>
/// [TODO:description]
/// </summary>
Available,
}
}
/// <summary>
@@ -45,6 +120,8 @@ public class Board
/// </summary>
public class AppDataConnection : DataConnection
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static readonly LinqToDB.DataOptions options =
new LinqToDB.DataOptions()
.UseSQLite($"Data Source={Environment.CurrentDirectory}/Database.sqlite");
@@ -77,30 +154,87 @@ public class AppDataConnection : DataConnection
/// 添加一个新的用户到数据库
/// </summary>
/// <param name="name">用户的名称</param>
/// <param name="email">[TODO:parameter]</param>
/// <param name="password">[TODO:parameter]</param>
/// <returns>插入的记录数</returns>
public int AddUser(string name)
public int AddUser(string name, string email, string password)
{
var user = new User()
{
Name = name
Name = name,
EMail = email,
Password = password,
Permission = Database.User.UserPermission.Normal,
};
return this.Insert(user);
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <param name="name">[TODO:parameter]</param>
/// <param name="password">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public Result<Optional<User>> CheckUserPassword(string name, string password)
{
var user = this.User.Where((user) => user.Name == name).ToArray();
if (user.Length > 1)
{
logger.Error($"TODO");
return new(new Exception($""));
}
if (user[0].Password == password) return new(user[0]);
else return new(Optional.Null<User>());
}
/// <summary>
/// 添加一块新的 FPGA 板子到数据库
/// </summary>
/// <param name="name">FPGA 板子的名称</param>
/// <param name="ipAddr">[TODO:Param]</param>
/// <param name="port">[TODO:Param]</param>
/// <returns>插入的记录数</returns>
public int AddBoard(string name)
public int AddBoard(string name, string ipAddr, int port)
{
var board = new Board()
{
BoardName = name
BoardName = name,
IpAddr = ipAddr,
Port = port,
Status = Database.Board.BoardStatus.Available,
};
return this.Insert(board);
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <returns>[TODO:return]</returns>
public Optional<Board> GetAvailableBoard()
{
var boards = this.Board.Where(
(board) => board.Status == Database.Board.BoardStatus.Available
).ToArray();
if (boards.Length < 0)
{
logger.Warn($"TODO");
return new(null);
}
else
{
var board = boards[0];
board.Status = Database.Board.BoardStatus.Busy;
this.Board
.Where(target => target.ID == board.ID)
.Set(target => target.Status, board.Status)
.Update();
return new(board);
}
}
/// <summary>
/// 用户表
/// </summary>