|
|
@@ -1,11 +1,14 @@
|
|
|
import {
|
|
|
Injectable
|
|
|
} from '@nestjs/common'
|
|
|
-import { In, Repository, UpdateResult } from 'typeorm'
|
|
|
+import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { paginate, Pagination } from 'nestjs-typeorm-paginate'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
import { Comment } from './entities/comment.entity'
|
|
|
+import { number } from 'yup'
|
|
|
+import { CommentDto } from './dto/comment.dto'
|
|
|
+import { el, ro } from 'date-fns/locale'
|
|
|
|
|
|
@Injectable()
|
|
|
export class CommentService {
|
|
|
@@ -16,17 +19,65 @@ export class CommentService {
|
|
|
) { }
|
|
|
|
|
|
|
|
|
- async findAll(req: PageRequest<Comment>, momentsId: number): Promise<Pagination<Comment>> {
|
|
|
- const query = await this.commentRepository.createQueryBuilder('comment')
|
|
|
- .where('(comment.momentsId = :momentsId and comment.rootCommentId is null) or (comment.rootCommentId in (select id from Comment where momentsId = :momentsId and rootCommentId is null))', { momentsId })
|
|
|
+ async findAll(req: PageRequest<Comment>): Promise<Pagination<Comment>> {
|
|
|
+
|
|
|
+ const query = await this.commentRepository.createQueryBuilder()
|
|
|
+ .where('comment.isDel = false')
|
|
|
+
|
|
|
+ if (req.search) {
|
|
|
+ const searchOptions = req.search as FindManyOptions<Comment>;
|
|
|
+ if ('where' in req.search) {
|
|
|
+ if ('where' in searchOptions && searchOptions.where) {
|
|
|
+ for (const [key, value] of Object.entries(searchOptions.where)) {
|
|
|
+ query.andWhere(`comment.${key} = ${value}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ query.orderBy('comment.isTop', 'DESC')
|
|
|
+
|
|
|
+ if ('order' in req.search) {
|
|
|
+ if ('order' in searchOptions && searchOptions.order) {
|
|
|
+ for (const [key, value] of Object.entries(searchOptions.order)) {
|
|
|
+ query.addOrderBy(`comment.${key}`, value as 'ASC' | 'DESC');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // if (req.search) {
|
|
|
- // query.where(req.search)
|
|
|
- // }
|
|
|
-
|
|
|
const options = { ...req.page }
|
|
|
+ const page = await paginate<Comment>(query, options)
|
|
|
+
|
|
|
+ const { items, meta } = page;
|
|
|
+ const rootMap = new Map<number, Comment>()
|
|
|
+
|
|
|
+ items.forEach(comment => {
|
|
|
+ if (!Boolean(comment.rootCommentId)) {
|
|
|
+ rootMap.set(comment.id, comment)
|
|
|
+ } else {
|
|
|
+ const root = rootMap.get(comment.rootCommentId)
|
|
|
+ if (Boolean(root.childList)) {
|
|
|
+ root.childList.push(comment)
|
|
|
+ } else {
|
|
|
+ const childList: Comment[] = []
|
|
|
+ childList.push(comment)
|
|
|
+ root.childList = childList
|
|
|
+ }
|
|
|
+ rootMap.set(root.id, root)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const newItems = Array.from(rootMap.values());
|
|
|
+
|
|
|
+ const result: Pagination<Comment> = {
|
|
|
+ items: newItems,
|
|
|
+ meta
|
|
|
+ };
|
|
|
+
|
|
|
+ return result
|
|
|
+ }
|
|
|
|
|
|
- return await paginate<Comment>(query, options)
|
|
|
+ public async create(commentDto: CommentDto) {
|
|
|
+ return await this.commentRepository.save(commentDto);
|
|
|
}
|
|
|
|
|
|
}
|