mirror of
https://github.com/SunnyQjm/sunny-blog-server.git
synced 2026-10-04 19:52:21 +08:00
add: 添加更新博文接口
This commit is contained in:
1 parent
98f69fa9e1
commit
d642794be4
3 files changed
+34
-3
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { CreatePostDto } from './dto/create-post.dto';
|
||||
import { Post as PostModel } from '../entity/post.entity';
|
||||
@@ -8,6 +8,7 @@ import { UserService } from '../user/user.service';
|
||||
import { AdminGuard } from '../guard/admin-guard';
|
||||
import { CreateTagDto } from './dto/create-tag.dto';
|
||||
import { Tag } from '../entity/tag.entity';
|
||||
import { UpdatePostDto } from './dto/update-post.dto';
|
||||
|
||||
@Controller('admin')
|
||||
@UseGuards(AuthGuard('jwt'), AdminGuard)
|
||||
@@ -56,8 +57,27 @@ export class AdminController {
|
||||
return ResponseUtil.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新博文
|
||||
* @param updatePostDto
|
||||
* @param req
|
||||
*/
|
||||
@Post('update-post')
|
||||
async updatePost(@Body() updatePostDto: UpdatePostDto, @Req() req) {
|
||||
const post = await this.postService.getPostById(updatePostDto.id);
|
||||
if (!post) {
|
||||
return ResponseUtil.fail('要修改的博文不存在');
|
||||
}
|
||||
post.tags = await this.postService.preSaveTags(updatePostDto.tags);
|
||||
if (updatePostDto.content) {
|
||||
post.content = updatePostDto.content;
|
||||
}
|
||||
await this.postService.savePost(post);
|
||||
return ResponseUtil.success();
|
||||
}
|
||||
|
||||
@Post('del-post')
|
||||
async delPost(@Body() delPostDto: {postId: number}) {
|
||||
async delPost(@Body() delPostDto: { postId: number }) {
|
||||
const result = await this.postService.delPost(delPostDto.postId);
|
||||
return ResponseUtil.success(result);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { IsArray, IsString } from 'class-validator';
|
||||
import { Tag } from '../../entity/tag.entity';
|
||||
|
||||
export class CreatePostDto {
|
||||
@IsString()
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { IsArray, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class UpdatePostDto {
|
||||
@IsNumber()
|
||||
id: number;
|
||||
|
||||
@IsString()
|
||||
content?: string;
|
||||
|
||||
@IsArray()
|
||||
tags?: string[];
|
||||
}
|
||||
Reference in new issue
Block a user