82 lines
1.6 KiB
TypeScript
82 lines
1.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
ParseIntPipe,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import {
|
|
IsInt,
|
|
IsOptional,
|
|
IsString,
|
|
MaxLength,
|
|
MinLength,
|
|
} from 'class-validator';
|
|
import { Type } from 'class-transformer';
|
|
import { ForumService } from './forum.service';
|
|
import { CurrentUser, JwtAuthGuard, type JwtPayload } from '../auth/roles';
|
|
|
|
class CreateThreadDto {
|
|
@IsString() @MinLength(1) @MaxLength(40)
|
|
board!: string;
|
|
|
|
@IsOptional() @Type(() => Number) @IsInt()
|
|
modelId?: number;
|
|
|
|
@IsString() @MinLength(2) @MaxLength(80)
|
|
title!: string;
|
|
|
|
@IsString() @MinLength(1) @MaxLength(5000)
|
|
body!: string;
|
|
}
|
|
|
|
class ReplyDto {
|
|
@IsString() @MinLength(1) @MaxLength(5000)
|
|
body!: string;
|
|
}
|
|
|
|
@Controller('api')
|
|
export class ForumController {
|
|
constructor(private readonly forum: ForumService) {}
|
|
|
|
@Get('boards')
|
|
boards() {
|
|
return this.forum.boards();
|
|
}
|
|
|
|
@Get('threads')
|
|
list(
|
|
@Query('board') board?: string,
|
|
@Query('modelId') modelId?: string,
|
|
) {
|
|
return this.forum.listThreads({
|
|
board,
|
|
modelId: modelId ? Number(modelId) : undefined,
|
|
});
|
|
}
|
|
|
|
@Get('threads/:id')
|
|
get(@Param('id', ParseIntPipe) id: number) {
|
|
return this.forum.getThread(id);
|
|
}
|
|
|
|
@Post('threads')
|
|
@UseGuards(JwtAuthGuard)
|
|
create(@Body() dto: CreateThreadDto, @CurrentUser() user: JwtPayload) {
|
|
return this.forum.createThread(user.sub, dto);
|
|
}
|
|
|
|
@Post('threads/:id/replies')
|
|
@UseGuards(JwtAuthGuard)
|
|
reply(
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() dto: ReplyDto,
|
|
@CurrentUser() user: JwtPayload,
|
|
) {
|
|
return this.forum.addReply(user.sub, id, dto.body);
|
|
}
|
|
}
|