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"; @Injectable() export class BlogCommentsRepository extends Repository { constructor(@InjectRepository(BlogComment) blogCommentsRepository: Repository) { super(blogCommentsRepository.target, blogCommentsRepository.manager, blogCommentsRepository.queryRunner); } async findOneById(id: string) { return this.findOne({ where: { id }, }); } async findBlogCommentsForAdmin(blogId: string) { return this.find({ where: { blog: { id: blogId, deletedAt: IsNull() } }, order: { createdAt: "DESC" }, relations: { user: true, replies: true, }, select: { id: true, title: true, content: true, createdAt: true, status: true, user: { id: true, firstName: true, lastName: true }, replies: { id: true, content: true, createdAt: true, author: { id: true, firstName: true, lastName: true } }, }, }); } async getAllCommentsForAdmin(queryDto: BlogCommentQueryDto) { const { limit, skip } = PaginationUtils(queryDto); const query = this.createQueryBuilder("blogComment") .leftJoin("blogComment.blog", "blog") .where("blog.deletedAt IS NULL") .addSelect(["blog.id", "blog.title"]) .leftJoin("blogComment.user", "user") .addSelect(["user.id", "user.firstName", "user.lastName"]) .leftJoin("blogComment.replies", "replies") .addSelect(["replies.id", "replies.content", "replies.createdAt"]) .leftJoin("replies.author", "repliesAuthor") .addSelect(["repliesAuthor.id", "repliesAuthor.firstName", "repliesAuthor.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 }, order: { createdAt: "DESC" }, relations: { user: true, replies: true, }, select: { id: true, title: true, content: true, createdAt: true, status: true, user: { id: true, firstName: true, lastName: true, profilePic: true }, replies: { id: true, content: true, createdAt: true, author: { id: true, firstName: true, lastName: true } }, }, }); } }