164 lines
4.9 KiB
TypeScript
164 lines
4.9 KiB
TypeScript
import {
|
||
Body,
|
||
Controller,
|
||
Get,
|
||
Param,
|
||
Patch,
|
||
Put,
|
||
Query,
|
||
Req,
|
||
} from '@nestjs/common';
|
||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||
import type {
|
||
AdminResourcePage,
|
||
AdminWorksheet,
|
||
AdminWorksheetPage,
|
||
AdminWorksheetResource,
|
||
ApiSuccess,
|
||
ContentPublication,
|
||
} from '@doodle/contracts';
|
||
import { CurrentUser, Roles } from '../auth/auth.decorators';
|
||
import type { AuthenticatedUser, HttpRequest } from '../common/http-request';
|
||
import { WorksheetService } from '../worksheets/worksheet.service';
|
||
import {
|
||
AdminResourceQueryDto,
|
||
AdminWorksheetQueryDto,
|
||
UpdateContentStatusDto,
|
||
} from './content.dto';
|
||
import { PublicationService } from './publication.service';
|
||
import { ResourcePreviewService } from './resource-preview.service';
|
||
|
||
@Controller('admin/worksheets')
|
||
@ApiTags('admin/worksheets')
|
||
@Roles('admin')
|
||
/** 管理预览使用本站 JWT 与当前管理员身份,不接受客户端自报测试身份。 */
|
||
export class AdminWorksheetController {
|
||
constructor(
|
||
private readonly worksheets: WorksheetService,
|
||
private readonly publication: PublicationService,
|
||
) {}
|
||
|
||
@Get()
|
||
@ApiOperation({ summary: '全部状态题型(管理员,支持状态及原有筛选)' })
|
||
async list(
|
||
@Query() query: AdminWorksheetQueryDto,
|
||
): Promise<ApiSuccess<AdminWorksheetPage>> {
|
||
return {
|
||
code: 0,
|
||
message: 'ok',
|
||
data: await this.worksheets.listForAdmin(query),
|
||
};
|
||
}
|
||
|
||
@Get(':id')
|
||
@ApiOperation({ summary: '题型管理预览(管理员,包含草稿及有效可见性)' })
|
||
async get(@Param('id') id: string): Promise<ApiSuccess<AdminWorksheet>> {
|
||
return {
|
||
code: 0,
|
||
message: 'ok',
|
||
data: await this.worksheets.getForAdmin(id),
|
||
};
|
||
}
|
||
|
||
@Put(':id/status')
|
||
@ApiOperation({ summary: '更新发布状态(管理员,小程序 PUT 兼容入口)' })
|
||
async updateByPut(
|
||
@Param('id') id: string,
|
||
@Body() input: UpdateContentStatusDto,
|
||
@CurrentUser() actor: AuthenticatedUser,
|
||
@Req() request: HttpRequest,
|
||
) {
|
||
return this.update(id, input, actor, request);
|
||
}
|
||
|
||
@Patch(':id/status')
|
||
@ApiOperation({
|
||
summary: '题型发布、下架或转草稿(管理员);发布要求分类已上线',
|
||
})
|
||
async update(
|
||
@Param('id') id: string,
|
||
@Body() input: UpdateContentStatusDto,
|
||
@CurrentUser() actor: AuthenticatedUser,
|
||
@Req() request: HttpRequest,
|
||
): Promise<ApiSuccess<ContentPublication>> {
|
||
return {
|
||
code: 0,
|
||
message: 'ok',
|
||
data: await this.publication.update(
|
||
'worksheet',
|
||
id,
|
||
input,
|
||
actor,
|
||
request.requestId!,
|
||
),
|
||
};
|
||
}
|
||
}
|
||
|
||
@Controller('admin/worksheet-resources')
|
||
@ApiTags('admin/worksheet-resources')
|
||
@Roles('admin')
|
||
/** 资源公开列表尚未开放;管理预览和上下架先落实同一发布规则。 */
|
||
export class AdminResourceController {
|
||
constructor(
|
||
private readonly resources: ResourcePreviewService,
|
||
private readonly publication: PublicationService,
|
||
) {}
|
||
|
||
@Get()
|
||
@ApiOperation({
|
||
summary: '分页查询资源(管理员,支持 worksheetId/status)',
|
||
})
|
||
async list(
|
||
@Query() query: AdminResourceQueryDto,
|
||
): Promise<ApiSuccess<AdminResourcePage>> {
|
||
return {
|
||
code: 0,
|
||
message: 'ok',
|
||
data: await this.resources.list(query),
|
||
};
|
||
}
|
||
|
||
@Get(':id')
|
||
@ApiOperation({ summary: '资源管理预览(管理员,包含草稿及有效可见性)' })
|
||
async get(
|
||
@Param('id') id: string,
|
||
): Promise<ApiSuccess<AdminWorksheetResource>> {
|
||
return { code: 0, message: 'ok', data: await this.resources.get(id) };
|
||
}
|
||
|
||
@Put(':id/status')
|
||
@ApiOperation({ summary: '更新发布状态(管理员,小程序 PUT 兼容入口)' })
|
||
async updateByPut(
|
||
@Param('id') id: string,
|
||
@Body() input: UpdateContentStatusDto,
|
||
@CurrentUser() actor: AuthenticatedUser,
|
||
@Req() request: HttpRequest,
|
||
) {
|
||
return this.update(id, input, actor, request);
|
||
}
|
||
|
||
@Patch(':id/status')
|
||
@ApiOperation({
|
||
summary: '资源发布、下架或转草稿(管理员);发布要求题型及分类已上线',
|
||
})
|
||
async update(
|
||
@Param('id') id: string,
|
||
@Body() input: UpdateContentStatusDto,
|
||
@CurrentUser() actor: AuthenticatedUser,
|
||
@Req() request: HttpRequest,
|
||
): Promise<ApiSuccess<ContentPublication>> {
|
||
return {
|
||
code: 0,
|
||
message: 'ok',
|
||
data: await this.publication.update(
|
||
'resource',
|
||
id,
|
||
input,
|
||
actor,
|
||
request.requestId!,
|
||
),
|
||
};
|
||
}
|
||
}
|