Sfoglia il codice sorgente

Merge branch 'wudi_dev' of fct/box-nestjs-monorepo into master

wudi 1 mese fa
parent
commit
6e95ac29c8

+ 11 - 36
apps/box-app-api/src/feature/homepage/homepage.controller.ts

@@ -1,6 +1,6 @@
 // apps/box-app-api/src/feature/homepage/homepage.controller.ts
 import { Controller, Get, Query } from '@nestjs/common';
-import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
+import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
 import { HomepageService } from './homepage.service';
 import { HomeAdsDto, AnnouncementDto, CategoryDto } from './dto/homepage.dto';
 import { RecommendedVideosDto } from '../video/dto';
@@ -39,13 +39,20 @@ export class HomepageController {
   }
 
   @Get('categorytags')
+  @ApiQuery({
+    name: 'channelId',
+    required: true,
+    description: '渠道ID',
+  })  
   @ApiOperation({
     summary: '获取分类列表',
     description:
-      '返回 Redis 中的完整分类缓存(box:app:category:all),按 seq 升序。',
+      '返回 Redis 中的完整分类。',
   })
-  async getCategoryList(): Promise<HomeCategoryCacheItem[]> {
-    return this.homepageService.getCategoryList();
+  async getCategoryList(
+    @Query('channelId') channelId: string,
+  ): Promise<any[]> {
+    return this.homepageService.getCategoryTags(channelId);
   }
 
   @Get('tags')
@@ -57,36 +64,4 @@ export class HomepageController {
   async getTagList(): Promise<any> {
     return this.homepageService.getTagList();
   }
-
-  // @Get('tags')
-  // @ApiOperation({
-  //   summary: '获取标签列表',
-  //   description: '返回 Redis 中的完整标签缓存(box:app:tag:all)或对象数据。',
-  // })
-  // async getTagList(): Promise<HomeTagCacheItem[] | Record<string, unknown>> {
-  //   return this.homepageService.getTagList();
-  // }
-
-  // @Get('search/category')
-  // @ApiOperation({
-  //   summary: '按分类名称搜索',
-  //   description: '按名称模糊匹配分类(大小写不敏感),q 为空返回空数组。',
-  // })
-  // async searchCategories(
-  //   @Query('q') query: string,
-  // ): Promise<HomeCategoryCacheItem[]> {
-  //   return this.homepageService.searchByCategoryName(query);
-  // }
-
-  // @Get('search/tag')
-  // @ApiOperation({
-  //   summary: '按标签名称搜索',
-  //   description:
-  //     '按标签名称模糊匹配分类 tags 字段(大小写不敏感),q 为空返回空数组。',
-  // })
-  // async searchTags(
-  //   @Query('q') query: string,
-  // ): Promise<HomeCategoryCacheItem[]> {
-  //   return this.homepageService.searchByTagName(query);
-  // }
 }

+ 53 - 0
apps/box-app-api/src/feature/homepage/homepage.service.ts

@@ -309,6 +309,59 @@ export class HomepageService {
     return this.videoService.getCategories();
   }
 
+  async getCategoryTags(
+    channelId: string
+  ): Promise<any[]> {
+    try {
+      const cacheKey = `category:tag:${channelId}`;
+      const cache = await this.redis.getJson<HomeCategoryCacheItem[]>(cacheKey);
+      if (cache) {
+        return cache;
+      }
+      
+      const channel = await this.prisma.channel.findUnique({
+        where: { channelId },
+        select: {
+          categories: true
+        }
+      });
+
+      type ChannelCategory = {
+        id: string
+        name?: string
+      };
+
+      const categoryIds =
+        (channel?.categories as ChannelCategory[] | null)?.map(c => c.id) ?? [];
+
+      const categories = await this.prisma.category.findMany({
+        where: {
+          id: { in: categoryIds }
+        },
+        select: {
+          name: true,
+          subtitle: true,
+          tagNames: true
+        },
+        orderBy: {
+          seq: 'desc'
+        }
+      });
+
+      if (categories.length > 0) {
+        await this.redis.setJson(cacheKey, categories, 24 * 3600);
+      }
+
+      return categories;
+    } catch (err) {
+      this.logger.error(
+        `Error fetching home section videos for channelId=${channelId}`,
+        err instanceof Error ? err.stack : String(err),
+      );
+      return [];
+    }
+  }  
+
   async getCategoryList(): Promise<HomeCategoryCacheItem[]> {
     const raw = await this.redis.get(tsCacheKeys.category.all());
     return this.parseCategoryCache(raw);