chapter.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const express = require('express');
  2. const _ = require('lodash');
  3. const { query, body } = require('express-validator');
  4. const { onSuccess, fill } = require('../../../utils');
  5. const { deleteVideo } = require('../../../utils/qiniu');
  6. const { limit } = require('../../config');
  7. const check = require('../../../middleware/check');
  8. const Chapter = require('../../../db/models/chapter');
  9. const router = express.Router();
  10. async function fillSingle(data) {
  11. return data;
  12. }
  13. /**
  14. * showdoc
  15. * @catalog v1/章节
  16. * @title 章节列表
  17. * @description 章节列表接口
  18. * @method GET
  19. * @url /master/chapter/
  20. * @param page 0 Int 页码: 不传默认取第1页数据,每页20条数据
  21. * @return {"code":0, "count":1, "data": []}
  22. * @return_param id integer id
  23. * @return_param cid integer 课程id
  24. * @return_param title string 标题
  25. * @return_param status tinyint 状态
  26. * @return_param video string 视频
  27. * @return_param weight integer 权重
  28. * @return_param createdAt date -
  29. * @return_param updatedAt date -
  30. * @remark
  31. * @number
  32. */
  33. router.get('/', async (req, res) => {
  34. const { page = 0 } = req.query;
  35. const where = _.omit(req.query, ['page']);
  36. const info = onSuccess();
  37. info.data = await Chapter.findAll({ where, limit, offset: page * limit });
  38. info.data = await fill(info.data, fillSingle);
  39. if (!page) {
  40. info.count = await Chapter.count({ where });
  41. }
  42. res.send(info);
  43. });
  44. /**
  45. * showdoc
  46. * @catalog v1/章节
  47. * @title 章节详情
  48. * @description
  49. * @method GET
  50. * @url /master/chapter/:id
  51. * @param :id 1 STRING 详情ID
  52. * @return {"code":0, "data": {}}
  53. * @return_param id integer id
  54. * @return_param cid integer 课程id
  55. * @return_param title string 标题
  56. * @return_param status tinyint 状态
  57. * @return_param video string 视频
  58. * @return_param weight integer 权重
  59. * @return_param createdAt date -
  60. * @return_param updatedAt date -
  61. * @remark
  62. * @number
  63. */
  64. router.get('/:id', async (req, res) => {
  65. let data = await Chapter.findOne({ where: req.params });
  66. data = await fill(data, fillSingle);
  67. res.send(onSuccess(data));
  68. });
  69. /**
  70. * showdoc
  71. * @catalog v1/章节
  72. * @title 章节添加
  73. * @description
  74. * @method POST
  75. * @url /master/chapter/
  76. * @param cid integer 课程id
  77. * @param title string 标题
  78. * @param status tinyint 状态
  79. * @param video string 视频
  80. * @param weight integer 权重
  81. * @return {"code":0, "data": {}}
  82. * @remark
  83. * @number
  84. */
  85. router.post('/', check([body([]).notEmpty()]), async (req, res) => {
  86. const data = await Chapter.create(req.body);
  87. res.send(onSuccess(data));
  88. });
  89. /**
  90. * showdoc
  91. * @catalog v1/焦点图
  92. * @title 焦点图排序
  93. * @description
  94. * @method PUT
  95. * @url /master/banner/sort
  96. * @param data 1 array idlist
  97. * @return {"code":0, "msg": "修改成功"}
  98. * @remark
  99. * @number
  100. */
  101. router.put('/sort', async (req, res) => {
  102. for (const i in req.body.data) {
  103. await Chapter.update({ weight: i }, { where: { id: req.body.data[i] }, limit: 1 });
  104. }
  105. res.send(onSuccess(null, '修改成功'));
  106. });
  107. /**
  108. * showdoc
  109. * @catalog v1/章节
  110. * @title 章节修改
  111. * @description
  112. * @method PUT
  113. * @url /master/chapter/:id
  114. * @param :id 1 STRING 详情ID
  115. * @param cid integer 课程id
  116. * @param title string 标题
  117. * @param status tinyint 状态
  118. * @param video string 视频
  119. * @param weight integer 权重
  120. * @return {"code":0, "msg": "修改成功"}
  121. * @remark
  122. * @number
  123. */
  124. router.put('/:id', async (req, res) => {
  125. const data = await Chapter.update(req.body, { where: req.params, limit: 1 });
  126. res.send(onSuccess(null, '修改成功'));
  127. });
  128. /**
  129. * showdoc
  130. * @catalog v1/章节
  131. * @title 章节删除
  132. * @description
  133. * @method DELETE
  134. * @url /master/chapter/:id
  135. * @param :id 1 STRING 详情ID
  136. * @return {"status":0, "msg": "删除成功"}
  137. * @remark
  138. * @number
  139. */
  140. router.delete('/:id', async (req, res) => {
  141. const data = await Chapter.findOne({ where: req.params });
  142. await Chapter.destroy({ where: req.params, limit: 1 });
  143. deleteVideo(data.video);
  144. res.send(onSuccess(null, '删除成功'));
  145. });
  146. module.exports = router;