All files / modules/book-generator album-management.controller.ts

0% Statements 0/57
0% Branches 0/1
0% Functions 0/1
0% Lines 0/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
import Router from '@koa/router';
import { Context } from 'koa';
import { optionalAuth } from '../../middleware/auth';
import { prisma } from '../../models';
 
const router = new Router();
 
// 重命名专辑
router.put('/:id/rename', optionalAuth, async (ctx: Context) => {
  const { id } = ctx.params;
  const { title } = ctx.request.body as { title: string };
 
  const book = await prisma.book.update({
    where: { id: parseInt(id) },
    data: { title },
  });
 
  ctx.body = { code: 0, message: 'success', data: book };
});
 
// 重命名章节
router.put('/:bookId/chapters/:chapterId/rename', optionalAuth, async (ctx: Context) => {
  const { chapterId } = ctx.params;
  const { title } = ctx.request.body as { title: string };
 
  const chapter = await prisma.bookChapter.update({
    where: { id: parseInt(chapterId) },
    data: { title },
  });
 
  ctx.body = { code: 0, message: 'success', data: chapter };
});
 
// 移动章节到新位置
router.put('/:bookId/chapters/:chapterId/move', optionalAuth, async (ctx: Context) => {
  const { bookId, chapterId } = ctx.params;
  const { newNumber } = ctx.request.body as { newNumber: number };
 
  const chapter = await prisma.bookChapter.findUnique({
    where: { id: parseInt(chapterId) },
  });
 
  if (!chapter) {
    ctx.status = 404;
    ctx.body = { code: 404, message: '章节不存在' };
    return;
  }
 
  // 调整其他章节的序号
  if (newNumber > chapter.number) {
    await prisma.bookChapter.updateMany({
      where: {
        bookId: parseInt(bookId),
        number: { gt: chapter.number, lte: newNumber },
      },
      data: { number: { decrement: 1 } },
    });
  } else if (newNumber < chapter.number) {
    await prisma.bookChapter.updateMany({
      where: {
        bookId: parseInt(bookId),
        number: { gte: newNumber, lt: chapter.number },
      },
      data: { number: { increment: 1 } },
    });
  }
 
  const updatedChapter = await prisma.bookChapter.update({
    where: { id: parseInt(chapterId) },
    data: { number: newNumber },
  });
 
  ctx.body = { code: 0, message: 'success', data: updatedChapter };
});
 
export default router;