Skip to content

Commit

Permalink
perf(post): define more options in model
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee committed Jul 17, 2022
1 parent d53def3 commit 1f90938
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/modules/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Auth } from "~/common/decorator/auth.decorator";
import { PostModel } from "./post.model";
import { Types, PipelineStage } from "mongoose";
import { MongoIdDto } from "~/shared/dto/id.dto";
import { IsMaster } from "~/common/decorator/role.decorator";
@Controller("posts")
@ApiName
export class PostController {
Expand All @@ -33,9 +34,18 @@ export class PostController {
@Get('/')
@Paginator
@ApiOperation({ summary: "获取文章列表(附带分页器)" })
async getPaginate(@Query() query: PagerDto) {
async getPaginate(@Query() query: PagerDto, @IsMaster() isMaster: boolean) {
const { size, select, page, year, sortBy, sortOrder } = query

let hideProperty
if (!isMaster) {
hideProperty = {
$project: {
hide: 0,
password: 0,
rss: 0,
},
}
}
return this.postService.model.aggregatePaginate(
this.postService.model.aggregate(
[
Expand All @@ -46,7 +56,7 @@ export class PostController {
},
// @see https://stackoverflow.com/questions/54810712/mongodb-sort-by-field-a-if-field-b-null-otherwise-sort-by-field-c
{
$addFields: {
$addFields: {
sortField: {
// create a new field called "sortField"
$cond: {
Expand All @@ -70,7 +80,7 @@ export class PostController {
},
},
{
$project: {
$project: { // project the fields we want to keep
sortField: 0, // remove "sort" field if needed
},
},
Expand Down Expand Up @@ -100,6 +110,8 @@ export class PostController {
preserveNullAndEmptyArrays: true, // if set to true, MongoDB will still create a document if the array is empty
},
},
// 移除 hide 字段
...hideProperty,
].filter(Boolean) as PipelineStage[],
),
{
Expand Down
20 changes: 19 additions & 1 deletion src/modules/post/post.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class PostModel extends WriteBaseModel {
return null;
}
})
@ApiProperty({ description: "文章置顶" })
@ApiProperty({ description: "文章pin日期" })
pin?: Date | null;

@prop()
Expand Down Expand Up @@ -130,6 +130,24 @@ export class PostModel extends WriteBaseModel {
@IsOptional()
@ApiProperty({ description: '文章修改时间' })
modified?: Date

@prop({ type: Boolean, default: false })
@IsOptional()
@IsBoolean()
@ApiProperty({ description: "文章是否隐藏" })
hide?: boolean;

@prop({ type: String, default: null })
@IsOptional()
@IsString()
@ApiProperty({ description: "文章加密密码(若填写则启动加密)" })
password?: string;

@prop({ type: Boolean, default: true })
@IsOptional()
@IsBoolean()
@ApiProperty({ description: "文章是否公开在RSS输出" })
rss?: boolean;
}


Expand Down

0 comments on commit 1f90938

Please sign in to comment.