add: 为前后端添加exam数据库管理
This commit is contained in:
@@ -150,6 +150,36 @@ public class Board
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实验类,表示实验信息
|
||||
/// </summary>
|
||||
public class Exam
|
||||
{
|
||||
/// <summary>
|
||||
/// 实验的唯一标识符
|
||||
/// </summary>
|
||||
[PrimaryKey]
|
||||
public required string ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验文档内容(Markdown格式)
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string DocContent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验创建时间
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public DateTime CreatedTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 实验最后更新时间
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public DateTime UpdatedTime { get; set; } = DateTime.Now;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序数据连接类,用于与数据库交互
|
||||
/// </summary>
|
||||
@@ -197,6 +227,7 @@ public class AppDataConnection : DataConnection
|
||||
logger.Info("正在创建数据库表...");
|
||||
this.CreateTable<User>();
|
||||
this.CreateTable<Board>();
|
||||
this.CreateTable<Exam>();
|
||||
logger.Info("数据库表创建完成");
|
||||
}
|
||||
|
||||
@@ -208,6 +239,7 @@ public class AppDataConnection : DataConnection
|
||||
logger.Warn("正在删除所有数据库表...");
|
||||
this.DropTable<User>();
|
||||
this.DropTable<Board>();
|
||||
this.DropTable<Exam>();
|
||||
logger.Warn("所有数据库表已删除");
|
||||
}
|
||||
|
||||
@@ -635,4 +667,117 @@ public class AppDataConnection : DataConnection
|
||||
/// FPGA 板子表
|
||||
/// </summary>
|
||||
public ITable<Board> BoardTable => this.GetTable<Board>();
|
||||
|
||||
/// <summary>
|
||||
/// 实验表
|
||||
/// </summary>
|
||||
public ITable<Exam> ExamTable => this.GetTable<Exam>();
|
||||
|
||||
/// <summary>
|
||||
/// 扫描 exam 文件夹并更新实验数据库
|
||||
/// </summary>
|
||||
/// <param name="examFolderPath">exam 文件夹的路径</param>
|
||||
/// <returns>更新的实验数量</returns>
|
||||
public int ScanAndUpdateExams(string examFolderPath)
|
||||
{
|
||||
if (!Directory.Exists(examFolderPath))
|
||||
{
|
||||
logger.Warn($"实验文件夹不存在: {examFolderPath}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int updateCount = 0;
|
||||
var subdirectories = Directory.GetDirectories(examFolderPath);
|
||||
|
||||
foreach (var examDir in subdirectories)
|
||||
{
|
||||
var examId = Path.GetFileName(examDir);
|
||||
var docPath = Path.Combine(examDir, "doc.md");
|
||||
|
||||
if (!File.Exists(docPath))
|
||||
{
|
||||
logger.Warn($"实验 {examId} 缺少 doc.md 文件");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var docContent = File.ReadAllText(docPath);
|
||||
var existingExam = this.ExamTable.Where(e => e.ID == examId).FirstOrDefault();
|
||||
|
||||
if (existingExam == null)
|
||||
{
|
||||
// 创建新实验
|
||||
var newExam = new Exam
|
||||
{
|
||||
ID = examId,
|
||||
DocContent = docContent,
|
||||
CreatedTime = DateTime.Now,
|
||||
UpdatedTime = DateTime.Now
|
||||
};
|
||||
this.Insert(newExam);
|
||||
logger.Info($"新实验已添加: {examId}");
|
||||
updateCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 更新现有实验
|
||||
var fileLastWrite = File.GetLastWriteTime(docPath);
|
||||
if (fileLastWrite > existingExam.UpdatedTime)
|
||||
{
|
||||
this.ExamTable
|
||||
.Where(e => e.ID == examId)
|
||||
.Set(e => e.DocContent, docContent)
|
||||
.Set(e => e.UpdatedTime, DateTime.Now)
|
||||
.Update();
|
||||
logger.Info($"实验已更新: {examId}");
|
||||
updateCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"处理实验 {examId} 时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info($"实验扫描完成,共更新 {updateCount} 个实验");
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有实验信息
|
||||
/// </summary>
|
||||
/// <returns>所有实验的数组</returns>
|
||||
public Exam[] GetAllExams()
|
||||
{
|
||||
var exams = this.ExamTable.OrderBy(e => e.ID).ToArray();
|
||||
logger.Debug($"获取所有实验,共 {exams.Length} 个");
|
||||
return exams;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据实验ID获取实验信息
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID</param>
|
||||
/// <returns>包含实验信息的结果,如果未找到则返回空</returns>
|
||||
public Result<Optional<Exam>> GetExamByID(string examId)
|
||||
{
|
||||
var exams = this.ExamTable.Where(exam => exam.ID == examId).ToArray();
|
||||
|
||||
if (exams.Length > 1)
|
||||
{
|
||||
logger.Error($"数据库中存在多个相同ID的实验: {examId}");
|
||||
return new(new Exception($"数据库中存在多个相同ID的实验: {examId}"));
|
||||
}
|
||||
|
||||
if (exams.Length == 0)
|
||||
{
|
||||
logger.Info($"未找到ID对应的实验: {examId}");
|
||||
return new(Optional<Exam>.None);
|
||||
}
|
||||
|
||||
logger.Debug($"成功获取实验信息: {examId}");
|
||||
return new(exams[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user