diff --git a/src/modules/content/controllers/post.controller.ts b/src/modules/content/controllers/post.controller.ts index 5fffa57..46b95a7 100644 --- a/src/modules/content/controllers/post.controller.ts +++ b/src/modules/content/controllers/post.controller.ts @@ -8,17 +8,29 @@ import { Patch, Post, Query, + ValidationPipe, } from '@nestjs/common'; +import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto'; import { PostService } from '@/modules/content/services/post.service'; -import { PaginateOptions } from '@/modules/database/types'; @Controller('posts') export class PostController { constructor(private postService: PostService) {} @Get() - async list(@Query() options: PaginateOptions) { + async list( + @Query( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidUnknownValues: true, + forbidNonWhitelisted: true, + validationError: { target: false }, + }), + ) + options: QueryPostDto, + ) { return this.postService.paginate(options); } @@ -29,16 +41,34 @@ export class PostController { @Post() async store( - @Body() - data: RecordAny, + @Body( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidUnknownValues: true, + forbidNonWhitelisted: true, + validationError: { target: false }, + groups: ['create'], + }), + ) + data: CreatePostDto, ) { return this.postService.create(data); } @Patch() async update( - @Body() - data: RecordAny, + @Body( + new ValidationPipe({ + transform: true, + whitelist: true, + forbidUnknownValues: true, + forbidNonWhitelisted: true, + validationError: { target: false }, + groups: ['update'], + }), + ) + data: UpdatePostDto, ) { return this.postService.update(data); } diff --git a/src/modules/content/services/post.service.ts b/src/modules/content/services/post.service.ts index d720f7b..bea1b72 100644 --- a/src/modules/content/services/post.service.ts +++ b/src/modules/content/services/post.service.ts @@ -5,6 +5,7 @@ import { isFunction, omit } from 'lodash'; import { EntityNotFoundError, IsNull, Not, SelectQueryBuilder } from 'typeorm'; import { PostOrder } from '@/modules/content/constants'; +import { CreatePostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto'; import { PostEntity } from '@/modules/content/entities/post.entity'; import { PostRepository } from '@/modules/content/repositories/post.repository'; import { PaginateOptions, QueryHook } from '@/modules/database/types'; @@ -30,14 +31,25 @@ export class PostService { return item; } - async create(data: RecordAny) { - const item = await this.repository.save(data); + async create(data: CreatePostDto) { + let publishedAt: Date | null; + if (!isNil(data.publish)) { + publishedAt = data.publish ? new Date() : null; + } + const item = await this.repository.save({ ...omit(data, ['publish']), publishedAt }); return this.detail(item.id); } - async update(data: RecordAny) { - data.updatedAt = new Date(); - await this.repository.update(data.id, omit(data, ['id'])); + async update(data: UpdatePostDto) { + let publishedAt: Date | null; + if (!isNil(data.publish)) { + publishedAt = data.publish ? new Date() : null; + } + await this.repository.update(data.id, { + ...omit(data, ['id', 'publish']), + publishedAt, + updatedAt: new Date(), + }); return this.detail(data.id); }