channel.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const express = require('express');
  2. const _ = require('lodash');
  3. const { query, body } = require('express-validator');
  4. const { onSuccess, fill } = require('../../../utils');
  5. const { limit } = require('../../config');
  6. const check = require('../../../middleware/check');
  7. const Channel = require('../../../db/models/channel');
  8. const router = express.Router();
  9. async function fillSingle(data) {
  10. return data;
  11. }
  12. /**
  13. * showdoc
  14. * @catalog v1/渠道
  15. * @title 渠道列表
  16. * @description 渠道列表接口
  17. * @method GET
  18. * @url /master/channel/
  19. * @param page 0 Int 页码: 不传默认取第1页数据,每页20条数据
  20. * @param all 0 Int 全部
  21. * @return {"code":0, "count":1, "data": []}
  22. * @return_param id integer id
  23. * @return_param name string 名
  24. * @return_param createdAt date -
  25. * @return_param updatedAt date -
  26. * @remark
  27. * @number
  28. */
  29. router.get('/', async (req, res) => {
  30. const { page = 0, all } = req.query;
  31. const where = _.omit(req.query, ['page']);
  32. const info = onSuccess();
  33. const options = { where };
  34. if (!all) {
  35. Object.assign(options, { limit, offset: page * limit });
  36. }
  37. info.data = await Channel.findAll(options);
  38. info.data = await fill(info.data, fillSingle);
  39. if (!page) {
  40. info.count = await Channel.count({ where });
  41. }
  42. res.send(info);
  43. });
  44. /**
  45. * showdoc
  46. * @catalog v1/渠道
  47. * @title 渠道添加
  48. * @description
  49. * @method POST
  50. * @url /master/channel/
  51. * @param name string 名
  52. * @return {"code":0, "data": {}}
  53. * @remark
  54. * @number
  55. */
  56. router.post('/', check([body([]).notEmpty()]), async (req, res) => {
  57. const data = await Channel.create(req.body);
  58. res.send(onSuccess(data));
  59. });
  60. /**
  61. * showdoc
  62. * @catalog v1/渠道
  63. * @title 渠道修改
  64. * @description
  65. * @method PUT
  66. * @url /master/channel/:id
  67. * @param :id 1 STRING 详情ID
  68. * @param name string 名
  69. * @return {"code":0, "msg": "修改成功"}
  70. * @remark
  71. * @number
  72. */
  73. router.put('/:id', async (req, res) => {
  74. const data = await Channel.update(req.body, { where: req.params, limit: 1 });
  75. res.send(onSuccess(null, '修改成功'));
  76. });
  77. /**
  78. * showdoc
  79. * @catalog v1/渠道
  80. * @title 渠道删除
  81. * @description
  82. * @method DELETE
  83. * @url /master/channel/:id
  84. * @param :id 1 STRING 详情ID
  85. * @return {"status":0, "msg": "删除成功"}
  86. * @remark
  87. * @number
  88. */
  89. router.delete('/:id', async (req, res) => {
  90. const data = await Channel.destroy({ where: req.params, limit: 1 });
  91. res.send(onSuccess(null, '删除成功'));
  92. });
  93. module.exports = router;