add content
This commit is contained in:
parent
65b52d8b6e
commit
6059ff4ed2
@ -7,10 +7,14 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Patch,
|
Patch,
|
||||||
Post,
|
Post,
|
||||||
|
ValidationPipe,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
|
||||||
import { isNil } from '@nestjs/common/utils/shared.utils';
|
import { isNil } from '@nestjs/common/utils/shared.utils';
|
||||||
|
|
||||||
|
import { CreatePostDto } from '@/modules/content/dtos/create-post.dto';
|
||||||
|
import { UpdatePostDto } from '@/modules/content/dtos/update-post.dto';
|
||||||
|
|
||||||
import { PostEntity } from '../types';
|
import { PostEntity } from '../types';
|
||||||
|
|
||||||
let posts: PostEntity[] = [
|
let posts: PostEntity[] = [
|
||||||
@ -39,7 +43,18 @@ export class PostController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async store(@Body() data: PostEntity) {
|
async store(
|
||||||
|
@Body(
|
||||||
|
new ValidationPipe({
|
||||||
|
transform: true,
|
||||||
|
forbidNonWhitelisted: true,
|
||||||
|
forbidUnknownValues: true,
|
||||||
|
validationError: { target: false },
|
||||||
|
groups: ['create'],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
data: CreatePostDto,
|
||||||
|
) {
|
||||||
const newPost: PostEntity = {
|
const newPost: PostEntity = {
|
||||||
id: Math.max(...posts.map(({ id }) => id + 1)),
|
id: Math.max(...posts.map(({ id }) => id + 1)),
|
||||||
...data,
|
...data,
|
||||||
@ -49,14 +64,25 @@ export class PostController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Patch()
|
@Patch()
|
||||||
async update(@Body() data: PostEntity) {
|
async update(
|
||||||
let toUpdate = posts.find((item) => item.id === Number(data.id));
|
@Body(
|
||||||
|
new ValidationPipe({
|
||||||
|
transform: true,
|
||||||
|
forbidNonWhitelisted: true,
|
||||||
|
forbidUnknownValues: true,
|
||||||
|
validationError: { target: false },
|
||||||
|
groups: ['update'],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
{ id, ...data }: UpdatePostDto,
|
||||||
|
) {
|
||||||
|
let toUpdate = posts.find((item) => item.id === Number(id));
|
||||||
if (isNil(toUpdate)) {
|
if (isNil(toUpdate)) {
|
||||||
throw new NotFoundException(`the post with id ${data.id} not exits!`);
|
throw new NotFoundException(`the post with id ${id} not exits!`);
|
||||||
}
|
}
|
||||||
|
|
||||||
toUpdate = { ...toUpdate, ...data };
|
toUpdate = { ...toUpdate, ...data };
|
||||||
posts = posts.map((item) => (item.id === Number(data.id) ? toUpdate : item));
|
posts = posts.map((item) => (item.id === Number(id) ? toUpdate : item));
|
||||||
return toUpdate;
|
return toUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
src/modules/content/dtos/create-post.dto.ts
Normal file
18
src/modules/content/dtos/create-post.dto.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { IsNotEmpty, IsOptional, MaxLength } from 'class-validator';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CreatePostDto {
|
||||||
|
@MaxLength(255, { always: true, message: 'the max length of content title is $constraint1' })
|
||||||
|
@IsNotEmpty({ groups: ['create'], message: 'the title of content should not be empty' })
|
||||||
|
@IsOptional({ groups: ['update'] })
|
||||||
|
title: string;
|
||||||
|
|
||||||
|
@IsNotEmpty({ groups: ['create'], message: 'the body of content should not be empty' })
|
||||||
|
@IsOptional({ groups: ['update'] })
|
||||||
|
body: string;
|
||||||
|
|
||||||
|
@MaxLength(500, { always: true, message: 'the max length of content summary is $constraint1' })
|
||||||
|
@IsOptional({ always: true })
|
||||||
|
summary?: string;
|
||||||
|
}
|
13
src/modules/content/dtos/update-post.dto.ts
Normal file
13
src/modules/content/dtos/update-post.dto.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { PartialType } from '@nestjs/swagger';
|
||||||
|
|
||||||
|
import { IsDefined, IsNumber } from 'class-validator';
|
||||||
|
|
||||||
|
import { CreatePostDto } from './create-post.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UpdatePostDto extends PartialType(CreatePostDto) {
|
||||||
|
@IsNumber(undefined, { groups: ['update'], message: 'The format of the post ID is incorrect.' })
|
||||||
|
@IsDefined({ groups: ['update'], message: 'The post ID must be specified' })
|
||||||
|
id: number;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user