返回 AiToEarn
media.controller.ts
根目录 / project / aitoearn-backend / apps / aitoearn-server / src / core / content / media.controller.ts
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 Put,
16 Query,
17 } from '@nestjs/common'
18 import { ApiTags } from '@nestjs/swagger'
19 import { GetToken, TokenInfo } from '@yikart/aitoearn-auth'
20 import { ApiDoc, AppException, ParseObjectIdPipe, ResponseCode, TableDto } from '@yikart/common'
21 import { MediaListVo, MediaVo } from './content.vo'
22 import { MaterialGroupService } from './material-group.service'
23 import { MediaGroupService } from './media-group.service'
24 import {
25 AddUseCountOfListDto,
26 CreateMediaDto,
27 MediaFilterDto,
28 MediaFilterSchema,
29 MediaIdsDto,
30 TransferMediaDto,
31 } from './media.dto'
32 import { MediaService } from './media.service'
33
34 @ApiTags('Me/Media')
35 @Controller('media')
36 export class MediaController {
37 constructor(
38 private readonly mediaService: MediaService,
39 private readonly mediaGroupService: MediaGroupService,
40 private readonly materialGroupService: MaterialGroupService,
41 ) { }
42
43 @ApiDoc({
44 summary: '创建媒体资源',
45 description: '创建媒体资源,包含元数据和文件URL。',
46 body: CreateMediaDto.schema,
47 })
48 @Post()
49 async create(
50 @GetToken() token: TokenInfo,
51 @Body() body: CreateMediaDto,
52 ) {
53 const res = await this.mediaService.create(token.id, body)
54 return MediaVo.create(res)
55 }
56
57 @ApiDoc({
58 summary: '媒体资源转移到其他分组',
59 description: '将媒体资源移动或复制到目标媒体分组。move 模式直接移动,copy 模式复制并重置使用次数。',
60 body: TransferMediaDto.schema,
61 })
62 @Post('/transfer')
63 async transferToGroup(
64 @GetToken() token: TokenInfo,
65 @Body() body: TransferMediaDto,
66 ) {
67 const targetGroup = await this.materialGroupService.getGroupInfo(body.targetGroupId)
68 if (!targetGroup || targetGroup.userId !== token.id) {
69 throw new AppException(ResponseCode.MediaGroupNotFound)
70 }
71
72 const count = await this.mediaService.transferToGroup(
73 token.id,
74 body.ids,
75 body.targetGroupId,
76 body.mode,
77 )
78 return { count }
79 }
80
81 @ApiDoc({
82 summary: '批量删除媒体资源',
83 description: '根据ID列表批量删除媒体资源。',
84 body: MediaIdsDto.schema,
85 })
86 @Delete('ids')
87 async delByIds(@GetToken() token: TokenInfo, @Body() body: MediaIdsDto) {
88 const res = await this.mediaService.delByIds(token.id, body.ids)
89 return res
90 }
91
92 @ApiDoc({
93 summary: '按条件删除媒体资源',
94 description: '删除符合筛选条件的媒体资源。',
95 body: MediaFilterDto.schema,
96 })
97 @Delete('filter')
98 async delByFilter(@GetToken() token: TokenInfo, @Body() body: MediaFilterDto) {
99 const res = await this.mediaService.delByFilter(token.id, body)
100 return res
101 }
102
103 @ApiDoc({
104 summary: '删除媒体资源',
105 description: '根据ID删除媒体资源。',
106 })
107 @Delete(':id')
108 async del(@GetToken() token: TokenInfo, @Param('id', ParseObjectIdPipe) id: string) {
109 const media = await this.mediaService.getInfo(id)
110 if (!media || media.userId !== token.id) {
111 throw new AppException(ResponseCode.MediaNotFound, 'Media Group not found')
112 }
113 const res = await this.mediaService.del(id)
114 return res
115 }
116
117 @ApiDoc({
118 summary: '获取媒体资源详情',
119 description: '根据ID获取媒体资源详情。',
120 })
121 @Get('info/:id')
122 async getInfo(@Param('id', ParseObjectIdPipe) id: string) {
123 const res = await this.mediaService.getInfo(id)
124 return res ? MediaVo.create(res) : res
125 }
126
127 @Get('list/:pageNo/:pageSize')
128 @ApiDoc({
129 summary: '获取媒体资源列表',
130 description: '分页获取媒体资源列表。',
131 query: MediaFilterSchema,
132 })
133 async getList(
134 @GetToken() token: TokenInfo,
135 @Param() param: TableDto,
136 @Query() query: MediaFilterDto,
137 ) {
138 const res = await this.mediaService.getList(param, {
139 userId: token.id,
140 ...query,
141 })
142 return MediaListVo.create(res)
143 }
144
145 @ApiDoc({
146 summary: '增加媒体使用次数',
147 description: '批量增加媒体资源的使用次数。',
148 body: AddUseCountOfListDto.schema,
149 })
150 @Put('addUseCountOfList')
151 async addUseCountOfList(
152 @GetToken() token: TokenInfo,
153 @Body() body: AddUseCountOfListDto,
154 ) {
155 const res = await this.mediaService.addUseCountOfList(token.id, body.ids)
156 return res
157 }
158 }
159
159 lines TYPESCRIPT