|
|
@@ -8,7 +8,6 @@ import { PageRequest } from '../common/dto/page-request'
|
|
|
import { Comment } from './entities/comment.entity'
|
|
|
import { CommentDto } from './dto/comment.dto'
|
|
|
import { LikesService } from 'src/likes/likes.service'
|
|
|
-import { string } from 'yup'
|
|
|
|
|
|
@Injectable()
|
|
|
export class CommentService {
|
|
|
@@ -61,7 +60,14 @@ export class CommentService {
|
|
|
const childList = await this.commentRepository.createQueryBuilder()
|
|
|
.where({
|
|
|
rootCommentId: In(ids),
|
|
|
- }).getMany();
|
|
|
+ })
|
|
|
+ .andWhere(`(
|
|
|
+ SELECT COUNT(*)
|
|
|
+ FROM comment AS c2
|
|
|
+ WHERE c2.rootCommentId = comment.rootCommentId AND c2.createdAt <= comment.createdAt
|
|
|
+ ) <= 2`)
|
|
|
+ .orderBy("comment.rootCommentId, comment.createdAt", "ASC")
|
|
|
+ .getMany();
|
|
|
|
|
|
childList.forEach(comment => {
|
|
|
if (Boolean(comment.rootCommentId)) {
|
|
|
@@ -87,9 +93,48 @@ export class CommentService {
|
|
|
}
|
|
|
|
|
|
public async create(commentDto: CommentDto) {
|
|
|
+ if (commentDto.rootCommentId) {
|
|
|
+ const list: CommentDto[] = []
|
|
|
+ const root = await this.commentRepository.findOneBy({ id: commentDto.toCommentId })
|
|
|
+ commentDto.replyUserName = root.userName
|
|
|
+ root.replyNum += 1
|
|
|
+ list.push(root)
|
|
|
+ if (commentDto.rootCommentId != commentDto.toCommentId) {
|
|
|
+ const root = await this.commentRepository.findOneBy({ id: commentDto.rootCommentId })
|
|
|
+ root.replyNum += 1
|
|
|
+ list.push(root)
|
|
|
+ }
|
|
|
+ await this.commentRepository.save(list)
|
|
|
+ }
|
|
|
return await this.commentRepository.save(commentDto);
|
|
|
}
|
|
|
|
|
|
+ public async findChildList(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}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return await paginate<Comment>(query, { ...req.page })
|
|
|
+ }
|
|
|
+
|
|
|
public async delete(id: number): Promise<void> {
|
|
|
const comment = await this.commentRepository.findOneBy({ id: +id })
|
|
|
if (comment) {
|