Compare commits
No commits in common. "7988850063ae2a904b3855a9a83b7c23c297fb26" and "d61527709de0145baaee7501857be1382ad0514a" have entirely different histories.
7988850063
...
d61527709d
@ -8,29 +8,17 @@ 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(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
whitelist: true,
|
||||
forbidUnknownValues: true,
|
||||
forbidNonWhitelisted: true,
|
||||
validationError: { target: false },
|
||||
}),
|
||||
)
|
||||
options: QueryPostDto,
|
||||
) {
|
||||
async list(@Query() options: PaginateOptions) {
|
||||
return this.postService.paginate(options);
|
||||
}
|
||||
|
||||
@ -41,34 +29,16 @@ export class PostController {
|
||||
|
||||
@Post()
|
||||
async store(
|
||||
@Body(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
whitelist: true,
|
||||
forbidUnknownValues: true,
|
||||
forbidNonWhitelisted: true,
|
||||
validationError: { target: false },
|
||||
groups: ['create'],
|
||||
}),
|
||||
)
|
||||
data: CreatePostDto,
|
||||
@Body()
|
||||
data: RecordAny,
|
||||
) {
|
||||
return this.postService.create(data);
|
||||
}
|
||||
|
||||
@Patch()
|
||||
async update(
|
||||
@Body(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
whitelist: true,
|
||||
forbidUnknownValues: true,
|
||||
forbidNonWhitelisted: true,
|
||||
validationError: { target: false },
|
||||
groups: ['update'],
|
||||
}),
|
||||
)
|
||||
data: UpdatePostDto,
|
||||
@Body()
|
||||
data: RecordAny,
|
||||
) {
|
||||
return this.postService.update(data);
|
||||
}
|
||||
|
@ -1,12 +1,5 @@
|
||||
import { Expose } from 'class-transformer';
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryColumn } from 'typeorm';
|
||||
|
||||
import { PostBodyType } from '@/modules/content/constants';
|
||||
|
||||
@ -28,7 +21,7 @@ export class PostEntity extends BaseEntity {
|
||||
@Column({ comment: '关键字', type: 'simple-array', nullable: true })
|
||||
keywords?: [];
|
||||
|
||||
@Column({ comment: '文章类型', type: 'enum', enum: PostBodyType, default: PostBodyType.HTML })
|
||||
@Column({ comment: '文章类型', type: 'enum', enum: PostBodyType })
|
||||
type: PostBodyType;
|
||||
|
||||
@Column({ comment: '发布时间', type: 'varchar', nullable: true })
|
||||
@ -40,6 +33,6 @@ export class PostEntity extends BaseEntity {
|
||||
@CreateDateColumn({ comment: '创建时间' })
|
||||
createdAt?: Date;
|
||||
|
||||
@UpdateDateColumn({ comment: '更新时间' })
|
||||
@Column({ comment: '更新时间', nullable: true })
|
||||
updatedAt?: Date;
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ 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';
|
||||
@ -31,24 +30,14 @@ export class PostService {
|
||||
return item;
|
||||
}
|
||||
|
||||
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 });
|
||||
async create(data: RecordAny) {
|
||||
const item = await this.repository.save(data);
|
||||
return this.detail(item.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,
|
||||
});
|
||||
async update(data: RecordAny) {
|
||||
data.updatedAt = new Date();
|
||||
await this.repository.update(data.id, omit(data, ['id']));
|
||||
return this.detail(data.id);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user