add content

This commit is contained in:
liuyi 2025-05-20 00:05:32 +08:00
parent d61527709d
commit 356d9d5996
2 changed files with 53 additions and 11 deletions

View File

@ -8,17 +8,29 @@ import {
Patch, Patch,
Post, Post,
Query, Query,
ValidationPipe,
} from '@nestjs/common'; } from '@nestjs/common';
import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto';
import { PostService } from '@/modules/content/services/post.service'; import { PostService } from '@/modules/content/services/post.service';
import { PaginateOptions } from '@/modules/database/types';
@Controller('posts') @Controller('posts')
export class PostController { export class PostController {
constructor(private postService: PostService) {} constructor(private postService: PostService) {}
@Get() @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); return this.postService.paginate(options);
} }
@ -29,16 +41,34 @@ export class PostController {
@Post() @Post()
async store( async store(
@Body() @Body(
data: RecordAny, new ValidationPipe({
transform: true,
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
validationError: { target: false },
groups: ['create'],
}),
)
data: CreatePostDto,
) { ) {
return this.postService.create(data); return this.postService.create(data);
} }
@Patch() @Patch()
async update( async update(
@Body() @Body(
data: RecordAny, new ValidationPipe({
transform: true,
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
validationError: { target: false },
groups: ['update'],
}),
)
data: UpdatePostDto,
) { ) {
return this.postService.update(data); return this.postService.update(data);
} }

View File

@ -5,6 +5,7 @@ import { isFunction, omit } from 'lodash';
import { EntityNotFoundError, IsNull, Not, SelectQueryBuilder } from 'typeorm'; import { EntityNotFoundError, IsNull, Not, SelectQueryBuilder } from 'typeorm';
import { PostOrder } from '@/modules/content/constants'; import { PostOrder } from '@/modules/content/constants';
import { CreatePostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto';
import { PostEntity } from '@/modules/content/entities/post.entity'; import { PostEntity } from '@/modules/content/entities/post.entity';
import { PostRepository } from '@/modules/content/repositories/post.repository'; import { PostRepository } from '@/modules/content/repositories/post.repository';
import { PaginateOptions, QueryHook } from '@/modules/database/types'; import { PaginateOptions, QueryHook } from '@/modules/database/types';
@ -30,14 +31,25 @@ export class PostService {
return item; return item;
} }
async create(data: RecordAny) { async create(data: CreatePostDto) {
const item = await this.repository.save(data); 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); return this.detail(item.id);
} }
async update(data: RecordAny) { async update(data: UpdatePostDto) {
data.updatedAt = new Date(); let publishedAt: Date | null;
await this.repository.update(data.id, omit(data, ['id'])); 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); return this.detail(data.id);
} }