From bba5a018ab69d86658a07eea7a203acc51b1bc01 Mon Sep 17 00:00:00 2001 From: echo Date: Sun, 1 Mar 2026 00:20:46 +0100 Subject: [PATCH] fix: implement article view count tracking - Add view count increment in findOne() and findBySlug() methods - Create findOneWithoutIncrement() for internal operations - Update remove(), archive(), and publish() to use findOneWithoutIncrement() - Prevents view count inflation from admin operations - Matches live blog view tracking implementation --- backend/src/modules/articles.service.ts | 27 +++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/backend/src/modules/articles.service.ts b/backend/src/modules/articles.service.ts index 4bd1085..5468eca 100644 --- a/backend/src/modules/articles.service.ts +++ b/backend/src/modules/articles.service.ts @@ -98,6 +98,9 @@ export class ArticlesService { throw new NotFoundException(`Article with ID ${id} not found`); } + // Increment view count + await this.articleRepository.increment({ id }, 'views', 1); + return article; } @@ -111,11 +114,27 @@ export class ArticlesService { throw new NotFoundException(`Article with slug ${slug} not found`); } + // Increment view count + await this.articleRepository.increment({ slug }, 'views', 1); + + return article; + } + + async findOneWithoutIncrement(id: string): Promise
{ + const article = await this.articleRepository.findOne({ + where: { id }, + relations: ['author', 'category'], + }); + + if (!article) { + throw new NotFoundException(`Article with ID ${id} not found`); + } + return article; } async update(id: string, dto: UpdateArticleDto): Promise
{ - const article = await this.findOne(id); + const article = await this.findOneWithoutIncrement(id); const oldStatus = article.status; Object.assign(article, dto); const savedArticle = await this.articleRepository.save(article); @@ -144,7 +163,7 @@ export class ArticlesService { } async remove(id: string): Promise { - const article = await this.findOne(id); + const article = await this.findOneWithoutIncrement(id); // Delete from Strapi if article has strapiId if (article.strapiId) { @@ -160,7 +179,7 @@ export class ArticlesService { } async archive(id: string): Promise
{ - const article = await this.findOne(id); + const article = await this.findOneWithoutIncrement(id); article.status = ArticleStatus.ARCHIVED; const savedArticle = await this.articleRepository.save(article); @@ -187,7 +206,7 @@ export class ArticlesService { id: string, status: ArticleStatus = ArticleStatus.PUBLISHED, ): Promise
{ - const article = await this.findOne(id); + const article = await this.findOneWithoutIncrement(id); const wasDraft = article.status === ArticleStatus.DRAFT; article.status = status; const savedArticle = await this.articleRepository.save(article);