Pārlūkot izejas kodu

feat(Post): 添加分类和描述字段并支持删除方法

在Post模型中新增cate和description字段,为文章添加分类和描述功能
将删除文章的请求方法从POST改为DELETE,遵循RESTful规范
同时更新了相关的控制器逻辑以支持这些新字段
Sakulin 2 mēneši atpakaļ
vecāks
revīzija
855a1eb611
2 mainītis faili ar 25 papildinājumiem un 12 dzēšanām
  1. 13 11
      prisma/schema.prisma
  2. 12 1
      src/post/post.controller.ts

+ 13 - 11
prisma/schema.prisma

@@ -4,22 +4,24 @@ datasource db {
 }
 
 generator client {
-  provider = "prisma-client-js"
+    provider = "prisma-client-js"
 }
 
 model Tag {
-    id        Int     @id @default(autoincrement())
-    name      String  @unique
-    color     String // 创建随机鲜艳颜色
+    id        Int      @id @default(autoincrement())
+    name      String   @unique
+    color     String   @default("#000000")
     createdAt DateTime @default(now())
     posts     Post[]
 }
 
 model Post {
-    id        Int     @id @default(autoincrement())
-    title     String
-    content   String
-    createdAt DateTime @default(now())
-    updatedAt DateTime @default(now())
-    tags      Tag[]
-}
+    id          Int      @id @default(autoincrement())
+    title       String
+    content     String
+    createdAt   DateTime @default(now())
+    updatedAt   DateTime @default(now())
+    cate        String
+    descirption String   @default("")
+    tags        Tag[]
+}

+ 12 - 1
src/post/post.controller.ts

@@ -6,6 +6,7 @@ import {
   Post,
   Body,
   Headers,
+  Delete,
 } from '@nestjs/common';
 import { AuthService } from 'src/auth/auth.service';
 import { DatasourceService } from 'src/datasource/datasource.service';
@@ -14,6 +15,8 @@ interface PostBody {
   title: string;
   content: string;
   tags: string[];
+  cate: string;
+  description?: string;
 }
 
 @Controller('post')
@@ -45,6 +48,8 @@ export class PostController {
         title: true,
         createdAt: true,
         updatedAt: true,
+        cate: true,
+        descirption: true,
         tags: {
           select: {
             name: true,
@@ -73,6 +78,8 @@ export class PostController {
         content: true,
         createdAt: true,
         updatedAt: true,
+        cate: true,
+        descirption: true,
         tags: {
           select: {
             name: true,
@@ -110,6 +117,8 @@ export class PostController {
         data: {
           title: body.title,
           content: body.content,
+          cate: body.cate,
+          descirption: body.description ?? '',
           tags: {
             connectOrCreate: body.tags.map((tag) => ({
               where: { name: tag },
@@ -150,6 +159,8 @@ export class PostController {
         data: {
           title: body.title,
           content: body.content,
+          cate: body.cate,
+          descirption: body.description ?? '',
           tags: {
             set: [],
             connectOrCreate: body.tags.map((tag) => ({
@@ -169,7 +180,7 @@ export class PostController {
       }));
   }
 
-  @Post('delete/:id')
+  @Delete('delete/:id')
   async deletePost(
     @Param('id') id: string,
     @Headers('Authorization') token: string,