| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:20 |
| 4 | * @LastEditTime: 2024-12-23 12:45:22 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 媒体资源 |
| 7 | */ |
| 8 | import { |
| 9 | Body, |
| 10 | Controller, |
| 11 | Delete, |
| 12 | Get, |
| 13 | Param, |
| 14 | Post, |
| 15 | Query, |
| 16 | } from '@nestjs/common' |
| 17 | import { ApiTags } from '@nestjs/swagger' |
| 18 | import { GetToken, TokenInfo } from '@yikart/aitoearn-auth' |
| 19 | import { ApiDoc, AppException, ParseObjectIdPipe, ResponseCode, TableDto } from '@yikart/common' |
| 20 | import { MediaGroup } from '@yikart/mongodb' |
| 21 | import { MediaGroupListVo, MediaGroupWithMediaListVo } from './content.vo' |
| 22 | import { |
| 23 | CreateMediaGroupDto, |
| 24 | MediaGroupFilterDto, |
| 25 | UpdateMediaGroupDto, |
| 26 | } from './media-group.dto' |
| 27 | import { MediaGroupService } from './media-group.service' |
| 28 | import { MediaService } from './media.service' |
| 29 | |
| 30 | @ApiTags('Me/MediaGroup') |
| 31 | @Controller('media/group') |
| 32 | export class MediaGroupController { |
| 33 | constructor( |
| 34 | private readonly mediaGroupService: MediaGroupService, |
| 35 | private readonly mediaService: MediaService, |
| 36 | ) { } |
| 37 | |
| 38 | @ApiDoc({ |
| 39 | summary: '创建媒体分组', |
| 40 | description: '创建用于组织资源的媒体分组。', |
| 41 | body: CreateMediaGroupDto.schema, |
| 42 | }) |
| 43 | @Post() |
| 44 | async createGroup( |
| 45 | @GetToken() token: TokenInfo, |
| 46 | @Body() body: CreateMediaGroupDto, |
| 47 | ) { |
| 48 | const res = await this.mediaGroupService.create(token.id, { |
| 49 | type: body.type, |
| 50 | title: body.title, |
| 51 | desc: body.desc, |
| 52 | }) |
| 53 | return res |
| 54 | } |
| 55 | |
| 56 | @ApiDoc({ |
| 57 | summary: '删除媒体分组', |
| 58 | description: '根据ID删除媒体分组。', |
| 59 | }) |
| 60 | @Delete(':id') |
| 61 | async delGroup(@GetToken() token: TokenInfo, @Param('id', ParseObjectIdPipe) id: string) { |
| 62 | const mediaGroup = await this.mediaGroupService.getInfo(id) |
| 63 | if (!mediaGroup || mediaGroup.userId !== token.id) { |
| 64 | throw new AppException(ResponseCode.MediaGroupNotFound, 'Media Group not found') |
| 65 | } |
| 66 | if ( |
| 67 | mediaGroup.isDefault |
| 68 | ) { |
| 69 | throw new AppException(ResponseCode.MediaGroupDefaultNotAllowed, 'Default media group cannot be deleted') |
| 70 | } |
| 71 | const res = await this.mediaGroupService.del(id) |
| 72 | return res |
| 73 | } |
| 74 | |
| 75 | @ApiDoc({ |
| 76 | summary: '更新媒体分组信息', |
| 77 | description: '更新媒体分组的属性。', |
| 78 | body: UpdateMediaGroupDto.schema, |
| 79 | }) |
| 80 | @Post('info/:id') |
| 81 | async updateGroupInfo( |
| 82 | @GetToken() token: TokenInfo, |
| 83 | @Param('id', ParseObjectIdPipe) id: string, |
| 84 | @Body() body: UpdateMediaGroupDto, |
| 85 | ) { |
| 86 | const dataInfo = await this.mediaGroupService.getInfo(id) |
| 87 | if (!dataInfo || dataInfo.userId !== token.id) { |
| 88 | throw new AppException(ResponseCode.MediaGroupNotFound) |
| 89 | } |
| 90 | const res = await this.mediaGroupService.updateInfo(id, body) |
| 91 | return res |
| 92 | } |
| 93 | |
| 94 | @ApiDoc({ |
| 95 | summary: '获取媒体分组详情', |
| 96 | description: '根据ID获取媒体分组详情。', |
| 97 | }) |
| 98 | @Get('info/:id') |
| 99 | async getGroupInfo(@Param('id', ParseObjectIdPipe) id: string) { |
| 100 | const res = await this.mediaGroupService.getInfo(id) |
| 101 | return res |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * 获取资源组的简略图列表 |
| 106 | * @param userId |
| 107 | * @param group |
| 108 | * @returns |
| 109 | */ |
| 110 | private async getMediaDesList(userId: string, group: MediaGroup) { |
| 111 | const res = await this.mediaService.getList( |
| 112 | { pageNo: 1, pageSize: 3 }, |
| 113 | { |
| 114 | userId, |
| 115 | groupId: group.id, |
| 116 | }, |
| 117 | ) |
| 118 | return MediaGroupWithMediaListVo.create({ ...group, mediaList: res }) |
| 119 | } |
| 120 | |
| 121 | @ApiDoc({ |
| 122 | summary: '获取媒体分组列表', |
| 123 | description: '分页获取媒体分组列表。', |
| 124 | query: MediaGroupFilterDto.schema, |
| 125 | }) |
| 126 | @Get('list/:pageNo/:pageSize') |
| 127 | async getGroupList( |
| 128 | @GetToken() token: TokenInfo, |
| 129 | @Param() param: TableDto, |
| 130 | @Query() query: MediaGroupFilterDto, |
| 131 | ) { |
| 132 | const { list, total } = await this.mediaGroupService.getList(param, { |
| 133 | userId: token.id, |
| 134 | ...query, |
| 135 | }) |
| 136 | |
| 137 | const updatedList = await Promise.all( |
| 138 | list.map(item => this.getMediaDesList(token.id, item)), |
| 139 | ) |
| 140 | |
| 141 | return MediaGroupListVo.create({ list: updatedList, total }) |
| 142 | } |
| 143 | } |
| 144 |