diff --git a/src/helpers/entity-fill.ts b/src/helpers/entity-fill.ts new file mode 100644 index 0000000..88f7f07 --- /dev/null +++ b/src/helpers/entity-fill.ts @@ -0,0 +1,21 @@ +import {IPost, IUser} from "../types" + +export function fillIUser(single: any) { + return { + id: single.id, + username: single.username, + realName: single.realName, + role: single.role + } +} + +export function fillIPost(single: any) { + return { + id: single.id, + userId: single.userId, + content: single.content, + image: single.image, + status: single.status, + time: single.time + } +} diff --git a/src/includes.ts b/src/includes.ts index 13a9a7f..345ab0a 100644 --- a/src/includes.ts +++ b/src/includes.ts @@ -73,3 +73,7 @@ export function getAxiosInstance() { instance.defaults.jar = new tough.CookieJar() return instance } + +export function getTimestampInSeconds() { + return Math.floor(Date.now() / 1000) +} diff --git a/src/models/post-model.ts b/src/models/post-model.ts index 6766568..f6a7169 100644 --- a/src/models/post-model.ts +++ b/src/models/post-model.ts @@ -1,7 +1,13 @@ import 'reflect-metadata' import {Service} from "typedi" +import {IPost} from "../types" +import {db} from "../includes" @Service() -export class PostModel{ - +export class PostModel { + async insertPost(post: IPost) { + return db.query('insert into posts (userId,content,image,status,time) values(?,?,?,?,?)', [ + post.userId, post.content, post.image, post.status, post.time + ]) + } } diff --git a/src/models/user-model.ts b/src/models/user-model.ts index 252ab21..0355a70 100644 --- a/src/models/user-model.ts +++ b/src/models/user-model.ts @@ -2,6 +2,7 @@ import 'reflect-metadata' import {Service} from "typedi" import {IUser} from "../types" import {db} from "../includes" +import {fillIUser} from "../helpers/entity-fill" @Service() export class UserModel { @@ -17,12 +18,7 @@ export class UserModel { if (!user.length) { return null } - return { - id: user[0].id, - username: user[0].username, - realName: user[0].realName, - role: user[0].role - } + return fillIUser(user[0]) } async findUserById(id: number) { @@ -30,11 +26,6 @@ export class UserModel { if (!user.length) { return null } - return { - id: user[0].id, - username: user[0].username, - realName: user[0].realName, - role: user[0].role - } + return fillIUser(user[0]) } } diff --git a/src/routers/post-router.ts b/src/routers/post-router.ts index 487f3b1..4bad711 100644 --- a/src/routers/post-router.ts +++ b/src/routers/post-router.ts @@ -5,6 +5,7 @@ import express from "express" import {body} from "express-validator" import * as Validator from "validator" import {hasValidationErrors} from "../includes" +import {JWTUserPayload} from "../types" let postService = Container.get(PostService) @@ -12,22 +13,10 @@ let postRouter = express.Router() postRouter.post('/submit', body('content').notEmpty().withMessage('请输入内容') .escape(), - body('image').isJSON().withMessage('图片必须为JSON格式').bail().custom((input, {req}) => { - let arr: string[] = JSON.parse(input) - if(!arr.length){ - throw new Error('图片数量至少为1') - } - for (let url of arr) { - if (!Validator.default.isURL(url, {host_whitelist: [/.*\.360buyimg\.com/]})) { - throw new Error('必须是360buyimg.com下的图片') - } - } - req.body.imageArr = arr - return true - }), - (req: express.Request, res: express.Response) => { + body('image').isURL({host_whitelist: [/.*\.360buyimg\.com/]}).withMessage('仅支援*.360buyimg.com下的图片'), + async (req: express.Request, res: express.Response) => { if (hasValidationErrors(req, res)) return - res.json(postService.submit(req.body.content, req.body.imageArr)) + res.json(await postService.submit(req.user as JWTUserPayload,req.body.content, req.body.image)) } ) diff --git a/src/services/post-service.ts b/src/services/post-service.ts index ddfaa79..fed6019 100644 --- a/src/services/post-service.ts +++ b/src/services/post-service.ts @@ -1,10 +1,22 @@ import 'reflect-metadata' -import {Service} from "typedi" +import {Inject, Service} from "typedi" +import {PostModel} from "../models/post-model" +import {JWTUserPayload} from "../types" +import {getTimestampInSeconds, resultJson} from "../includes" @Service() export class PostService { - async submit(content: string, imageArr: string[]) { - console.log(content) - console.log(imageArr) + @Inject() + postModel!: PostModel + + async submit(jwtUserPayload: JWTUserPayload, content: string, image: string) { + let packet = await this.postModel.insertPost({ + userId: jwtUserPayload.id, + content: content, + image: image, + status: 1, + time: getTimestampInSeconds() + }) + return resultJson.success(packet.insertId) } } diff --git a/src/types.ts b/src/types.ts index d56b184..1a4c219 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,15 +3,31 @@ export interface IResultJson { data: any } +export type TUserRole = 1 | 2 + export interface IUser { id?: number, username: string, realName: string, - role: 1 | 2 + role: TUserRole } export interface JWTUserPayload { id: number, - role: 1 | 2 + role: TUserRole +} + +export type TPostPending = 1 +export type TPostPublished = 2 +export type TPostRejected = 3 +export type TPostStatus = TPostPending | TPostPublished | TPostRejected + +export interface IPost { + id?: number, + userId: number, + content: string, + image: string, + status: TPostStatus, + time: number }