Forráskód Böngészése

优化数据库新增
新增板块模块
新增帖子模块

枫叶秋林 2 éve
szülő
commit
a9eb4cf643

+ 51 - 0
prisma/migrations/20221220073355_init_post_comment_pleate/migration.sql

@@ -0,0 +1,51 @@
+-- DropForeignKey
+ALTER TABLE `userinfo` DROP FOREIGN KEY `userinfo_authId_fkey`;
+
+-- CreateTable
+CREATE TABLE `post` (
+    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+    `title` VARCHAR(191) NOT NULL,
+    `content` VARCHAR(191) NOT NULL,
+    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
+    `updatedAt` DATETIME(3) NOT NULL,
+    `authorId` INTEGER UNSIGNED NOT NULL,
+    `plateId` INTEGER UNSIGNED NOT NULL,
+
+    PRIMARY KEY (`id`)
+) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
+
+-- CreateTable
+CREATE TABLE `comment` (
+    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+    `content` VARCHAR(191) NOT NULL,
+    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
+    `updatedAt` DATETIME(3) NOT NULL,
+    `authorId` INTEGER UNSIGNED NOT NULL,
+    `postId` INTEGER UNSIGNED NOT NULL,
+
+    PRIMARY KEY (`id`)
+) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
+
+-- CreateTable
+CREATE TABLE `plate` (
+    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+    `name` VARCHAR(191) NOT NULL,
+
+    UNIQUE INDEX `plate_name_key`(`name`),
+    PRIMARY KEY (`id`)
+) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
+
+-- AddForeignKey
+ALTER TABLE `post` ADD CONSTRAINT `post_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `auth`(`auth_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE `post` ADD CONSTRAINT `post_plateId_fkey` FOREIGN KEY (`plateId`) REFERENCES `plate`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE `comment` ADD CONSTRAINT `comment_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `auth`(`auth_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE `comment` ADD CONSTRAINT `comment_postId_fkey` FOREIGN KEY (`postId`) REFERENCES `post`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE `userinfo` ADD CONSTRAINT `userinfo_authId_fkey` FOREIGN KEY (`authId`) REFERENCES `auth`(`auth_id`) ON DELETE RESTRICT ON UPDATE CASCADE;

+ 2 - 0
prisma/migrations/20221231004543_updata_post_plateid_add_default/migration.sql

@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE `post` MODIFY `plateId` INTEGER UNSIGNED NOT NULL DEFAULT 0;

+ 32 - 2
prisma/schema.prisma

@@ -7,11 +7,41 @@ datasource db {
   url      = env("DATABASE_URL")
 }
 model auth{
-  auth_id        Int   @id @default(autoincrement()) @db.UnsignedInt
+  auth_id   Int     @id @default(autoincrement()) @db.UnsignedInt
   username  String  @unique
   email     String  @unique
   password  String
   user      userinfo?
+  post      post[]
+  comment   comment[]
+}
+model post{
+  id        Int       @id @default(autoincrement()) @db.UnsignedInt
+  title     String
+  content   String
+  createdAt DateTime  @default(now())
+  updatedAt DateTime  @updatedAt
+  authorId  Int       @db.UnsignedInt
+  author    auth      @relation(fields: [authorId], references: [auth_id])
+  comment   comment[]
+  plateId   Int       @db.UnsignedInt @default(0)
+  plate     plate     @relation(fields: [plateId], references: [id])
+}
+model comment{
+  id        Int       @id @default(autoincrement()) @db.UnsignedInt
+  content   String
+  createdAt DateTime  @default(now())
+  updatedAt DateTime  @updatedAt
+  authorId  Int       @db.UnsignedInt
+  author    auth      @relation(fields: [authorId], references: [auth_id])
+  postId    Int       @db.UnsignedInt
+  post      post      @relation(fields: [postId], references: [id])
+}
+
+model plate{
+  id        Int       @id @default(autoincrement()) @db.UnsignedInt
+  name      String    @unique
+  post      post[]
 }
 model userinfo{
   id        Int       @id @default(autoincrement()) @db.UnsignedInt
@@ -20,7 +50,7 @@ model userinfo{
   level     Int       @default(0) @db.UnsignedInt
   createdAt DateTime  @default(now())
   authId    Int       @unique @db.UnsignedInt
-  user      auth      @relation(fields: [authId], references: [auth_id], onDelete: Cascade)
+  user      auth      @relation(fields: [authId], references: [auth_id])
 
 }
 

+ 4 - 0
src/app.module.ts

@@ -5,6 +5,8 @@ import { PrismaModule } from '@/prisma/prisma.module'
 import { CodModule } from './cod/cod.module';
 import { RedisModule } from './redis/redis.module';
 import { UserinfoModule } from './userinfo/userinfo.module';
+import { PostModule } from './post/post.module';
+import { PlateModule } from './plate/plate.module';
 @Module({
   imports: [
     AuthModule,
@@ -15,6 +17,8 @@ import { UserinfoModule } from './userinfo/userinfo.module';
     CodModule,
     RedisModule,
     UserinfoModule,
+    PostModule,
+    PlateModule,
   ],
   controllers: [],
   providers: [],

+ 1 - 5
src/auth/JwtStrategy.ts

@@ -17,10 +17,6 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
 
   //验证通过后获取用户资料
   async validate({ sub: id }) {
-    return this.prisma.auth.findUnique({
-      where: {
-        auth_id: id,
-      },
-    })
+    return id
   }
 }

+ 5 - 0
src/auth/auth.service.ts

@@ -16,6 +16,11 @@ export class AuthService {
         username: dto.name,
         password: paw,
         email: dto.emali,
+        user: {
+          create: {
+            avatar: '',
+          },
+        },
       },
     })
     delete user.password

+ 23 - 0
src/plate/plate.controller.ts

@@ -0,0 +1,23 @@
+import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
+import { PlateService } from './plate.service'
+
+@Controller('plate')
+export class PlateController {
+  constructor(private readonly postService: PlateService) {}
+  @Post('addolate')
+  async addplate(@Body() { plate }) {
+    return await this.postService.addplate(plate)
+  }
+  @Get('getplate')
+  async getplate() {
+    return await this.postService.getplate()
+  }
+  @Delete('deleteplate')
+  async deleteplate(@Body() plateid: number) {
+    return await this.postService.deleteplate(plateid)
+  }
+  @Put('updateplate')
+  async updateplate(@Body() { plateid, plate }) {
+    return await this.postService.updateplate(plateid, plate)
+  }
+}

+ 9 - 0
src/plate/plate.module.ts

@@ -0,0 +1,9 @@
+import { Module } from '@nestjs/common';
+import { PlateService } from './plate.service';
+import { PlateController } from './plate.controller';
+
+@Module({
+  providers: [PlateService],
+  controllers: [PlateController]
+})
+export class PlateModule {}

+ 40 - 0
src/plate/plate.service.ts

@@ -0,0 +1,40 @@
+import { PrismaService } from '@/prisma/prisma.service'
+import { Injectable } from '@nestjs/common'
+
+@Injectable()
+export class PlateService {
+  constructor(private readonly prisma: PrismaService) {}
+  addplate(plate: string) {
+    return this.prisma.plate.create({
+      data: {
+        name: plate,
+      },
+    })
+  }
+  async getplate() {
+    return await this.prisma.plate.findMany({
+      select: {
+        id: true,
+        name: true,
+      },
+    })
+  }
+  async deleteplate(plateid: number) {
+    return await this.prisma.plate.deleteMany({
+      where: {
+        id: plateid,
+      },
+    })
+  }
+
+  async updateplate(plateid: number, name: string) {
+    return await this.prisma.plate.updateMany({
+      where: {
+        id: plateid,
+      },
+      data: {
+        name,
+      },
+    })
+  }
+}

+ 30 - 0
src/post/post.controller.ts

@@ -0,0 +1,30 @@
+import { Body, Controller, Delete, Get, Post, Put, Query, Req, UseGuards } from '@nestjs/common'
+import { PostService } from './post.service'
+import { AuthGuard } from '@nestjs/passport'
+import { Request } from 'express'
+
+@Controller('post')
+export class PostController {
+  constructor(private readonly postService: PostService) {}
+  @Get('post')
+  async getpost(@Query('id') id: number) {
+    return await this.postService.getpost(id)
+  }
+  @Get('delete')
+  async delete(@Query('postid') postid: number) {
+    return await this.postService.delete(postid)
+  }
+  @Get('updated')
+  async updated(@Body() { postid, title, content, plateId }) {
+    return await this.postService.updated(postid, { title, content, plateId })
+  }
+  @Post('post')
+  @UseGuards(AuthGuard('jwt'))
+  async post(@Req() req: Request, @Body() { title, content, plateid }) {
+    return await this.postService.post(req.user as number, plateid, { title, content })
+  }
+  @Get('platelist')
+  async getpostlist(@Query('plateid') plateid: number) {
+    return await this.postService.getpostlist(plateid)
+  }
+}

+ 9 - 0
src/post/post.module.ts

@@ -0,0 +1,9 @@
+import { Module } from '@nestjs/common';
+import { PostService } from './post.service';
+import { PostController } from './post.controller';
+
+@Module({
+  providers: [PostService],
+  controllers: [PostController]
+})
+export class PostModule {}

+ 80 - 0
src/post/post.service.ts

@@ -0,0 +1,80 @@
+import { PrismaService } from '@/prisma/prisma.service'
+import { Injectable } from '@nestjs/common'
+
+@Injectable()
+export class PostService {
+  constructor(private readonly prisma: PrismaService) {}
+  async post(authId: number, plateid: number, { title, content }) {
+    return await this.prisma.post.create({
+      data: {
+        title,
+        content,
+        authorId: authId,
+        plateId: plateid,
+      },
+    })
+  }
+  async updated(postid: number, { title, content, plateId }) {
+    return await this.prisma.post.updateMany({
+      where: {
+        id: postid,
+      },
+      data: {
+        title,
+        content,
+        plateId,
+      },
+    })
+  }
+
+  async delete(postid: number) {
+    return await this.prisma.post.deleteMany({
+      where: {
+        id: postid,
+      },
+    })
+  }
+
+  async getpost(postid: number) {
+    return await this.prisma.post.findUnique({
+      where: {
+        id: postid,
+      },
+      include: {
+        author: true,
+        plate: true,
+        comment: true,
+      },
+    })
+  }
+
+  async getpostlist(plateid: number) {
+    const data = await this.prisma.post.findMany({
+      where: {
+        plateId: plateid,
+      },
+      select: {
+        id: true,
+        title: true,
+        authorId: true,
+      },
+    })
+    data.map(async (item) => {
+      const user = await this.prisma.auth.findUnique({
+        where: {
+          auth_id: item.authorId,
+        },
+        include: {
+          user: true,
+        },
+      })
+      delete user.password
+      delete user.user.authId
+      return {
+        ...item,
+        ...user,
+      }
+    })
+    return data
+  }
+}

+ 5 - 0
src/userinfo/dto/userinfo.dto.ts

@@ -0,0 +1,5 @@
+export default class userDateDto {
+  avatar: string
+  exp: number
+  level: number
+}

+ 18 - 3
src/userinfo/userinfo.controller.ts

@@ -1,4 +1,19 @@
-import { Controller } from '@nestjs/common';
-
+import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'
+import { AuthGuard } from '@nestjs/passport'
+import { UserinfoService } from './userinfo.service'
+import { Request } from 'express'
+import userDateDto from './dto/userinfo.dto'
 @Controller('userinfo')
-export class UserinfoController {}
+export class UserinfoController {
+  constructor(private readonly userinfoService: UserinfoService) {}
+  @Get('getinfo')
+  @UseGuards(AuthGuard('jwt'))
+  async getinfo(@Req() req: Request) {
+    return await this.userinfoService.getinfo(req.user as number)
+  }
+  @Post('updateinfo')
+  @UseGuards(AuthGuard('jwt'))
+  updateinfo(@Req() req: Request, @Body() data: userDateDto) {
+    return this.userinfoService.updateinfo(req.user as number, data)
+  }
+}

+ 42 - 2
src/userinfo/userinfo.service.ts

@@ -1,4 +1,44 @@
-import { Injectable } from '@nestjs/common';
+import { PrismaService } from '@/prisma/prisma.service'
+import { Injectable } from '@nestjs/common'
+import { auth } from '@prisma/client'
+import userDateDto from './dto/userinfo.dto'
 
 @Injectable()
-export class UserinfoService {}
+export class UserinfoService {
+  constructor(private prisma: PrismaService) {}
+  async getinfo(id: number) {
+    const userinfo = await this.prisma.auth.findUnique({
+      where: {
+        auth_id: id,
+      },
+      include: {
+        user: true,
+      },
+    })
+    delete userinfo.auth_id
+    delete userinfo.user.authId
+    delete userinfo.password
+    return { cod: 200, msg: '获取成功', data: { ...userinfo } }
+  }
+  async updateinfo(id: number, data: userDateDto) {
+    const userinfo = await this.prisma.auth.findUnique({
+      where: {
+        auth_id: id,
+      },
+      include: {
+        user: true,
+      },
+    })
+    const user = await this.prisma.userinfo.update({
+      where: {
+        authId: userinfo.user.authId,
+      },
+      data: {
+        avatar: data.avatar,
+        exp: data.exp,
+        level: data.level,
+      },
+    })
+    return { cod: 200, msg: '修改成功', data: user }
+  }
+}