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
This commit is contained in:
parent
aa43f50a8c
commit
bba5a018ab
@ -98,6 +98,9 @@ export class ArticlesService {
|
|||||||
throw new NotFoundException(`Article with ID ${id} not found`);
|
throw new NotFoundException(`Article with ID ${id} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Increment view count
|
||||||
|
await this.articleRepository.increment({ id }, 'views', 1);
|
||||||
|
|
||||||
return article;
|
return article;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,11 +114,27 @@ export class ArticlesService {
|
|||||||
throw new NotFoundException(`Article with slug ${slug} not found`);
|
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<Article> {
|
||||||
|
const article = await this.articleRepository.findOne({
|
||||||
|
where: { id },
|
||||||
|
relations: ['author', 'category'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!article) {
|
||||||
|
throw new NotFoundException(`Article with ID ${id} not found`);
|
||||||
|
}
|
||||||
|
|
||||||
return article;
|
return article;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, dto: UpdateArticleDto): Promise<Article> {
|
async update(id: string, dto: UpdateArticleDto): Promise<Article> {
|
||||||
const article = await this.findOne(id);
|
const article = await this.findOneWithoutIncrement(id);
|
||||||
const oldStatus = article.status;
|
const oldStatus = article.status;
|
||||||
Object.assign(article, dto);
|
Object.assign(article, dto);
|
||||||
const savedArticle = await this.articleRepository.save(article);
|
const savedArticle = await this.articleRepository.save(article);
|
||||||
@ -144,7 +163,7 @@ export class ArticlesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<void> {
|
async remove(id: string): Promise<void> {
|
||||||
const article = await this.findOne(id);
|
const article = await this.findOneWithoutIncrement(id);
|
||||||
|
|
||||||
// Delete from Strapi if article has strapiId
|
// Delete from Strapi if article has strapiId
|
||||||
if (article.strapiId) {
|
if (article.strapiId) {
|
||||||
@ -160,7 +179,7 @@ export class ArticlesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async archive(id: string): Promise<Article> {
|
async archive(id: string): Promise<Article> {
|
||||||
const article = await this.findOne(id);
|
const article = await this.findOneWithoutIncrement(id);
|
||||||
article.status = ArticleStatus.ARCHIVED;
|
article.status = ArticleStatus.ARCHIVED;
|
||||||
const savedArticle = await this.articleRepository.save(article);
|
const savedArticle = await this.articleRepository.save(article);
|
||||||
|
|
||||||
@ -187,7 +206,7 @@ export class ArticlesService {
|
|||||||
id: string,
|
id: string,
|
||||||
status: ArticleStatus = ArticleStatus.PUBLISHED,
|
status: ArticleStatus = ArticleStatus.PUBLISHED,
|
||||||
): Promise<Article> {
|
): Promise<Article> {
|
||||||
const article = await this.findOne(id);
|
const article = await this.findOneWithoutIncrement(id);
|
||||||
const wasDraft = article.status === ArticleStatus.DRAFT;
|
const wasDraft = article.status === ArticleStatus.DRAFT;
|
||||||
article.status = status;
|
article.status = status;
|
||||||
const savedArticle = await this.articleRepository.save(article);
|
const savedArticle = await this.articleRepository.save(article);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user