update: add blog comment to the blog fetch by id

This commit is contained in:
mahyargdz
2025-04-12 16:14:20 +03:30
parent a9879732a3
commit 4ae735b048
4 changed files with 41 additions and 10 deletions
@@ -86,12 +86,12 @@ export class BlogsController {
return this.blogsService.createComment(paramDto.id, createCommentDto, userId);
}
@ApiOperation({ summary: "Get all comments for a blog" })
@ApiOperation({ summary: "Get all comments for a blog (admin route)" })
@AdminRoute()
@PermissionsDec(PermissionEnum.BLOGS)
@Get(":id/comments")
getBlogComments(@Param() paramDto: ParamDto) {
return this.blogsService.getBlogComments(paramDto.id);
getBlogCommentsForAdmin(@Param() paramDto: ParamDto) {
return this.blogsService.getBlogCommentsForAdmin(paramDto.id);
}
@ApiOperation({ summary: "Update the status of a comment (admin route)" })
+4 -3
View File
@@ -150,7 +150,8 @@ export class BlogsService {
async getBlogById(id: string) {
const blog = await this.findBlogById(id);
return { blog };
const comments = await this.blogCommentsRepository.findBlogCommentsByBlogId(id);
return { blog, comments };
}
//*********************************** */
@@ -208,8 +209,8 @@ export class BlogsService {
}
//*********************************** */
async getBlogComments(blogId: string) {
const comments = await this.blogCommentsRepository.findAllByBlogId(blogId);
async getBlogCommentsForAdmin(blogId: string) {
const comments = await this.blogCommentsRepository.findBlogCommentsForAdmin(blogId);
return { comments };
}
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { IsNull, Repository } from "typeorm";
import { BlogComment } from "../entities/blog-comment.entity";
import { CommentStatus } from "../enums/comment-status.enum";
@@ -17,10 +17,39 @@ export class BlogCommentsRepository extends Repository<BlogComment> {
});
}
async findAllByBlogId(blogId: string) {
async findBlogCommentsForAdmin(blogId: string) {
return this.find({
where: { blog: { id: blogId }, status: CommentStatus.APPROVED },
where: { blog: { id: blogId, deletedAt: IsNull() } },
order: { createdAt: "DESC" },
relations: {
user: true,
},
select: {
id: true,
title: true,
content: true,
createdAt: true,
status: true,
user: { id: true, firstName: true, lastName: true },
},
});
}
async findBlogCommentsByBlogId(blogId: string) {
return this.find({
where: { blog: { id: blogId, deletedAt: IsNull() }, status: CommentStatus.APPROVED },
order: { createdAt: "DESC" },
relations: {
user: true,
},
select: {
id: true,
title: true,
content: true,
createdAt: true,
status: true,
user: { id: true, firstName: true, lastName: true },
},
});
}
}