新增文章阅读数功能,包括在控制器和服务中添加增加阅读数的方法,并更新相关路由以支持该功能。

This commit is contained in:
richarjiang
2025-08-14 16:11:22 +08:00
parent b4dfdcfe70
commit 96a1190f74
3 changed files with 173 additions and 172 deletions

View File

@@ -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);
}
}

View File

@@ -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 };
}
}