wuyi 2 лет назад
Родитель
Сommit
fea2da990a

+ 4 - 2
src/app.module.ts

@@ -23,6 +23,7 @@ import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
 import { ChatRoleModule } from './chat-role/chat-role.module'
 import { LabelModule } from './label/label.module'
 import { MomentsModule } from './moments/moments.module'
+import { CommentModule } from './comment/comment.module'
 @Module({
     imports: [
         DevtoolsModule.register({
@@ -83,7 +84,8 @@ import { MomentsModule } from './moments/moments.module'
         SysConfigModule,
         ChatRoleModule,
         LabelModule,
-        MomentsModule
+        MomentsModule,
+        CommentModule
     ],
     controllers: [],
     providers: [
@@ -93,4 +95,4 @@ import { MomentsModule } from './moments/moments.module'
         }
     ]
 })
-export class AppModule {}
+export class AppModule { }

+ 31 - 0
src/comment/comment.controller.ts

@@ -0,0 +1,31 @@
+import {
+    Controller,
+    Put,
+    Get,
+    Body,
+    Param,
+    HttpStatus,
+    NotFoundException,
+    Delete,
+    BadRequestException,
+    Req,
+    Post
+} from '@nestjs/common'
+import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
+import { CommentService } from './comment.service'
+import { Comment } from './entities/comment.entity'
+import { PageRequest } from 'src/common/dto/page-request'
+
+@ApiTags('comment')
+@Controller('/comment')
+@ApiBearerAuth()
+export class CommentController {
+
+    constructor(private readonly commentService: CommentService) { }
+
+    @Post('/:id')
+    public async list(@Body() page: PageRequest<Comment>, @Param('id') id: number) {
+        return await this.commentService.findAll(page,id)
+    }
+
+}

+ 13 - 0
src/comment/comment.module.ts

@@ -0,0 +1,13 @@
+import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+import { Comment } from './entities/comment.entity'
+import { CommentService } from './comment.service'
+import { CommentController } from './comment.controller'
+
+@Module({
+    imports: [TypeOrmModule.forFeature([Comment])],
+    controllers: [CommentController],
+    providers: [CommentService],
+    exports: [CommentService]
+})
+export class CommentModule { }

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

@@ -0,0 +1,32 @@
+import {
+    Injectable
+} from '@nestjs/common'
+import { 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'
+
+@Injectable()
+export class CommentService {
+
+    constructor(
+        @InjectRepository(Comment)
+        private readonly commentRepository: Repository<Comment>
+    ) { }
+
+
+    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 })
+
+        // if (req.search) {
+        //     query.where(req.search)
+        // }
+    
+        const options = { ...req.page }
+
+        return await paginate<Comment>(query, options)
+    }
+
+}

+ 36 - 0
src/comment/entities/comment.entity.ts

@@ -0,0 +1,36 @@
+import { Column, CreateDateColumn, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
+
+@Entity()
+export class Comment {
+
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    content: string
+
+    @Column()
+    userId: number
+
+    @Column()
+    momentsId: number
+
+    @Column({ nullable: true })
+    rootCommentId: number
+
+    @Column({ nullable: true })
+    toCommentId: number
+
+    @Column({ default: 0 })
+    commentLikeCount: number
+
+    @Column({ default: false })
+    isTop: boolean
+
+    @Column({ default: false })
+    isDel: boolean
+
+    @CreateDateColumn()
+    createdAt: Date
+
+}

+ 3 - 0
src/moments/entities/moments.entity.ts

@@ -16,6 +16,9 @@ export class Moments {
     @Column()
     content: string
 
+    @Column({default: 0})
+    momentsLikeCount: number
+
     @CreateDateColumn()
     createdAt: Date
 

+ 0 - 13
src/moments/moments.service.ts

@@ -40,17 +40,4 @@ export class MomentsService {
         return page
     }
 
-
-    public async findById(id: number): Promise<Moments> {
-        const moments = await this.momentsRepository.findOneBy({
-            id: +id
-        })
-
-        if (!moments) {
-            throw new NotFoundException(`Monents #${id} not found`)
-        }
-
-        return moments
-    }
-
 }