117 lines
4.0 KiB
TypeScript
117 lines
4.0 KiB
TypeScript
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
|
|
import {
|
|
ApiBearerAuth,
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiTags,
|
|
} from '@nestjs/swagger';
|
|
import { ShareService } from './share.service';
|
|
import { CreateShareDto } from './dto/create-share.dto';
|
|
import {
|
|
CreateShareResponseDto,
|
|
CreatedShareListResponseDto,
|
|
JoinShareResponseDto,
|
|
ShareChallengeDetailResponseDto,
|
|
SubmitShareChallengeResponseDto,
|
|
} from './dto/share-response.dto';
|
|
import { ApiResponseDto } from '../../common/dto/api-response.dto';
|
|
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|
import type { JwtPayload } from '../../common/guards/jwt-auth.guard';
|
|
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
|
import { SubmitShareChallengeDto } from './dto/submit-share-challenge.dto';
|
|
|
|
@ApiTags('分享挑战')
|
|
@Controller('v1/share')
|
|
export class ShareController {
|
|
constructor(private readonly shareService: ShareService) {}
|
|
|
|
@Get('created')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({
|
|
summary: '获取我创建的分享挑战',
|
|
description: '返回当前用户创建过的分享挑战,并统计参与人数和用户排名',
|
|
})
|
|
@ApiResponse({ status: 200, description: '成功' })
|
|
async getCreatedShares(
|
|
@CurrentUser() user: JwtPayload,
|
|
): Promise<ApiResponseDto<CreatedShareListResponseDto>> {
|
|
const data = await this.shareService.getCreatedShares(user.sub);
|
|
return ApiResponseDto.success(data);
|
|
}
|
|
|
|
@Post()
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({
|
|
summary: '创建分享',
|
|
description: '选择6关+标题,生成分享码',
|
|
})
|
|
@ApiResponse({ status: 201, description: '创建成功' })
|
|
@ApiResponse({ status: 400, description: '参数错误' })
|
|
async createShare(
|
|
@CurrentUser() user: JwtPayload,
|
|
@Body() dto: CreateShareDto,
|
|
): Promise<ApiResponseDto<CreateShareResponseDto>> {
|
|
const data = await this.shareService.createShare(user.sub, dto);
|
|
return ApiResponseDto.success(data);
|
|
}
|
|
|
|
@Get(':code')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({
|
|
summary: '获取分享挑战详情',
|
|
description:
|
|
'返回分享挑战基本信息,以及所有已提交挑战结果用户的排行榜。排行榜按答对题数降序、总耗时升序排列。',
|
|
})
|
|
@ApiResponse({ status: 200, description: '成功' })
|
|
@ApiResponse({ status: 404, description: '分享不存在' })
|
|
async getShareChallengeDetail(
|
|
@CurrentUser() user: JwtPayload,
|
|
@Param('code') code: string,
|
|
): Promise<ApiResponseDto<ShareChallengeDetailResponseDto>> {
|
|
const data = await this.shareService.getShareChallengeDetail(
|
|
user.sub,
|
|
code,
|
|
);
|
|
return ApiResponseDto.success(data);
|
|
}
|
|
|
|
@Post(':code/join')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({
|
|
summary: '接受分享',
|
|
description: '通过分享码加入挑战并获取关卡数据',
|
|
})
|
|
@ApiResponse({ status: 200, description: '成功' })
|
|
@ApiResponse({ status: 404, description: '分享不存在' })
|
|
async joinShare(
|
|
@CurrentUser() user: JwtPayload,
|
|
@Param('code') code: string,
|
|
): Promise<ApiResponseDto<JoinShareResponseDto>> {
|
|
const data = await this.shareService.joinShare(user.sub, code);
|
|
return ApiResponseDto.success(data);
|
|
}
|
|
|
|
@Post(':code/submit')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiOperation({
|
|
summary: '提交分享挑战结果',
|
|
description:
|
|
'客户端一次性提交分享挑战中每一关的耗时和答案,服务端校验后返回排名、答对题数、参与人数和完整关卡答案',
|
|
})
|
|
@ApiResponse({ status: 200, description: '成功' })
|
|
@ApiResponse({ status: 404, description: '分享或关卡不存在' })
|
|
async submitChallenge(
|
|
@CurrentUser() user: JwtPayload,
|
|
@Param('code') code: string,
|
|
@Body() dto: SubmitShareChallengeDto,
|
|
): Promise<ApiResponseDto<SubmitShareChallengeResponseDto>> {
|
|
const data = await this.shareService.submitChallenge(user.sub, code, dto);
|
|
return ApiResponseDto.success(data);
|
|
}
|
|
}
|