chore: add most visited blog route

This commit is contained in:
mahyargdz
2025-04-12 16:22:17 +03:30
parent 4ae735b048
commit 39cce1c380
4 changed files with 94 additions and 2 deletions
+26 -2
View File
@@ -148,16 +148,28 @@ export class BlogsService {
}
//*********************************** */
async getBlogById(id: string) {
async getBlogById(id: string, isAdmin: boolean = false) {
const blog = await this.findBlogById(id);
const comments = await this.blogCommentsRepository.findBlogCommentsByBlogId(id);
if (!isAdmin) {
blog.viewCount += 1;
await this.blogsRepository.save(blog);
}
return { blog, comments };
}
//*********************************** */
async getBlogBySlug(slug: string) {
async getBlogBySlug(slug: string, isAdmin: boolean = false) {
const blog = await this.blogsRepository.findOneBySlug(slug);
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
if (!isAdmin) {
blog.viewCount += 1;
await this.blogsRepository.save(blog);
}
return { blog };
}
//*********************************** */
@@ -238,4 +250,16 @@ export class BlogsService {
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
return blog;
}
async incrementViewCount(id: string) {
const blog = await this.findBlogById(id);
blog.viewCount += 1;
await this.blogsRepository.save(blog);
return { message: "View count incremented successfully" };
}
async getMostVisitedBlogs() {
const blogs = await this.blogsRepository.getMostVisitedBlogs();
return { blogs };
}
}