refactor(service): 重构服务接口和实现类结构

将服务接口从service包移动到service.imp包
修复ArticleRepository中viewCount的COALESCE处理
添加getPublishedArticles方法获取已发布文章
优化incrementViewCount方法使用仓库直接更新
修正HelpController中README_API.md路径
This commit is contained in:
qingfeng1121
2025-10-26 20:18:35 +08:00
parent 46be613f28
commit 9132feb870
15 changed files with 87 additions and 54 deletions

View File

@@ -30,31 +30,35 @@ public class HelpController {
@GetMapping
public ResponseMessage<String> getReadmeApi() {
try {
// 获取项目根目录
String rootPath = System.getProperty("user.dir");
// 构建README_API.md文件路径
File readmeFile = new File(rootPath, "README_API.md");
// 获取README_API.md文件的绝对路径
String readmePath = "e:\\MyWebProject\\MyAfterProjecy\\README_API.md";
File readmeFile = new File(readmePath);
// 检查文件是否存在
if (!readmeFile.exists() || !readmeFile.isFile()) {
// 如果不存在,尝试使用类路径资源加载
try {
ClassPathResource resource = new ClassPathResource("README_API.md");
String markdownContent = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()), StandardCharsets.UTF_8);
// 将Markdown转换为HTML
String htmlContent = convertMarkdownToHtml(markdownContent);
return ResponseMessage.success(htmlContent, "获取API文档成功");
} catch (IOException e) {
return ResponseMessage.error("未找到README_API.md文件");
}
if (readmeFile.exists() && readmeFile.isFile()) {
// 读取文件内容
String markdownContent = new String(FileCopyUtils.copyToByteArray(new FileInputStream(readmeFile)), StandardCharsets.UTF_8);
// 将Markdown转换为HTML
String htmlContent = convertMarkdownToHtml(markdownContent);
return ResponseMessage.success(htmlContent, "获取API文档成功");
}
// 读取文件内容
String markdownContent = new String(FileCopyUtils.copyToByteArray(new FileInputStream(readmeFile)), StandardCharsets.UTF_8);
// 将Markdown转换为HTML
String htmlContent = convertMarkdownToHtml(markdownContent);
return ResponseMessage.success(htmlContent, "获取API文档成功");
} catch (IOException e) {
// 如果直接路径不存在,尝试从类路径加载
try {
ClassPathResource resource = new ClassPathResource("README_API.md");
String markdownContent = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()), StandardCharsets.UTF_8);
// 将Markdown转换为HTML
String htmlContent = convertMarkdownToHtml(markdownContent);
return ResponseMessage.success(htmlContent, "获取API文档成功");
} catch (IOException e) {
// 记录详细错误信息以便调试
System.err.println("无法从类路径加载README_API.md: " + e.getMessage());
return ResponseMessage.error("未找到README_API.md文件");
}
} catch (Exception e) {
// 捕获所有异常并记录详细错误信息
System.err.println("处理README_API.md时出错: " + e.getMessage());
e.printStackTrace();
return ResponseMessage.error("读取README_API.md文件失败: " + e.getMessage());
}
}