FC_DAN\c9837 преди 1 месец
родител
ревизия
ffe0a99d74
променени са 2 файла, в които са добавени 68 реда и са изтрити 15 реда
  1. 41 1
      apps/box-app-api/src/feature/video/video.controller.ts
  2. 27 14
      apps/box-app-api/src/feature/video/video.service.ts

+ 41 - 1
apps/box-app-api/src/feature/video/video.controller.ts

@@ -64,12 +64,52 @@ export class VideoController {
     type: VideoItemDto,
     isArray: true,
   })
-  async searchBySecondTags(
+  async search(
     @Body() req: VideoListRequestDto,
   ): Promise<VideoItemDto[]> {
     return await this.videoService.getVideoList(req);
   }
 
+  @Get('homevideo')
+  @ApiOperation({
+    summary: '首页视频',
+  })
+  @ApiQuery({
+    name: 'channelId',
+    required: true,
+    description: '渠道ID',
+  })
+  @ApiResponse({
+    status: 200,
+    description: '标签+视频',
+    isArray: true,
+  })
+  async homeVideo(
+    @Query('channelId') channelId: string,
+  ): Promise<any[]> {
+    return await this.videoService.getHomeSectionVideos(channelId);
+  }  
+
+@Get('guess')
+  @ApiOperation({
+    summary: '猜你喜欢',
+  })
+  @ApiQuery({
+    name: 'tag',
+    required: true,
+    description: '标签',
+  })
+  @ApiResponse({
+    status: 200,
+    description: '视频',
+    isArray: true,
+  })
+  async guess(
+    @Query('tag') tag: string,
+  ): Promise<any[]> {
+    return await this.videoService.getGuessLikeVideos(tag);
+  }    
+
   /**
    * GET /api/v1/video/latest
    *

+ 27 - 14
apps/box-app-api/src/feature/video/video.service.ts

@@ -103,7 +103,7 @@ export class VideoService {
    * Reads video IDs from Redis cache, fetches full details from MongoDB,
    * and returns paginated results.
    */
-  async getVideoList(dto: VideoListRequestDto): Promise<VideoListItemDto[]> {
+  async getVideoList(dto: VideoListRequestDto, ttl?: number): Promise<VideoListItemDto[]> {
     const { page, size, tag, keyword, random } = dto;
     const start = (page - 1) * size;
 
@@ -114,7 +114,9 @@ export class VideoService {
       JSON.stringify(dto),
     ).toString('base64')}`;
 
-    const ttl = random ? 15 : 300;
+    if(!ttl){
+      ttl = random ? 15 : 300;
+    }
 
     let fallbackRecords: VideoListItemDto[] = [];
     try {
@@ -127,19 +129,18 @@ export class VideoService {
         status: 'Completed'
       };
 
-      if (tag) {
-        where.secondTags = {
-          has: tag,
-        };
-      }
+      if(random){
+        if (tag) {
+          where.secondTags = tag;
+        }
 
-      if (keyword) {
-        where.title = {
-          contains: keyword, mode: 'insensitive'
+        if (keyword) {
+          where.title = {
+            $regex: keyword,
+            $options: 'i',
+          }
         }
-      }
 
-      if(random){
         fallbackRecords = (await this.mongoPrisma.videoMedia.aggregateRaw({
           pipeline: [
             { $match: where },
@@ -157,6 +158,18 @@ export class VideoService {
           ],
         })) as unknown as VideoListItemDto[];
       }else{
+        if (tag) {
+          where.secondTags = {
+            has: tag,
+          };
+        }
+
+        if (keyword) {
+          where.title = {
+            contains: keyword, mode: 'insensitive'
+          }
+        }
+
         fallbackRecords = (await this.mongoPrisma.videoMedia.findMany({
           where,
           orderBy: [{ addedTime: 'desc' }, { createdAt: 'desc' }],
@@ -473,7 +486,7 @@ export class VideoService {
   async getGuessLikeVideos(tag: string): Promise<VideoItemDto[]> {
     try {
       // Try to fetch from Redis cache first
-      const cached = this.readCachedVideoList(tsCacheKeys.video.guess() + tag, 'guess like videos');
+      const cached = await this.readCachedVideoList(tsCacheKeys.video.guess() + encodeURIComponent(tag), 'guess like videos');
 
       if (cached && Array.isArray(cached) && cached.length > 0) {
         return cached;
@@ -495,7 +508,7 @@ export class VideoService {
         this.mapVideoToDto(v),
       );
 
-      this.redis.setJson(tsCacheKeys.video.guess() + tag, items, 3600).catch(err => {
+      this.redis.setJson(tsCacheKeys.video.guess() + encodeURIComponent(tag), items, 3600).catch(err => {
         this.logger.warn("Redis setJson video.guess failed", err);
       });