update: the blog comment logic for admin
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsEnum, IsOptional } from "class-validator";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
||||
import { BlogMessage } from "../../../common/enums/message.enum";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
export class BlogCommentQueryDto extends PaginationDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: BlogMessage.SEARCH_QUERY_STRING })
|
||||
@ApiPropertyOptional({ description: "Search query", example: "search query" })
|
||||
q?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(CommentStatus)
|
||||
@ApiPropertyOptional({ description: "Status", example: "status" })
|
||||
status?: string;
|
||||
}
|
||||
+27
-19
@@ -1,25 +1,25 @@
|
||||
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Patch, Post, Query } from "@nestjs/common";
|
||||
import { ApiOperation } from "@nestjs/swagger";
|
||||
|
||||
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
||||
import { AuthGuards } from "../../../common/decorators/auth-guard.decorator";
|
||||
import { Pagination } from "../../../common/decorators/pagination.decorator";
|
||||
import { PermissionsDec } from "../../../common/decorators/permission.decorator";
|
||||
import { SkipAuth } from "../../../common/decorators/skip-auth.decorator";
|
||||
import { UserDec } from "../../../common/decorators/user.decorator";
|
||||
import { ParamDto } from "../../../common/DTO/param.dto";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { PermissionEnum } from "../../users/enums/permission.enum";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
import { BlogSlugDto } from "../DTO/get-blog-by-slug.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "../DTO/update-blog.dto";
|
||||
import { UpdateBlogCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "../DTO/update-comment-status.dto";
|
||||
import { BlogsService } from "../providers/blogs.service";
|
||||
|
||||
import { BlogCommentQueryDto } from "./DTO/blog-comment-query.dto";
|
||||
import { CreateBlogDto } from "./DTO/create-blog.dto";
|
||||
import { CreateBlogCategoryDto } from "./DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "./DTO/create-comment.dto";
|
||||
import { BlogSlugDto } from "./DTO/get-blog-by-slug.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "./DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "./DTO/update-blog.dto";
|
||||
import { UpdateBlogCategoryDto } from "./DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "./DTO/update-comment-status.dto";
|
||||
import { BlogsService } from "./providers/blogs.service";
|
||||
import { AdminRoute } from "../../common/decorators/admin.decorator";
|
||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
import { Pagination } from "../../common/decorators/pagination.decorator";
|
||||
import { PermissionsDec } from "../../common/decorators/permission.decorator";
|
||||
import { SkipAuth } from "../../common/decorators/skip-auth.decorator";
|
||||
import { UserDec } from "../../common/decorators/user.decorator";
|
||||
import { ParamDto } from "../../common/DTO/param.dto";
|
||||
import { CategoryListSearchQueryDto } from "../danak-services/DTO/category-search-query.dto";
|
||||
import { PermissionEnum } from "../users/enums/permission.enum";
|
||||
@Controller("blogs")
|
||||
@AuthGuards()
|
||||
export class BlogsController {
|
||||
@@ -131,6 +131,14 @@ export class BlogsController {
|
||||
return this.blogsService.getBlogCommentsForAdmin(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Get all comments for a blog (admin route)" })
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
@Get("comments")
|
||||
getAllCommentForAdmin(@Query() queryDto: BlogCommentQueryDto) {
|
||||
return this.blogsService.getAllCommentForAdmin(queryDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Update the status of a comment (admin route)" })
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { BlogsController } from "./controllers/blogs.controller";
|
||||
import { BlogsController } from "./blogs.controller";
|
||||
import { BlogCategory } from "./entities/blog-category.entity";
|
||||
import { BlogComment } from "./entities/blog-comment.entity";
|
||||
import { Blog } from "./entities/blog.entity";
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Not } from "typeorm";
|
||||
import { BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { BlogCommentQueryDto } from "../DTO/blog-comment-query.dto";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
@@ -260,6 +261,13 @@ export class BlogsService {
|
||||
return { comments };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async getAllCommentForAdmin(queryDto: BlogCommentQueryDto) {
|
||||
const [comments, count] = await this.blogCommentsRepository.getAllCommentsForAdmin(queryDto);
|
||||
return { comments, count, paginate: true };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private generateSlug(slug: string): string {
|
||||
if (!slug) return "";
|
||||
|
||||
@@ -2,6 +2,8 @@ import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { IsNull, Repository } from "typeorm";
|
||||
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { BlogCommentQueryDto } from "../DTO/blog-comment-query.dto";
|
||||
import { BlogComment } from "../entities/blog-comment.entity";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
|
||||
@@ -35,6 +37,27 @@ export class BlogCommentsRepository extends Repository<BlogComment> {
|
||||
});
|
||||
}
|
||||
|
||||
async getAllCommentsForAdmin(queryDto: BlogCommentQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
const query = this.createQueryBuilder("blogComment")
|
||||
.leftJoin("blogComment.blog", "blog")
|
||||
.addSelect(["blog.id", "blog.title"])
|
||||
.leftJoin("blogComment.user", "user")
|
||||
.addSelect(["user.id", "user.firstName", "user.lastName"]);
|
||||
|
||||
if (queryDto.q) {
|
||||
query.andWhere("blogComment.content ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
if (queryDto.status) {
|
||||
query.andWhere("blogComment.status = :status", { status: queryDto.status });
|
||||
}
|
||||
|
||||
query.orderBy("blogComment.createdAt", "DESC").skip(skip).take(limit);
|
||||
|
||||
return query.getManyAndCount();
|
||||
}
|
||||
|
||||
async findBlogCommentsByBlogId(blogId: string) {
|
||||
return this.find({
|
||||
where: { blog: { id: blogId, deletedAt: IsNull() }, status: CommentStatus.APPROVED },
|
||||
|
||||
Reference in New Issue
Block a user