import { Controller, Get, Param, Query } from '@nestjs/common'; import { CurrentUser, Public, type AuthPrincipal } from '../../common/auth'; import { AppError } from '../../common/errors'; import { CoursesService } from './courses.service'; @Controller('v1') export class CoursesController { constructor(private readonly courses: CoursesService) {} @Get('courses') list(@CurrentUser() user: AuthPrincipal) { return this.courses.list(user.id); } @Get('courses/:code') detail(@CurrentUser() user: AuthPrincipal, @Param('code') code: string) { return this.courses.detail(user.id, code); } @Get('courses/:code/lessons') lessons(@CurrentUser() user: AuthPrincipal, @Param('code') code: string) { return this.courses.lessons(user.id, code); } @Get('courses/:code/play') play( @CurrentUser() user: AuthPrincipal, @Param('code') code: string, @Query('chapterId') chapterId?: string, @Query('playId') playId?: string, ) { if (!chapterId || !playId) throw AppError.badRequest('参数错误:缺少 chapterId 或 playId'); return this.courses.playUrl(user.id, code, chapterId, playId); } @Public() @Get('free-lessons') freeLessons(@Query('seriesNo') seriesNo?: string) { if (seriesNo === undefined || seriesNo === '') return this.courses.freeLessons(); const n = Number(seriesNo); return this.courses.freeLessons(Number.isFinite(n) ? n : undefined); } }