소스 검색

feat(video): add delete functionality for video media by ID

Dave 2 달 전
부모
커밋
3387d13227

+ 29 - 0
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.controller.ts

@@ -9,6 +9,7 @@ import {
   Post,
   Req,
   BadRequestException,
+  Delete,
 } from '@nestjs/common';
 import type { FastifyRequest } from 'fastify';
 import {
@@ -250,4 +251,32 @@ export class VideoMediaController {
     }
     return this.videoMediaService.updateCover(id, mpFile);
   }
+
+  // TODO: 删除视频媒体
+  @ApiOperation({
+    summary: '删除视频媒体',
+    description: '根据 ID 删除指定的视频媒体',
+  })
+  @ApiParam({
+    name: 'id',
+    type: String,
+    description: '视频媒体 MongoDB ID',
+    example: '507f1f77bcf86cd799439011',
+  })
+  @ApiOkResponse({
+    description: '删除成功',
+    schema: {
+      type: 'object',
+      properties: {
+        id: { type: 'string', example: '507f1f77bcf86cd799439011' },
+      },
+    },
+  })
+  @ApiNotFoundResponse({
+    description: '视频媒体不存在',
+  })
+  @Delete(':id')
+  async delete(@Param('id') id: string) {
+    return this.videoMediaService.delete(id);
+  }
 }

+ 30 - 0
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -316,6 +316,36 @@ export class VideoMediaService {
     };
   }
 
+  // create an async function to delete a video media by id and return the deleted id also update Redis cache
+  async delete(id: string) {
+    const video = await this.prisma.videoMedia.findUnique({
+      where: { id },
+    });
+
+    if (!video) {
+      throw new NotFoundException('Video not found');
+    }
+
+    await this.prisma.videoMedia.delete({
+      where: { id },
+    });
+
+    // Refresh category video lists cache if video has a category
+    if (video.categoryIds && video.categoryIds.length > 0) {
+      for (const categoryId of video.categoryIds) {
+        await this.cacheSyncService.scheduleAction({
+          entityType: CacheEntityType.VIDEO_LIST,
+          operation: 'REFRESH',
+          payload: { categoryId },
+        } as any);
+      }
+    }
+
+    return {
+      id,
+    };
+  }
+
   /**
    * Upload and update VideoMedia cover image.
    */