管理员审核post
This commit is contained in:
父節點
8f7a71321b
當前提交
1bcaa0f84b
@ -12,9 +12,21 @@ export class PostModel {
|
||||
])
|
||||
}
|
||||
|
||||
async findPostByStatus(status: TPostStatus, page: number) {
|
||||
async findPostsByStatus(status: TPostStatus, page: number) {
|
||||
let arr = await db.query('select * from posts where status=? order by time desc '
|
||||
+ pageToLimitSqlSegment(page),[status])
|
||||
+ pageToLimitSqlSegment(page), [status])
|
||||
return arr.map((single: any) => fillIPost(single))
|
||||
}
|
||||
|
||||
async findPostById(id: number) {
|
||||
let raw = await db.query('select * from posts where id=?', [id])
|
||||
if (!raw.length) {
|
||||
return null
|
||||
}
|
||||
return fillIPost(raw[0])
|
||||
}
|
||||
|
||||
async setStatusByPostId(id: number, status: TPostStatus) {
|
||||
console.log(await db.query('update posts set status=? where id=?', [status, id]))
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,13 @@ import 'reflect-metadata'
|
||||
import express from "express"
|
||||
import {JWTUserPayload, TPostStatus} from "../types"
|
||||
import {hasValidationErrors, resultJson} from "../includes"
|
||||
import {param, query} from "express-validator"
|
||||
import {body, param, query} from "express-validator"
|
||||
import {Container} from "typedi"
|
||||
import {AdminService} from "../services/admin-service"
|
||||
import {PostModel} from "../models/post-model"
|
||||
|
||||
let adminService = Container.get(AdminService)
|
||||
let postModel = Container.get(PostModel)
|
||||
let adminRouter = express.Router()
|
||||
adminRouter.use((req: express.Request, res: express.Response, next) => {
|
||||
if ((req.user as JWTUserPayload).role < 2) {
|
||||
@ -19,9 +21,22 @@ adminRouter.get('/list/:status',
|
||||
param('status').isIn([1, 2, 3]).withMessage('Post状态仅支援1,2,3'),
|
||||
query('page').default(1).isInt({min: 1}).withMessage('页码必须>=1'),
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
if(hasValidationErrors(req,res)) return
|
||||
if (hasValidationErrors(req, res)) return
|
||||
res.json(await adminService.list(req.params.status as any as TPostStatus, req.query.page as any as number))
|
||||
}
|
||||
)
|
||||
adminRouter.post('/setStatus',
|
||||
body('id').isInt().bail().custom(async input => {
|
||||
let post = await postModel.findPostById(input)
|
||||
if (!post) {
|
||||
throw new Error('Post ID不存在')
|
||||
}
|
||||
}),
|
||||
body('status').isIn([1, 2, 3]).withMessage('Post状态仅支援1,2,3'),
|
||||
async (req: express.Request, res: express.Response) => {
|
||||
if (hasValidationErrors(req, res)) return
|
||||
res.json(await adminService.setStatus(req.body.id, req.body.status))
|
||||
}
|
||||
)
|
||||
|
||||
export {adminRouter}
|
||||
|
@ -10,6 +10,11 @@ export class AdminService {
|
||||
postModel!: PostModel
|
||||
|
||||
async list(status: TPostStatus, page: number) {
|
||||
return resultJson.success(await this.postModel.findPostByStatus(status, page))
|
||||
return resultJson.success(await this.postModel.findPostsByStatus(status, page))
|
||||
}
|
||||
|
||||
async setStatus(id: number, status: TPostStatus) {
|
||||
await this.postModel.setStatusByPostId(id, status)
|
||||
return resultJson.success({})
|
||||
}
|
||||
}
|
||||
|
載入中…
x
新增問題並參考
Block a user