增加题库相关接口

This commit is contained in:
zhl 2021-04-22 20:22:22 +08:00
parent 5fdf04b5b5
commit 4f17e9c6ec
4 changed files with 137 additions and 2 deletions

2
.env
View File

@ -16,5 +16,5 @@ ADMIN_TOKEN_SECRET=322f(**&*&xx221356
ADMIN_TOKEN_EXPIRESIN=1d ADMIN_TOKEN_EXPIRESIN=1d
DB_MAIN=mongodb://localhost/pyxis-development DB_MAIN=mongodb://localhost/pyxis-development
DB_SECOND=mongodb://localhost/pyxis2-development DB_SECOND=mongodb://tiku:kingsome@192.168.100.30/tiku?authSource=tiku&w=1

View File

@ -16,5 +16,5 @@ ADMIN_TOKEN_SECRET=322f(**&*&xx221356
ADMIN_TOKEN_EXPIRESIN=1d ADMIN_TOKEN_EXPIRESIN=1d
DB_MAIN=mongodb://localhost/pyxis-development DB_MAIN=mongodb://localhost/pyxis-development
DB_SECOND=mongodb://localhost/pyxis2-development DB_SECOND=mongodb://tiku:kingsome@192.168.100.30/tiku?authSource=tiku&w=1

View File

@ -0,0 +1,74 @@
import BaseController from '../../common/base.controller'
import { permission, router } from '../../decorators/router'
import { ZError } from '../../common/ZError'
import { Puzzle } from '../../models/content/Puzzle'
class PuzzleController extends BaseController{
@permission('puzzle:read')
@router('post /puzzles')
async list(req, res) {
let { start, limit } = req.params
start = +start || 0
limit = +limit || 10
let { opt, sort } = Puzzle.parseQueryParam(req.params)
let articles = await Puzzle.find(opt)
.sort(sort)
.skip(start)
.limit(limit)
let count = await Puzzle.count(opt)
let records = []
for (let record of articles) {
records.push(record.toJson())
}
return {
total: count,
start,
limit,
records
}
}
@permission('puzzle:read')
@router('get /puzzle/:id')
async detail(req, res) {
let { id } = req.params
const record = await Puzzle.findById(id)
if (!record) {
throw new ZError(11, 'record not found')
}
return record.toJson()
}
@permission('puzzle:read')
@router('post /puzzle/save')
async save(req: any) {
let { _id } = req.params
let user = req.user
let record
if (!_id) {
record = new Puzzle(req.params)
record.createdBy = user.id
} else {
record = await Puzzle.findById(_id)
record.updateFromReq(req.params)
}
await record.save()
return record.toJson()
}
@permission('puzzle:read')
@router('post /puzzle/:id/delete')
async delete(req: any) {
let { id } = req.params
if (!id) {
throw new ZError(11, 'params mismatch')
}
await Puzzle.findByIdAndUpdate(id, {
$set: {
deleted: 1,
deleteTime: new Date()
}
})
return {}
}
}

View File

@ -0,0 +1,61 @@
import { dbconn } from '../../decorators/dbconn'
import { getModelForClass, modelOptions, prop } from '@typegoose/typegoose'
import { BaseModule } from '../Base'
import { noJson } from '../../decorators/nojson'
@dbconn('second')
@modelOptions({ schemaOptions: { collection: 'question' } })
class PuzzleClass extends BaseModule {
@prop()
public question: string
@prop()
public a1: string
@prop()
public a2: string
@prop()
public a3: string
@prop()
public a4: string
@prop()
public quality: number
/**
*
* @type {boolean}
*/
@noJson()
@prop({default: 0})
public deleted: number
@noJson()
@prop()
public is_hide: number
@prop()
public group: string
@prop()
public tag: string
public static parseQueryParam(params) {
let {key, timeBegin, timeEnd } = params
let opt: any = {deleted: 0, is_hide: 0}
if (key) {
opt.question = {$regex: key, $options: 'i'}
}
if (timeBegin && !timeEnd) {
opt.createtime = {$gte: timeBegin};
} else if (timeBegin && timeEnd) {
opt['$and'] = [{createtime: {$gte: timeBegin}}, {createtime: {$lte: timeEnd}}];
} else if (!timeBegin && timeEnd) {
opt.createtime = {$lte: timeEnd};
}
let sort = {_id: 1}
return { opt, sort }
}
}
export const Puzzle = getModelForClass(PuzzleClass, { existingConnection: PuzzleClass.db })