playlist.controller.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Router from '@koa/router';
  2. import { Context } from 'koa';
  3. import { optionalAuth } from '../../middleware/auth';
  4. import { prisma } from '../../models';
  5. const TEST_USER_ID = '1';
  6. const router = new Router();
  7. // 获取播放列表
  8. router.get('/', optionalAuth, async (ctx: Context) => {
  9. const userId = ctx.state.user?.userId || TEST_USER_ID;
  10. const playlists = await prisma.playlist.findMany({
  11. where: { userId: parseInt(userId) },
  12. include: { _count: { select: { items: true } } },
  13. orderBy: { createdAt: 'desc' },
  14. });
  15. ctx.body = { code: 0, message: 'success', data: playlists };
  16. });
  17. // 创建播放列表
  18. router.post('/', optionalAuth, async (ctx: Context) => {
  19. const userId = ctx.state.user?.userId || TEST_USER_ID;
  20. const { name, description } = ctx.request.body as { name: string; description?: string };
  21. const playlist = await prisma.playlist.create({
  22. data: {
  23. userId: parseInt(userId),
  24. name,
  25. description: description || '',
  26. },
  27. });
  28. ctx.body = { code: 0, message: 'success', data: playlist };
  29. });
  30. // 获取单个播放列表详情
  31. router.get('/:id', optionalAuth, async (ctx: Context) => {
  32. const { id } = ctx.params;
  33. const playlist = await prisma.playlist.findUnique({
  34. where: { id: parseInt(id) },
  35. include: { items: { orderBy: { order: 'asc' }, include: { chapter: true } },
  36. });
  37. ctx.body = { code: 0, message: 'success', data: playlist };
  38. });
  39. // 添加项目到播放列表
  40. router.post('/:id/items', optionalAuth, async (ctx: Context) => {
  41. const { id } = ctx.params;
  42. const { chapterId, audioId } = ctx.request.body as { chapterId?: number; audioId?: string };
  43. const maxOrder = await prisma.playlistItem.aggregate({
  44. where: { playlistId: parseInt(id) },
  45. _max: { order: true },
  46. });
  47. const nextOrder = (maxOrder._max.order ?? -1) + 1;
  48. const item = await prisma.playlistItem.create({
  49. data: {
  50. playlistId: parseInt(id),
  51. chapterId,
  52. audioId,
  53. order: nextOrder,
  54. },
  55. });
  56. ctx.body = { code: 0, message: 'success', data: item };
  57. });
  58. // 重新排序项目
  59. router.put('/:id/items/reorder', optionalAuth, async (ctx: Context) => {
  60. const { id } = ctx.params;
  61. const { items } = ctx.request.body as { items: { id: number; order: number }[] };
  62. for (const item of items) {
  63. await prisma.playlistItem.update({
  64. where: { id: item.id },
  65. data: { order: item.order },
  66. });
  67. }
  68. ctx.body = { code: 0, message: 'success' };
  69. });
  70. // 删除项目
  71. router.delete('/:id/items/:itemId', optionalAuth, async (ctx: Context) => {
  72. const { itemId } = ctx.params;
  73. await prisma.playlistItem.delete({
  74. where: { id: parseInt(itemId) },
  75. });
  76. ctx.body = { code: 0, message: 'success' };
  77. });
  78. export default router;