feat: post pin

This commit is contained in:
Innei
2022-06-12 21:24:35 +08:00
parent 102b0723a5
commit 2a1d59f8da
2 changed files with 30 additions and 2 deletions

View File

@@ -53,7 +53,9 @@ export class PostController {
limit: size,
page,
select,
sort: sortBy ? { [sortBy]: sortOrder || -1 } : { created: -1 },
sort: sortBy
? { [sortBy]: sortOrder || -1 }
: { created: -1, pin: -1, pinOrder: -1 },
},
)
}
@@ -113,7 +115,6 @@ export class PostController {
@Post('/')
@Auth()
@HttpCode(201)
@HTTPDecorators.Idempotence()
async create(@Body() body: PostModel) {
const _id = new Types.ObjectId()

View File

@@ -1,13 +1,18 @@
import { Transform } from 'class-transformer'
import {
ArrayUnique,
IsBoolean,
IsDate,
IsInt,
IsMongoId,
IsNotEmpty,
IsOptional,
IsPositive,
IsString,
} from 'class-validator'
import { Query } from 'mongoose'
import { UnprocessableEntityException } from '@nestjs/common'
import { PartialType } from '@nestjs/mapped-types'
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger'
import {
@@ -89,6 +94,28 @@ export class PostModel extends WriteBaseModel {
@ApiHideProperty()
count?: Count
@prop()
@IsDate()
@IsOptional()
@Transform(({ value }) => {
if (typeof value != 'boolean') {
throw new UnprocessableEntityException('pin value must be boolean')
}
if (value === true) {
return new Date()
} else {
return null
}
})
pin?: Date | null
@prop()
@IsPositive()
@IsInt()
@IsOptional()
pinOrder?: number
static get protectedKeys() {
return ['count'].concat(super.protectedKeys)
}