49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, Query, UseGuards } from '@nestjs/common';
|
|
import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
|
import { ArticlesService } from './articles.service';
|
|
import { CreateArticleDto, QueryArticlesDto, CreateArticleResponseDto, QueryArticlesResponseDto } from './dto/article.dto';
|
|
|
|
@ApiTags('articles')
|
|
@Controller('articles')
|
|
|
|
export class ArticlesController {
|
|
constructor(private readonly articlesService: ArticlesService) { }
|
|
|
|
@Post('create')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: '创建文章' })
|
|
@ApiBody({ type: CreateArticleDto })
|
|
@ApiResponse({ status: 200 })
|
|
@UseGuards(JwtAuthGuard)
|
|
async create(@Body() dto: CreateArticleDto): Promise<CreateArticleResponseDto> {
|
|
return this.articlesService.create(dto);
|
|
}
|
|
|
|
@Get('list')
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiOperation({ summary: '查询文章列表(分页)' })
|
|
async list(@Query() query: QueryArticlesDto): Promise<QueryArticlesResponseDto> {
|
|
return this.articlesService.query(query);
|
|
}
|
|
|
|
@Get(':id')
|
|
@HttpCode(HttpStatus.OK)
|
|
@ApiOperation({ summary: '获取文章详情并增加阅读数' })
|
|
async getOne(@Param('id') id: string): Promise<CreateArticleResponseDto> {
|
|
return this.articlesService.getAndIncreaseReadCount(id);
|
|
}
|
|
|
|
// 增加阅读数
|
|
@Post(':id/read-count')
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiOperation({ summary: '增加文章阅读数' })
|
|
async increaseReadCount(@Param('id') id: string): Promise<CreateArticleResponseDto> {
|
|
return this.articlesService.increaseReadCount(id);
|
|
}
|
|
}
|
|
|
|
|