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`);
|
||||
}
|
||||
|
||||
// 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<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;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateArticleDto): Promise<Article> {
|
||||
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<void> {
|
||||
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<Article> {
|
||||
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<Article> {
|
||||
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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user