chore: format
This commit is contained in:
@@ -4,8 +4,8 @@ import { AppController } from './app.controller'
|
||||
import { InitModule } from './modules/init/init.module'
|
||||
import { UserModule } from './modules/user/user.module'
|
||||
import { HelperModule } from './modules/helper/helper.module'
|
||||
import { PostModule } from './modules/post/post.module';
|
||||
import { CategoryModule } from './modules/category/category.module';
|
||||
import { PostModule } from './modules/post/post.module'
|
||||
import { CategoryModule } from './modules/category/category.module'
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
||||
15
src/common/exceptions/cant-find.exception.ts
Normal file
15
src/common/exceptions/cant-find.exception.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NotFoundException } from '@nestjs/common'
|
||||
import { sample } from 'lodash'
|
||||
export const NotFoundMessage = [
|
||||
'真不巧, 内容走丢了 o(╥﹏╥)o',
|
||||
'电波无法到达 ωω',
|
||||
'数据..不小心丢失了啦 π_π',
|
||||
'404, 这也不是我的错啦 (๐•̆ ·̭ •̆๐)',
|
||||
'嘿, 这里空空如也, 不如别处走走?',
|
||||
]
|
||||
|
||||
export class CannotFindException extends NotFoundException {
|
||||
constructor() {
|
||||
super(sample(NotFoundMessage))
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PostController } from './post.controller';
|
||||
import { Test, TestingModule } from '@nestjs/testing'
|
||||
import { PostController } from './post.controller'
|
||||
|
||||
describe('PostController', () => {
|
||||
let controller: PostController;
|
||||
let controller: PostController
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [PostController],
|
||||
}).compile();
|
||||
}).compile()
|
||||
|
||||
controller = module.get<PostController>(PostController);
|
||||
});
|
||||
controller = module.get<PostController>(PostController)
|
||||
})
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
expect(controller).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
import { InjectModel } from 'nestjs-typegoose'
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { CategoryService } from '../category/category.service'
|
||||
import { PostModel } from './post.model'
|
||||
|
||||
@@ -27,7 +28,21 @@ export class PostService {
|
||||
if (!category) {
|
||||
throw new UnprocessableEntityException('分类丢失了 ಠ_ಠ')
|
||||
}
|
||||
const res = this.postModel.create(post)
|
||||
const res = await this.postModel.create({
|
||||
...post,
|
||||
categoryId: category.id,
|
||||
created: new Date(),
|
||||
modified: null,
|
||||
})
|
||||
// TODO: clean cache
|
||||
return res
|
||||
}
|
||||
|
||||
async findPostById(id: string) {
|
||||
const doc = await this.postModel.findById(id).populate('category')
|
||||
if (!doc) {
|
||||
throw new CannotFindException()
|
||||
}
|
||||
return doc
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ export abstract class WriteBaseModel extends BaseCommentIndexModel {
|
||||
@prop({ type: Image })
|
||||
images?: Image[]
|
||||
|
||||
@prop({ default: () => new Date() })
|
||||
modified: Date
|
||||
@prop({ default: null })
|
||||
modified: Date | null
|
||||
}
|
||||
|
||||
@modelOptions({
|
||||
|
||||
Reference in New Issue
Block a user