|
|
@@ -1,6 +1,4 @@
|
|
|
-import {
|
|
|
- Injectable
|
|
|
-} from '@nestjs/common'
|
|
|
+import { Inject, Injectable, forwardRef } from '@nestjs/common'
|
|
|
import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { paginate, Pagination } from 'nestjs-typeorm-paginate'
|
|
|
@@ -14,7 +12,6 @@ import { ConditionService } from 'src/condition/condition.service'
|
|
|
|
|
|
@Injectable()
|
|
|
export class CommentService {
|
|
|
-
|
|
|
constructor(
|
|
|
@InjectRepository(Comment)
|
|
|
private readonly commentRepository: Repository<Comment>,
|
|
|
@@ -23,21 +20,22 @@ export class CommentService {
|
|
|
@InjectRepository(Moments)
|
|
|
private readonly momentsRepository: Repository<Moments>,
|
|
|
private readonly likesService: LikesService,
|
|
|
+ @Inject(forwardRef(() => ConditionService))
|
|
|
private readonly conditionService: ConditionService
|
|
|
- ) { }
|
|
|
-
|
|
|
+ ) {}
|
|
|
|
|
|
async findAll(req: PageRequest<Comment>, userId: number): Promise<Pagination<Comment>> {
|
|
|
- const query = await this.commentRepository.createQueryBuilder()
|
|
|
+ const query = await this.commentRepository
|
|
|
+ .createQueryBuilder()
|
|
|
.where('comment.isDel = false')
|
|
|
.andWhere('rootCommentId is null')
|
|
|
|
|
|
if (req.search) {
|
|
|
- const searchOptions = req.search as FindManyOptions<Comment>;
|
|
|
+ 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.andWhere(`comment.${key} = ${value}`)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -47,7 +45,7 @@ export class CommentService {
|
|
|
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');
|
|
|
+ query.addOrderBy(`comment.${key}`, value as 'ASC' | 'DESC')
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -56,32 +54,36 @@ export class CommentService {
|
|
|
const options = { ...req.page }
|
|
|
const page = await paginate<Comment>(query, options)
|
|
|
|
|
|
- const { items, meta } = page;
|
|
|
+ const { items, meta } = page
|
|
|
const rootMap = new Map<number, Comment>()
|
|
|
const ids: number[] = []
|
|
|
|
|
|
- items.forEach(comment => {
|
|
|
+ items.forEach((comment) => {
|
|
|
rootMap.set(comment.id, comment)
|
|
|
ids.push(comment.id)
|
|
|
})
|
|
|
|
|
|
- const childList = await this.commentRepository.createQueryBuilder()
|
|
|
+ const childList = await this.commentRepository
|
|
|
+ .createQueryBuilder()
|
|
|
.where({
|
|
|
- rootCommentId: In(ids),
|
|
|
+ rootCommentId: In(ids)
|
|
|
})
|
|
|
- .andWhere(`(
|
|
|
+ .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();
|
|
|
+ ) <= 2`
|
|
|
+ )
|
|
|
+ .orderBy('comment.rootCommentId, comment.createdAt', 'ASC')
|
|
|
+ .getMany()
|
|
|
|
|
|
- childList.forEach(comment => {
|
|
|
+ childList.forEach((comment) => {
|
|
|
ids.push(comment.id)
|
|
|
})
|
|
|
|
|
|
- const likes = await this.likesRepository.createQueryBuilder('likes')
|
|
|
+ const likes = await this.likesRepository
|
|
|
+ .createQueryBuilder('likes')
|
|
|
.select('likes.targetId')
|
|
|
.where({
|
|
|
userId: +userId,
|
|
|
@@ -90,9 +92,9 @@ export class CommentService {
|
|
|
isValid: true
|
|
|
})
|
|
|
.getMany()
|
|
|
- const targetIds: number[] = likes.map((like) => like.targetId);
|
|
|
+ const targetIds: number[] = likes.map((like) => like.targetId)
|
|
|
|
|
|
- childList.forEach(comment => {
|
|
|
+ childList.forEach((comment) => {
|
|
|
if (targetIds.includes(comment.id)) {
|
|
|
comment.isLiked = true
|
|
|
} else {
|
|
|
@@ -111,15 +113,15 @@ export class CommentService {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- const newItems = Array.from(rootMap.values());
|
|
|
- newItems.forEach(comment => {
|
|
|
+ const newItems = Array.from(rootMap.values())
|
|
|
+ newItems.forEach((comment) => {
|
|
|
comment.isLiked = targetIds.includes(comment.id) ? true : false
|
|
|
})
|
|
|
|
|
|
const result: Pagination<Comment> = {
|
|
|
items: newItems,
|
|
|
meta
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
return result
|
|
|
}
|
|
|
@@ -151,15 +153,14 @@ export class CommentService {
|
|
|
}
|
|
|
|
|
|
public async findChildList(req: PageRequest<Comment>): Promise<Pagination<Comment>> {
|
|
|
- const query = await this.commentRepository.createQueryBuilder()
|
|
|
- .where('comment.isDel = false')
|
|
|
+ const query = await this.commentRepository.createQueryBuilder().where('comment.isDel = false')
|
|
|
|
|
|
if (req.search) {
|
|
|
- const searchOptions = req.search as FindManyOptions<Comment>;
|
|
|
+ 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.andWhere(`comment.${key} = ${value}`)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -167,7 +168,7 @@ export class CommentService {
|
|
|
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');
|
|
|
+ query.addOrderBy(`comment.${key}`, value as 'ASC' | 'DESC')
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -199,5 +200,4 @@ export class CommentService {
|
|
|
await this.commentRepository.save(comment)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+}
|