style: 重新调整一下后端的结构,并通过csharpier格式化
This commit is contained in:
55
server/src/Controllers/TutorialController.cs
Normal file
55
server/src/Controllers/TutorialController.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 教程 API
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class TutorialController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
|
||||
public TutorialController(IWebHostEnvironment environment)
|
||||
{
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有可用的教程目录
|
||||
/// </summary>
|
||||
/// <returns>教程目录列表</returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetTutorials()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取文档目录
|
||||
string docPath = Path.Combine(_environment.WebRootPath, "doc");
|
||||
|
||||
if (!Directory.Exists(docPath))
|
||||
{
|
||||
return Ok(new { tutorials = new List<string>() });
|
||||
}
|
||||
|
||||
// 获取所有子目录
|
||||
var directories = Directory.GetDirectories(docPath)
|
||||
.Select(Path.GetFileName)
|
||||
.Where(dir => !string.IsNullOrEmpty(dir))
|
||||
.ToList();
|
||||
|
||||
return Ok(new { tutorials = directories });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取教程目录失败");
|
||||
return StatusCode(500, new { error = "无法读取教程目录" });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user