add content
This commit is contained in:
parent
d61527709d
commit
356d9d5996
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user