Просмотр исходного кода

Merge branch 'comment' of xiongzhu/chat-api into master

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

+ 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 { }

+ 3 - 0
src/chat-role/entities/chat-role.entity.ts

@@ -14,6 +14,9 @@ export class ChatRole {
     @Column()
     describe: string
 
+    @Column()
+    pic: String
+
     @Column()
     chatted: number
 

+ 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
 

+ 10 - 1
src/moments/moments.controller.ts

@@ -1,4 +1,4 @@
-import { Body, Controller, Post } from "@nestjs/common";
+import { BadRequestException, Body, Controller, Get, Param, Post } from "@nestjs/common";
 import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
 import { MomentsService } from "./moments.service";
 import { PageRequest } from "src/common/dto/page-request";
@@ -16,4 +16,13 @@ export class MomentsController {
         return await this.momentsService.findAll(page)
     }
 
+    @Get('/get/:id')
+    public async get(@Param('id') id: string) {
+        try {
+            return await this.momentsService.findById(Number(id))
+        } catch (err) {
+            throw  new BadRequestException(err, 'Error: Moments not existent!')
+        }
+    }
+
 }

+ 5 - 10
src/moments/moments.service.ts

@@ -14,6 +14,7 @@ import { Moments } from './entities/moments.entity'
 import { paginate, Pagination } from 'nestjs-typeorm-paginate'
 import { PageRequest } from '../common/dto/page-request'
 import { ChatRoleService } from 'src/chat-role/chat-role.service'
+import { ro } from 'date-fns/locale'
 
 @Injectable()
 export class MomentsService {
@@ -40,16 +41,10 @@ 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`)
-        }
-
+    async findById(id: number): Promise<Moments> {
+        const moments = await this.momentsRepository.findOneBy({ id: +id })
+        const chatRole = await this.chatRoleService.findById(moments.userId)
+        moments.chatRole = chatRole
         return moments
     }