const express = require('express'); const _ = require('lodash'); const { query, body } = require('express-validator'); const { onSuccess, fill } = require('../../../utils'); const { deleteVideo } = require('../../../utils/qiniu'); const { limit } = require('../../config'); const check = require('../../../middleware/check'); const Chapter = require('../../../db/models/chapter'); const router = express.Router(); async function fillSingle(data) { return data; } /** * showdoc * @catalog v1/章节 * @title 章节列表 * @description 章节列表接口 * @method GET * @url /master/chapter/ * @param page 0 Int 页码: 不传默认取第1页数据,每页20条数据 * @return {"code":0, "count":1, "data": []} * @return_param id integer id * @return_param cid integer 课程id * @return_param title string 标题 * @return_param status tinyint 状态 * @return_param video string 视频 * @return_param weight integer 权重 * @return_param createdAt date - * @return_param updatedAt date - * @remark * @number */ router.get('/', async (req, res) => { const { page = 0 } = req.query; const where = _.omit(req.query, ['page']); const info = onSuccess(); info.data = await Chapter.findAll({ where, limit, offset: page * limit }); info.data = await fill(info.data, fillSingle); if (!page) { info.count = await Chapter.count({ where }); } res.send(info); }); /** * showdoc * @catalog v1/章节 * @title 章节详情 * @description * @method GET * @url /master/chapter/:id * @param :id 1 STRING 详情ID * @return {"code":0, "data": {}} * @return_param id integer id * @return_param cid integer 课程id * @return_param title string 标题 * @return_param status tinyint 状态 * @return_param video string 视频 * @return_param weight integer 权重 * @return_param createdAt date - * @return_param updatedAt date - * @remark * @number */ router.get('/:id', async (req, res) => { let data = await Chapter.findOne({ where: req.params }); data = await fill(data, fillSingle); res.send(onSuccess(data)); }); /** * showdoc * @catalog v1/章节 * @title 章节添加 * @description * @method POST * @url /master/chapter/ * @param cid integer 课程id * @param title string 标题 * @param status tinyint 状态 * @param video string 视频 * @param weight integer 权重 * @return {"code":0, "data": {}} * @remark * @number */ router.post('/', check([body([]).notEmpty()]), async (req, res) => { const data = await Chapter.create(req.body); res.send(onSuccess(data)); }); /** * showdoc * @catalog v1/焦点图 * @title 焦点图排序 * @description * @method PUT * @url /master/banner/sort * @param data 1 array idlist * @return {"code":0, "msg": "修改成功"} * @remark * @number */ router.put('/sort', async (req, res) => { for (const i in req.body.data) { await Chapter.update({ weight: i }, { where: { id: req.body.data[i] }, limit: 1 }); } res.send(onSuccess(null, '修改成功')); }); /** * showdoc * @catalog v1/章节 * @title 章节修改 * @description * @method PUT * @url /master/chapter/:id * @param :id 1 STRING 详情ID * @param cid integer 课程id * @param title string 标题 * @param status tinyint 状态 * @param video string 视频 * @param weight integer 权重 * @return {"code":0, "msg": "修改成功"} * @remark * @number */ router.put('/:id', async (req, res) => { const data = await Chapter.update(req.body, { where: req.params, limit: 1 }); res.send(onSuccess(null, '修改成功')); }); /** * showdoc * @catalog v1/章节 * @title 章节删除 * @description * @method DELETE * @url /master/chapter/:id * @param :id 1 STRING 详情ID * @return {"status":0, "msg": "删除成功"} * @remark * @number */ router.delete('/:id', async (req, res) => { const data = await Chapter.findOne({ where: req.params }); await Chapter.destroy({ where: req.params, limit: 1 }); deleteVideo(data.video); res.send(onSuccess(null, '删除成功')); }); module.exports = router;