add: 为前后端添加exam数据库管理
This commit is contained in:
231
server/src/Controllers/ExamController.cs
Normal file
231
server/src/Controllers/ExamController.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using DotNext;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 实验控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ExamController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 实验信息类
|
||||
/// </summary>
|
||||
public class ExamInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 实验的唯一标识符
|
||||
/// </summary>
|
||||
public required string ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验文档内容(Markdown格式)
|
||||
/// </summary>
|
||||
public required string DocContent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验创建时间
|
||||
/// </summary>
|
||||
public DateTime CreatedTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验最后更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdatedTime { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实验简要信息类(用于列表显示)
|
||||
/// </summary>
|
||||
public class ExamSummary
|
||||
{
|
||||
/// <summary>
|
||||
/// 实验的唯一标识符
|
||||
/// </summary>
|
||||
public required string ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验创建时间
|
||||
/// </summary>
|
||||
public DateTime CreatedTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验最后更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdatedTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实验标题(从文档内容中提取)
|
||||
/// </summary>
|
||||
public string Title { get; set; } = "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描结果类
|
||||
/// </summary>
|
||||
public class ScanResult
|
||||
{
|
||||
/// <summary>
|
||||
/// 结果消息
|
||||
/// </summary>
|
||||
public required string Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新的实验数量
|
||||
/// </summary>
|
||||
public int UpdateCount { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有实验列表
|
||||
/// </summary>
|
||||
/// <returns>实验列表</returns>
|
||||
[Authorize]
|
||||
[HttpGet("list")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ExamSummary[]), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetExamList()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var exams = db.GetAllExams();
|
||||
|
||||
var examSummaries = exams.Select(exam => new ExamSummary
|
||||
{
|
||||
ID = exam.ID,
|
||||
CreatedTime = exam.CreatedTime,
|
||||
UpdatedTime = exam.UpdatedTime,
|
||||
Title = ExtractTitleFromMarkdown(exam.DocContent)
|
||||
}).ToArray();
|
||||
|
||||
logger.Info($"成功获取实验列表,共 {examSummaries.Length} 个实验");
|
||||
return Ok(examSummaries);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取实验列表时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据实验ID获取实验详细信息
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID</param>
|
||||
/// <returns>实验详细信息</returns>
|
||||
[Authorize]
|
||||
[HttpGet("{examId}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ExamInfo), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetExam(string examId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(examId))
|
||||
return BadRequest("实验ID不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.GetExamByID(examId);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取实验时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
if (!result.Value.HasValue)
|
||||
{
|
||||
logger.Warn($"实验不存在: {examId}");
|
||||
return NotFound($"实验 {examId} 不存在");
|
||||
}
|
||||
|
||||
var exam = result.Value.Value;
|
||||
var examInfo = new ExamInfo
|
||||
{
|
||||
ID = exam.ID,
|
||||
DocContent = exam.DocContent,
|
||||
CreatedTime = exam.CreatedTime,
|
||||
UpdatedTime = exam.UpdatedTime
|
||||
};
|
||||
|
||||
logger.Info($"成功获取实验信息: {examId}");
|
||||
return Ok(examInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取实验 {examId} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新扫描实验文件夹并更新数据库
|
||||
/// </summary>
|
||||
/// <returns>更新结果</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("scan")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ScanResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult ScanExams()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var examFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "exam");
|
||||
var updateCount = db.ScanAndUpdateExams(examFolderPath);
|
||||
|
||||
var result = new ScanResult
|
||||
{
|
||||
Message = $"扫描完成,更新了 {updateCount} 个实验",
|
||||
UpdateCount = updateCount
|
||||
};
|
||||
|
||||
logger.Info($"手动扫描实验完成,更新了 {updateCount} 个实验");
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"扫描实验时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"扫描实验失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 Markdown 内容中提取标题
|
||||
/// </summary>
|
||||
/// <param name="markdownContent">Markdown 内容</param>
|
||||
/// <returns>提取的标题</returns>
|
||||
private static string ExtractTitleFromMarkdown(string markdownContent)
|
||||
{
|
||||
if (string.IsNullOrEmpty(markdownContent))
|
||||
return "";
|
||||
|
||||
var lines = markdownContent.Split('\n');
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmedLine = line.Trim();
|
||||
if (trimmedLine.StartsWith("# "))
|
||||
{
|
||||
return trimmedLine.Substring(2).Trim();
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -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