chore: add most visited blog route
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user