xiongzhu 2 лет назад
Родитель
Сommit
8a8fbb0b63

+ 9 - 3
src/comment/comment.module.ts

@@ -1,4 +1,4 @@
-import { Module } from '@nestjs/common'
+import { Module, forwardRef } from '@nestjs/common'
 import { TypeOrmModule } from '@nestjs/typeorm'
 import { Comment } from './entities/comment.entity'
 import { CommentService } from './comment.service'
@@ -7,11 +7,17 @@ import { LikesModule } from 'src/likes/likes.module'
 import { Likes } from 'src/likes/entities/likes.entity'
 import { Moments } from 'src/moments/entities/moments.entity'
 import { MomentsModule } from 'src/moments/moments.module'
+import { ConditionModule } from 'src/condition/condition.module'
 
 @Module({
-    imports: [TypeOrmModule.forFeature([Comment, Likes, Moments]), LikesModule, MomentsModule],
+    imports: [
+        TypeOrmModule.forFeature([Comment, Likes, Moments]),
+        LikesModule,
+        MomentsModule,
+        forwardRef(() => ConditionModule)
+    ],
     controllers: [CommentController],
     providers: [CommentService],
     exports: [CommentService]
 })
-export class CommentModule { }
+export class CommentModule {}

+ 32 - 32
src/comment/comment.service.ts

@@ -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)
         }
     }
-
-}
+}

+ 8 - 3
src/condition/condition.module.ts

@@ -1,4 +1,4 @@
-import { Module } from '@nestjs/common'
+import { Module, forwardRef } from '@nestjs/common'
 import { TypeOrmModule } from '@nestjs/typeorm'
 import { Moments } from 'src/moments/entities/moments.entity'
 import { Comment } from 'src/comment/entities/comment.entity'
@@ -10,9 +10,14 @@ import { CommentModule } from 'src/comment/comment.module'
 import { ChatModule } from 'src/chat/chat.module'
 
 @Module({
-    imports: [TypeOrmModule.forFeature([Condition, Moments, Comment]), MomentsModule, CommentModule, ChatModule],
+    imports: [
+        TypeOrmModule.forFeature([Condition, Moments, Comment]),
+        MomentsModule,
+        forwardRef(() => CommentModule),
+        ChatModule
+    ],
     controllers: [ConditionController],
     providers: [ConditionService],
     exports: [ConditionService]
 })
-export class ConditionModule { }
+export class ConditionModule {}

+ 14 - 16
src/condition/condition.service.ts

@@ -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'
@@ -15,7 +13,6 @@ import { ConditionDto } from './dto/condition.dto'
 
 @Injectable()
 export class ConditionService {
-
     constructor(
         @InjectRepository(Condition)
         private readonly conditionRepository: Repository<Condition>,
@@ -24,28 +21,31 @@ export class ConditionService {
         @InjectRepository(Comment)
         private readonly commentRepository: Repository<Comment>,
         private readonly chatService: ChatService,
+        @Inject(forwardRef(() => CommentService))
         private readonly commentService: CommentService
-    ) { }
-
-    public async create() {
+    ) {}
 
-    }
+    public async create() {}
 
     public async reply(comment: Comment, moments: Moments) {
         const chat = await this.chatService.sendMessage(null, moments.messageId, comment.content)
 
         if (chat.message.trim() !== '') {
-
-            const favorabilityChat = await this.chatService.sendMessage(null, chat.messageId, "这是我的评论<" + comment.content + ">,请你就这条评论打分,分数在0到10这个区间,返回数字,不必做解释,不要出现文字")
+            const favorabilityChat = await this.chatService.sendMessage(
+                null,
+                chat.messageId,
+                '这是我的评论<' +
+                    comment.content +
+                    '>,请你就这条评论打分,分数在0到10这个区间,返回数字,不必做解释,不要出现文字'
+            )
             const fraction = favorabilityChat.message
 
-            const dto = new ConditionDto
+            const dto = new ConditionDto()
             dto.userId = comment.userId
             dto.favorability = parseFloat(fraction)
             dto.chatRoleId = moments.userId
 
-
-            const commentDto = new CommentDto
+            const commentDto = new CommentDto()
             commentDto.content = chat.message
             commentDto.userId = comment.userId
             commentDto.userName = comment.userName
@@ -56,7 +56,5 @@ export class ConditionService {
             const replyComment = await this.commentService.create(commentDto)
             await this.conditionRepository.save(dto)
         }
-
     }
-
-}
+}