|
@@ -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
|
|
* GET /api/book-generator/albums/:id/chapters
|
|
|
* 获取专辑章节列表
|
|
* 获取专辑章节列表
|