فهرست منبع

fix: 补回书籍编辑所需的 GET /albums/:id 和 PUT /albums/:id 路由

这些路由之前在未提交的工作区,被 git checkout 还原时丢失。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
MyFramework User 3 ماه پیش
والد
کامیت
f3d064f60d
1فایلهای تغییر یافته به همراه90 افزوده شده و 0 حذف شده
  1. 90 0
      server/src/modules/book-generator/album-controller.ts

+ 90 - 0
server/src/modules/book-generator/album-controller.ts

@@ -142,6 +142,96 @@ router.post('/albums', async (ctx: Context) => {
   }
 });
 
+/**
+ * GET /api/book-generator/albums/:id
+ * 获取单个书籍详情(用于编辑页面回填)
+ */
+router.get('/albums/:id', optionalAuth, async (ctx: Context) => {
+  try {
+    const bookId = parseInt(ctx.params.id as string);
+    const book = await prisma.book.findUnique({
+      where: { id: bookId },
+      include: { chapters: { orderBy: { number: 'asc' } } },
+    });
+
+    if (!book) {
+      ctx.status = 404;
+      ctx.body = { code: 1, message: '书籍不存在' };
+      return;
+    }
+
+    ctx.body = {
+      code: 0,
+      message: 'success',
+      data: {
+        id: book.id,
+        title: book.title,
+        description: book.description,
+        totalChapters: book.totalChapters,
+        genStage: book.genStage,
+        progress: book.progress,
+        createdAt: book.createdAt,
+        updatedAt: book.updatedAt,
+        chapters: book.chapters.map(ch => ({
+          id: ch.id,
+          title: ch.title,
+          genStage: ch.genStage,
+          audioUrl: ch.audioUrl,
+          videoUrl: ch.videoUrl,
+        })),
+      },
+    };
+  } catch (error) {
+    console.error('查询书籍失败:', error);
+    ctx.status = 500;
+    ctx.body = { code: 1, message: error instanceof Error ? error.message : '查询失败' };
+  }
+});
+
+/**
+ * PUT /api/book-generator/albums/:id
+ * 更新书籍(书名、描述等)
+ */
+router.put('/albums/:id', optionalAuth, async (ctx: Context) => {
+  try {
+    const bookId = parseInt(ctx.params.id as string);
+    const body = ctx.request.body as {
+      title?: string;
+      description?: string;
+      bookScale?: string;
+      style?: string;
+    };
+
+    const existing = await prisma.book.findUnique({ where: { id: bookId } });
+    if (!existing) {
+      ctx.status = 404;
+      ctx.body = { code: 1, message: '书籍不存在' };
+      return;
+    }
+
+    const data: any = {};
+    if (body.title !== undefined) data.title = body.title;
+    if (body.description !== undefined) data.description = body.description;
+    if (body.bookScale !== undefined) data.bookScale = body.bookScale;
+    if (body.style !== undefined) data.style = body.style;
+
+    const updated = await prisma.book.update({
+      where: { id: bookId },
+      data,
+    });
+
+    ctx.body = {
+      code: 0,
+      message: '更新成功',
+      data: { id: updated.id, title: updated.title },
+    };
+  } catch (error) {
+    console.error('更新书籍失败:', error);
+    ctx.status = 500;
+    ctx.body = { code: 1, message: error instanceof Error ? error.message : '更新失败' };
+  }
+});
+
 /**
  * GET /api/book-generator/albums/:id/chapters
  * 获取专辑章节列表