chore: add blog blog comment and blog category crud
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import slugify from "slugify";
|
||||
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 { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "../DTO/update-blog.dto";
|
||||
import { UpdateCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "../DTO/update-comment-status.dto";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
import { BlogCategoriesRepository } from "../repositories/blog-categories.repository";
|
||||
import { BlogCommentsRepository } from "../repositories/blog-comments.repository";
|
||||
import { BlogsRepository } from "../repositories/blogs.repository";
|
||||
@Injectable()
|
||||
export class BlogsService {
|
||||
constructor(
|
||||
private readonly blogsRepository: BlogsRepository,
|
||||
private readonly blogCommentsRepository: BlogCommentsRepository,
|
||||
private readonly blogCategoriesRepository: BlogCategoriesRepository,
|
||||
private readonly usersService: UsersService,
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
async createCategory(createCategoryDto: CreateCategoryDto) {
|
||||
const existingCategory = await this.blogCategoriesRepository.findOneBy({ title: createCategoryDto.title });
|
||||
if (existingCategory) throw new BadRequestException(BlogMessage.CATEGORY_ALREADY_EXISTS);
|
||||
|
||||
const category = await this.blogCategoriesRepository.createCategory(createCategoryDto);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
category,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async updateCategory(id: string, updateCategoryDto: UpdateCategoryDto) {
|
||||
const category = await this.findCategoryById(id);
|
||||
|
||||
if (updateCategoryDto.title) {
|
||||
await this.checkCategoryExists(updateCategoryDto.title, id);
|
||||
}
|
||||
|
||||
await this.blogCategoriesRepository.save({ ...category, ...updateCategoryDto });
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
category,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async deleteCategory(id: string) {
|
||||
await this.findCategoryById(id);
|
||||
|
||||
await this.blogCategoriesRepository.softDelete(id);
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async toggleCategoryStatus(id: string) {
|
||||
const category = await this.findCategoryById(id);
|
||||
|
||||
//
|
||||
category.isActive = !category.isActive;
|
||||
await this.blogCategoriesRepository.save(category);
|
||||
|
||||
//
|
||||
return { message: CommonMessage.UPDATE_SUCCESS, category };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getCategory(id: string) {
|
||||
const category = await this.findCategoryById(id);
|
||||
return { category };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getCategoriesUserSide() {
|
||||
const categories = await this.blogCategoriesRepository.getCategoriesUserSide();
|
||||
return { categories };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getCategories(queryDto: CategoryListSearchQueryDto) {
|
||||
const [categories, count] = await this.blogCategoriesRepository.getCategories(queryDto);
|
||||
return { categories, count, paginate: true };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async createBlog(createBlogDto: CreateBlogDto, authorId: string) {
|
||||
const existingBlog = await this.blogsRepository.findOne({ where: { title: createBlogDto.title } });
|
||||
if (existingBlog) throw new BadRequestException(BlogMessage.BLOG_ALREADY_EXISTS);
|
||||
|
||||
const category = await this.findCategoryById(createBlogDto.categoryId);
|
||||
|
||||
const slug = this.generateSlug(createBlogDto.title);
|
||||
|
||||
const blog = this.blogsRepository.create({
|
||||
...createBlogDto,
|
||||
slug,
|
||||
author: { id: authorId },
|
||||
category,
|
||||
});
|
||||
|
||||
await this.blogsRepository.save(blog);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
blog,
|
||||
};
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async updateBlog(id: string, updateBlogDto: UpdateBlogDto) {
|
||||
const blog = await this.findBlogById(id);
|
||||
|
||||
if (updateBlogDto.title) {
|
||||
const existBlog = await this.blogsRepository.findOne({ where: { title: updateBlogDto.title, id: Not(id) } });
|
||||
if (existBlog) throw new BadRequestException(BlogMessage.BLOG_ALREADY_EXISTS);
|
||||
blog.slug = this.generateSlug(updateBlogDto.title);
|
||||
}
|
||||
|
||||
if (updateBlogDto.categoryId) {
|
||||
const category = await this.findCategoryById(updateBlogDto.categoryId);
|
||||
blog.category = category;
|
||||
}
|
||||
|
||||
await this.blogsRepository.save({ ...blog, ...updateBlogDto });
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
blog,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async deleteBlog(id: string) {
|
||||
await this.findBlogById(id);
|
||||
|
||||
await this.blogsRepository.softDelete(id);
|
||||
return { message: CommonMessage.DELETED };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogById(id: string) {
|
||||
const blog = await this.findBlogById(id);
|
||||
return { blog };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogBySlug(slug: string) {
|
||||
const blog = await this.blogsRepository.findOneBySlug(slug);
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
return { blog };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogsPublic(queryDto: BlogSearchQueryDto) {
|
||||
const blogs = await this.blogsRepository.getBlogsForUserSide(queryDto);
|
||||
return { blogs };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogList(queryDto: BlogListSearchQueryDto) {
|
||||
const [blogs, count] = await this.blogsRepository.getBlogListForAdmin(queryDto);
|
||||
return { blogs, count, paginate: true };
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async createComment(blogId: string, createCommentDto: CreateCommentDto, userId: string) {
|
||||
const blog = await this.findBlogById(blogId);
|
||||
|
||||
const { user } = await this.usersService.findOneById(userId);
|
||||
|
||||
const comment = this.blogCommentsRepository.create({
|
||||
...createCommentDto,
|
||||
blog,
|
||||
user,
|
||||
status: CommentStatus.PENDING,
|
||||
});
|
||||
|
||||
await this.blogCommentsRepository.save(comment);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
comment,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async updateCommentStatus(commentId: string, updateCommentStatusDto: UpdateCommentStatusDto) {
|
||||
const comment = await this.blogCommentsRepository.findOneById(commentId);
|
||||
if (!comment) throw new BadRequestException(BlogMessage.COMMENT_NOT_FOUND);
|
||||
|
||||
comment.status = updateCommentStatusDto.status;
|
||||
await this.blogCommentsRepository.save(comment);
|
||||
|
||||
return {
|
||||
message: CommonMessage.UPDATE_SUCCESS,
|
||||
comment,
|
||||
};
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async getBlogComments(blogId: string) {
|
||||
const comments = await this.blogCommentsRepository.findAllByBlogId(blogId);
|
||||
return { comments };
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private generateSlug(title: string): string {
|
||||
return slugify(title, { lower: true, strict: true });
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private async checkCategoryExists(title: string, id?: string) {
|
||||
const existingCategory = await this.blogCategoriesRepository.findOneBy({ title, ...(id && { id: Not(id) }) });
|
||||
if (existingCategory) throw new BadRequestException(BlogMessage.CATEGORY_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private async findCategoryById(id: string) {
|
||||
const category = await this.blogCategoriesRepository.findOneById(id);
|
||||
if (!category) throw new BadRequestException(BlogMessage.CATEGORY_NOT_FOUND);
|
||||
return category;
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
private async findBlogById(id: string) {
|
||||
const blog = await this.blogsRepository.findOneById(id);
|
||||
if (!blog) throw new BadRequestException(BlogMessage.BLOG_NOT_FOUND);
|
||||
return blog;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user