chore: add blog blog comment and blog category crud
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { IsNull, Repository } from "typeorm";
|
||||
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { BlogCategory } from "../entities/blog-category.entity";
|
||||
|
||||
@Injectable()
|
||||
export class BlogCategoriesRepository extends Repository<BlogCategory> {
|
||||
constructor(@InjectRepository(BlogCategory) repository: Repository<BlogCategory>) {
|
||||
super(repository.target, repository.manager, repository.queryRunner);
|
||||
}
|
||||
|
||||
async findOneById(id: string): Promise<BlogCategory | null> {
|
||||
return this.findOne({
|
||||
where: { id, deletedAt: IsNull() },
|
||||
});
|
||||
}
|
||||
|
||||
async getCategoriesUserSide() {
|
||||
return this.find({
|
||||
where: { isActive: true, deletedAt: IsNull() },
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
}
|
||||
|
||||
async getCategories(queryDto: CategoryListSearchQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const query = this.createQueryBuilder("blogCategory").where("blogCategory.deletedAt IS NULL");
|
||||
|
||||
if (queryDto.q) {
|
||||
query.andWhere("blogCategory.title ILIKE :q OR blogCategory.slug ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
if (queryDto.isActive) {
|
||||
query.andWhere("blogCategory.isActive = :isActive", { isActive: queryDto.isActive });
|
||||
}
|
||||
|
||||
query.skip(skip).take(limit).orderBy({ createdAt: "DESC" });
|
||||
|
||||
return query.getManyAndCount();
|
||||
}
|
||||
|
||||
async createCategory(createBlogCategoryDto: CreateCategoryDto) {
|
||||
const { title, description, iconUrl, isActive } = createBlogCategoryDto;
|
||||
|
||||
const blogCategory = this.create({ title, description, iconUrl, isActive });
|
||||
|
||||
return this.save(blogCategory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { BlogComment } from "../entities/blog-comment.entity";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
|
||||
@Injectable()
|
||||
export class BlogCommentsRepository extends Repository<BlogComment> {
|
||||
constructor(@InjectRepository(BlogComment) blogCommentsRepository: Repository<BlogComment>) {
|
||||
super(blogCommentsRepository.target, blogCommentsRepository.manager, blogCommentsRepository.queryRunner);
|
||||
}
|
||||
|
||||
async findOneById(id: string) {
|
||||
return this.findOne({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByBlogId(blogId: string) {
|
||||
return this.find({
|
||||
where: { blog: { id: blogId }, status: CommentStatus.APPROVED },
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Brackets, IsNull, Repository } from "typeorm";
|
||||
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { Blog } from "../entities/blog.entity";
|
||||
|
||||
@Injectable()
|
||||
export class BlogsRepository extends Repository<Blog> {
|
||||
constructor(@InjectRepository(Blog) blogsRepository: Repository<Blog>) {
|
||||
super(blogsRepository.target, blogsRepository.manager, blogsRepository.queryRunner);
|
||||
}
|
||||
|
||||
async findOneById(id: string): Promise<Blog | null> {
|
||||
return this.findOne({
|
||||
where: { id, deletedAt: IsNull() },
|
||||
relations: {
|
||||
author: true,
|
||||
category: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
previewContent: true,
|
||||
metaTitle: true,
|
||||
metaDescription: true,
|
||||
imageUrl: true,
|
||||
slug: true,
|
||||
createdAt: true,
|
||||
content: true,
|
||||
tags: true,
|
||||
category: {
|
||||
id: true,
|
||||
title: true,
|
||||
iconUrl: true,
|
||||
description: true,
|
||||
},
|
||||
author: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOneBySlug(slug: string): Promise<Blog | null> {
|
||||
return this.findOne({
|
||||
where: { slug, deletedAt: IsNull(), isActive: true },
|
||||
});
|
||||
}
|
||||
|
||||
async fineOneByTitle(title: string): Promise<Blog | null> {
|
||||
return this.findOne({
|
||||
where: { title, deletedAt: IsNull(), isActive: true },
|
||||
});
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogsForUserSide(queryDto: BlogSearchQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const queryBuilder = this.createQueryBuilder("blog")
|
||||
.where("blog.deletedAt IS NULL")
|
||||
.andWhere("blog.isActive = :isActive", { isActive: true })
|
||||
.leftJoinAndSelect("blog.category", "category")
|
||||
.leftJoinAndSelect("blog.author", "author")
|
||||
.select([
|
||||
"blog.id",
|
||||
"blog.title",
|
||||
"blog.previewContent",
|
||||
"blog.metaTitle",
|
||||
"blog.metaDescription",
|
||||
"blog.imageUrl",
|
||||
"blog.slug",
|
||||
"blog.createdAt",
|
||||
"category.id",
|
||||
"category.iconUrl",
|
||||
"category.title",
|
||||
"author.id",
|
||||
"author.firstName",
|
||||
"author.lastName",
|
||||
]);
|
||||
|
||||
if (queryDto.categoryId) {
|
||||
queryBuilder.andWhere("blog.categoryId = :categoryId", { categoryId: queryDto.categoryId });
|
||||
}
|
||||
|
||||
queryBuilder.skip(skip).take(limit).orderBy("blog.createdAt", "DESC");
|
||||
return queryBuilder.getManyAndCount();
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
async getBlogListForAdmin(queryDto: BlogListSearchQueryDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const queryBuilder = this.createQueryBuilder("blog")
|
||||
.where("blog.deletedAt IS NULL")
|
||||
.leftJoinAndSelect("blog.category", "category")
|
||||
.leftJoinAndSelect("blog.author", "author");
|
||||
|
||||
if (queryDto.isActive) {
|
||||
queryBuilder.andWhere("blog.isActive = :isActive", { isActive: queryDto.isActive === 1 });
|
||||
}
|
||||
|
||||
if (queryDto.q) {
|
||||
// queryBuilder.andWhere(
|
||||
// "blog.title ILIKE :q OR blog.previewContent ILIKE :q OR blog.content ILIKE :q OR blog.metaTitle ILIKE :q OR blog.slug ILIKE :q",
|
||||
// { q: `%${queryDto.q}%` },
|
||||
// );
|
||||
|
||||
queryBuilder.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where("blog.title ILIKE :q", { q: `%${queryDto.q}%` })
|
||||
.orWhere("blog.previewContent ILIKE :q", { q: `%${queryDto.q}%` })
|
||||
.orWhere("blog.content ILIKE :q", { q: `%${queryDto.q}%` })
|
||||
.orWhere("blog.metaTitle ILIKE :q", { q: `%${queryDto.q}%` })
|
||||
.orWhere("blog.slug ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if (queryDto.categoryId) {
|
||||
queryBuilder.andWhere("blog.categoryId = :categoryId", { categoryId: queryDto.categoryId });
|
||||
}
|
||||
|
||||
if (queryDto.since) {
|
||||
queryBuilder.andWhere("blog.createdAt >= :since", { since: queryDto.since });
|
||||
}
|
||||
|
||||
queryBuilder.skip(skip).take(limit).orderBy("blog.createdAt", "DESC");
|
||||
|
||||
return queryBuilder.getManyAndCount();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user