Compare commits
2 Commits
d61527709d
...
7988850063
Author | SHA1 | Date | |
---|---|---|---|
7988850063 | |||
356d9d5996 |
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
import { Expose } from 'class-transformer';
|
import { Expose } from 'class-transformer';
|
||||||
import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryColumn } from 'typeorm';
|
import {
|
||||||
|
BaseEntity,
|
||||||
|
Column,
|
||||||
|
CreateDateColumn,
|
||||||
|
Entity,
|
||||||
|
PrimaryColumn,
|
||||||
|
UpdateDateColumn,
|
||||||
|
} from 'typeorm';
|
||||||
|
|
||||||
import { PostBodyType } from '@/modules/content/constants';
|
import { PostBodyType } from '@/modules/content/constants';
|
||||||
|
|
||||||
@ -21,7 +28,7 @@ export class PostEntity extends BaseEntity {
|
|||||||
@Column({ comment: '关键字', type: 'simple-array', nullable: true })
|
@Column({ comment: '关键字', type: 'simple-array', nullable: true })
|
||||||
keywords?: [];
|
keywords?: [];
|
||||||
|
|
||||||
@Column({ comment: '文章类型', type: 'enum', enum: PostBodyType })
|
@Column({ comment: '文章类型', type: 'enum', enum: PostBodyType, default: PostBodyType.HTML })
|
||||||
type: PostBodyType;
|
type: PostBodyType;
|
||||||
|
|
||||||
@Column({ comment: '发布时间', type: 'varchar', nullable: true })
|
@Column({ comment: '发布时间', type: 'varchar', nullable: true })
|
||||||
@ -33,6 +40,6 @@ export class PostEntity extends BaseEntity {
|
|||||||
@CreateDateColumn({ comment: '创建时间' })
|
@CreateDateColumn({ comment: '创建时间' })
|
||||||
createdAt?: Date;
|
createdAt?: Date;
|
||||||
|
|
||||||
@Column({ comment: '更新时间', nullable: true })
|
@UpdateDateColumn({ comment: '更新时间' })
|
||||||
updatedAt?: Date;
|
updatedAt?: Date;
|
||||||
}
|
}
|
||||||
|
@ -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,24 @@ 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,
|
||||||
|
});
|
||||||
return this.detail(data.id);
|
return this.detail(data.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user