chore: add most visited blog route
This commit is contained in:
@@ -30,6 +30,7 @@ export class BlogsRepository extends Repository<Blog> {
|
||||
createdAt: true,
|
||||
content: true,
|
||||
tags: true,
|
||||
viewCount: true,
|
||||
category: {
|
||||
id: true,
|
||||
title: true,
|
||||
@@ -48,6 +49,34 @@ export class BlogsRepository extends Repository<Blog> {
|
||||
async findOneBySlug(slug: string): Promise<Blog | null> {
|
||||
return this.findOne({
|
||||
where: { slug, deletedAt: IsNull(), isActive: true },
|
||||
relations: {
|
||||
author: true,
|
||||
category: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
previewContent: true,
|
||||
metaTitle: true,
|
||||
metaDescription: true,
|
||||
imageUrl: true,
|
||||
slug: true,
|
||||
createdAt: true,
|
||||
content: true,
|
||||
tags: true,
|
||||
viewCount: true,
|
||||
category: {
|
||||
id: true,
|
||||
title: true,
|
||||
iconUrl: true,
|
||||
description: true,
|
||||
},
|
||||
author: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -75,6 +104,7 @@ export class BlogsRepository extends Repository<Blog> {
|
||||
"blog.imageUrl",
|
||||
"blog.slug",
|
||||
"blog.createdAt",
|
||||
"blog.viewCount",
|
||||
"category.id",
|
||||
"category.iconUrl",
|
||||
"category.title",
|
||||
@@ -133,4 +163,32 @@ export class BlogsRepository extends Repository<Blog> {
|
||||
|
||||
return queryBuilder.getManyAndCount();
|
||||
}
|
||||
|
||||
async getMostVisitedBlogs(limit: number = 5) {
|
||||
return this.createQueryBuilder("blog")
|
||||
.where("blog.deletedAt IS NULL")
|
||||
.andWhere("blog.isActive = :isActive", { isActive: true })
|
||||
.leftJoinAndSelect("blog.category", "category")
|
||||
.leftJoinAndSelect("blog.author", "author")
|
||||
.select([
|
||||
"blog.id",
|
||||
"blog.title",
|
||||
"blog.previewContent",
|
||||
"blog.metaTitle",
|
||||
"blog.metaDescription",
|
||||
"blog.imageUrl",
|
||||
"blog.slug",
|
||||
"blog.createdAt",
|
||||
"blog.viewCount",
|
||||
"category.id",
|
||||
"category.iconUrl",
|
||||
"category.title",
|
||||
"author.id",
|
||||
"author.firstName",
|
||||
"author.lastName",
|
||||
])
|
||||
.orderBy("blog.viewCount", "DESC")
|
||||
.take(limit)
|
||||
.getMany();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user