chore: format

This commit is contained in:
Innei
2021-08-01 16:25:14 +08:00
parent 02f8004145
commit f7d61346d8
7 changed files with 54 additions and 14 deletions

View File

@@ -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: [

View 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))
}
}

View File

@@ -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()
})
})

View File

@@ -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
}
}

View File

@@ -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({