Эх сурвалжийг харах

fix: 补全 /albums 路由别名 — GET列表 + POST创建

前端统一用 /albums 但后端只有 /books,逐条漏掉导致多处404。
将 getAlbums/createAlbum 提取为命名 handler 并注册到两条路径。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
MyFramework User 3 сар өмнө
parent
commit
cd71814fe8

+ 15 - 10
server/src/modules/book-generator/album-controller.ts

@@ -15,13 +15,11 @@ const TEST_USER_ID = '1';
 const router = new Router();
 
 /**
- * GET /api/book-generator/books
- * 获取专辑列表
+ * 获取专辑列表 handler
  */
-router.get('/books', async (ctx: Context) => {
+async function getAlbums(ctx: Context) {
   try {
     const books = await bookStore.getAllByUser();
-    // 只返回基本信息
     const albums = books.map(book => ({
       id: book.id,
       title: book.title,
@@ -37,7 +35,11 @@ router.get('/books', async (ctx: Context) => {
     ctx.status = 500;
     ctx.body = { code: 1, message: error instanceof Error ? error.message : '查询失败' };
   }
-});
+}
+
+// GET /api/book-generator/books 和 /albums
+router.get('/books', getAlbums);
+router.get('/albums', getAlbums);
 
 /**
  * GET /api/book-generator/list
@@ -104,10 +106,9 @@ router.get('/list', optionalAuth, async (ctx: Context) => {
 });
 
 /**
- * POST /api/book-generator/books
- * 创建专辑
+ * 创建专辑 handler
  */
-router.post('/books', async (ctx: Context) => {
+async function createAlbum(ctx: Context) {
   try {
     const body = ctx.request.body as {
       title: string;
@@ -123,7 +124,7 @@ router.post('/books', async (ctx: Context) => {
     const book = await bookStore.create({
       title: body.title.trim(),
       description: body.description || '',
-      totalChapters: 0, // 初始为 0,等待添加章节
+      totalChapters: 0,
     });
 
     ctx.body = {
@@ -140,7 +141,11 @@ router.post('/books', async (ctx: Context) => {
     ctx.status = 500;
     ctx.body = { code: 1, message: error instanceof Error ? error.message : '创建失败' };
   }
-});
+}
+
+// POST /api/book-generator/books 和 /albums
+router.post('/books', createAlbum);
+router.post('/albums', createAlbum);
 
 /**
  * GET /api/book-generator/books/:id