update:add ci cd files ==> do not edit those file
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { getAllBlogCategoriesObj } from '../../../DTO/blogCategory.dto';
|
||||
import { CategoryDataAccess } from '../../../dataAccess/blogCategory.dataAccess';
|
||||
import { ItemStatus } from '../../../common/eNums/itemStatus.enum';
|
||||
import { BlogPostCommentDataAccess } from '../../../dataAccess/blogPostComment.dataAccess';
|
||||
import { BlogPostDataAccess } from '../../../dataAccess/blogPost.dataAccess';
|
||||
import { PostVisitDataAccess } from '../../../dataAccess/postVisit.dataAccess';
|
||||
import {
|
||||
postInfoObjDto,
|
||||
viewPostObjDto,
|
||||
ViewListPostDto,
|
||||
} from '../../../DTO/blogPost.dto';
|
||||
import commentStatuses from '../../../common/eNums/itemStatus.enum';
|
||||
|
||||
@Injectable()
|
||||
export class BlogService {
|
||||
constructor(
|
||||
private readonly categoryDataAccess: CategoryDataAccess,
|
||||
private readonly blogPostCommentDataAccess: BlogPostCommentDataAccess,
|
||||
private readonly blogPostDataAccess: BlogPostDataAccess,
|
||||
private readonly postVisitDataAccess: PostVisitDataAccess,
|
||||
) {}
|
||||
// blogCategory ******************************************************
|
||||
async listPost(query) {
|
||||
const listPost = await this.blogPostDataAccess.listBlogPosts(query);
|
||||
return listPost.map((item) => viewPostObjDto(item));
|
||||
}
|
||||
// post *****************************************************************
|
||||
async post(slug, user, ip) {
|
||||
const post = await this.blogPostDataAccess.findBlogPostBySlug(slug);
|
||||
if (!post) {
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.NOT_FOUND,
|
||||
error: 'POST_NOT_FOUND',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
if (!user) {
|
||||
user = { id: null };
|
||||
}
|
||||
|
||||
const visit = await this.postVisitDataAccess.findById(
|
||||
user.id,
|
||||
post._id,
|
||||
ip,
|
||||
);
|
||||
if (!visit) {
|
||||
if (user) {
|
||||
await this.blogPostDataAccess.updateVisitCount(
|
||||
post._id,
|
||||
post.visitCount + 1,
|
||||
);
|
||||
await this.postVisitDataAccess.create(user.id, post._id, ip);
|
||||
} else {
|
||||
user = { id: null };
|
||||
await this.blogPostDataAccess.updateVisitCount(
|
||||
post._id,
|
||||
post.visitCount + 1,
|
||||
);
|
||||
await this.postVisitDataAccess.create(user.id, post._id, ip);
|
||||
}
|
||||
}
|
||||
// await this.blogPostDataAccess.updateVisitCount(post.id, post.visitCount + 1);
|
||||
if (post.Comment.length !== 0) {
|
||||
post.Comment = post.Comment.filter(
|
||||
(x) => x.status === commentStatuses.Active,
|
||||
);
|
||||
} else {
|
||||
post.Comment = [];
|
||||
}
|
||||
return postInfoObjDto(post);
|
||||
}
|
||||
}
|
||||
// create postComment ***************************************************
|
||||
async createPostComment(userInf, createPostCommentDto, id): Promise<boolean> {
|
||||
const { text } = createPostCommentDto;
|
||||
const post = await this.blogPostDataAccess.findById(id);
|
||||
if (!post || post.status !== ItemStatus.Active) {
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.NOT_FOUND,
|
||||
error: 'پست وجود ندارد',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
const comment =
|
||||
await this.blogPostCommentDataAccess.createBlogPostComment(
|
||||
id,
|
||||
userInf.id,
|
||||
text,
|
||||
);
|
||||
post.Comment.push(comment._id ? comment._id : comment.id);
|
||||
post.save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// blogCat structure ******************************************************
|
||||
async getAllBlogCategories() {
|
||||
const list = await this.categoryDataAccess.getAll();
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
list[i].parents = list[i].parents.filter(
|
||||
(x) => x.status === ItemStatus.Active,
|
||||
);
|
||||
}
|
||||
return list.map((item) => getAllBlogCategoriesObj(item));
|
||||
}
|
||||
// replay post comment *******************************************************
|
||||
async replyPostComment(userInf, createPostCommentDto, id): Promise<boolean> {
|
||||
const postComment = await this.blogPostCommentDataAccess.findById(id);
|
||||
if (!postComment || postComment.status !== ItemStatus.Active) {
|
||||
throw new HttpException(
|
||||
{
|
||||
status: HttpStatus.NOT_FOUND,
|
||||
error: 'کامنت وجود ندارد',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
const { text } = createPostCommentDto;
|
||||
const replyComment = await this.blogPostCommentDataAccess.replyComment(
|
||||
postComment.blogPost._id,
|
||||
id,
|
||||
userInf.id,
|
||||
text,
|
||||
);
|
||||
postComment.Replies.push(replyComment.id);
|
||||
postComment.save();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// search blog *************************************************************
|
||||
async search(query): Promise<ViewListPostDto[]> {
|
||||
const { title, page, pageSize } = query;
|
||||
const resultPosts = await this.blogPostDataAccess.search(
|
||||
title,
|
||||
page,
|
||||
pageSize,
|
||||
);
|
||||
return resultPosts.map((item) => viewPostObjDto(item));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user