新增文章阅读数功能,包括在控制器和服务中添加增加阅读数的方法,并更新相关路由以支持该功能。
This commit is contained in:
@@ -32,6 +32,14 @@ export class ArticlesController {
|
||||
async getOne(@Param('id') id: string): Promise<CreateArticleResponseDto> {
|
||||
return this.articlesService.getAndIncreaseReadCount(id);
|
||||
}
|
||||
|
||||
// 增加阅读数
|
||||
@Post(':id/read-count')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '增加文章阅读数' })
|
||||
async increaseReadCount(@Param('id') id: string): Promise<CreateArticleResponseDto> {
|
||||
return this.articlesService.increaseReadCount(id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -57,6 +57,14 @@ export class ArticlesService {
|
||||
await article.save();
|
||||
return { code: ResponseCode.SUCCESS, message: 'success', data: article.toJSON() as ArticleVo };
|
||||
}
|
||||
|
||||
async increaseReadCount(id: string) {
|
||||
const article = await this.articleModel.findByPk(id);
|
||||
if (!article) throw new NotFoundException('文章不存在');
|
||||
article.readCount += 1;
|
||||
await article.save();
|
||||
return { code: ResponseCode.SUCCESS, message: 'success', data: article.toJSON() as ArticleVo };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user