pages.controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Created by zhengguorong on 16/11/4.
  3. */
  4. var jsonpatch = require('fast-json-patch')
  5. var Pages = require('./pages.model')
  6. var tools = require('../../util/tools')
  7. const respondWithResult = (res, statusCode) => {
  8. statusCode = statusCode || 200
  9. return function (entity) {
  10. if (entity) {
  11. return res.status(statusCode).json(entity)
  12. }
  13. return null
  14. }
  15. }
  16. const patchUpdates = (patches) => {
  17. return function (entity) {
  18. try {
  19. jsonpatch.apply(entity, patches, /*validate*/ true)
  20. } catch (err) {
  21. return Promise.reject(err)
  22. }
  23. return entity.save()
  24. }
  25. }
  26. const removeEntity = (res) => {
  27. return function (entity) {
  28. if (entity) {
  29. return entity.remove()
  30. .then(() => {
  31. res.status(204).end()
  32. })
  33. }
  34. }
  35. }
  36. const handleEntityNotFound = (res) => {
  37. return function (entity) {
  38. if (!entity) {
  39. res.status(404).end()
  40. return null
  41. }
  42. return entity
  43. }
  44. }
  45. const handleError = (res, statusCode) => {
  46. statusCode = statusCode || 500
  47. return function (err) {
  48. res.status(statusCode).send(err)
  49. }
  50. }
  51. module.exports.index = (req, res) => {
  52. return Pages.find().exec()
  53. .then(respondWithResult(res))
  54. .catch(handleError(res))
  55. }
  56. module.exports.findByLoginId = (req, res) => {
  57. var loginId = req.user.loginId
  58. var type = req.query.type;
  59. return Pages.find({ loginId: loginId, type: type }).exec()
  60. .then(respondWithResult(res))
  61. .catch(handleError(res))
  62. }
  63. // Gets a single Pages from the DB
  64. module.exports.show = (req, res) => {
  65. return Pages.findById(req.params.id).exec()
  66. .then(handleEntityNotFound(res))
  67. .then(respondWithResult(res))
  68. .catch(handleError(res))
  69. }
  70. // Creates a new Pages in the DB
  71. module.exports.create = (req, res) => {
  72. //添加作者信息
  73. req.body.loginId = req.user.loginId
  74. return Pages.create(req.body)
  75. .then(respondWithResult(res, 201))
  76. .catch(handleError(res))
  77. }
  78. // Upserts the given Pages in the DB at the specified ID
  79. module.exports.update = (req, res) => {
  80. if (req.body._id) {
  81. delete req.body._id
  82. }
  83. if (req.body.type === 'h5') {
  84. tools.renderFile('template.html', req.body, (html) => {
  85. tools.saveFile(req.params.id + '.html', html)
  86. })
  87. } else if (req.body.type === 'spa') {
  88. tools.renderFile('spa.html', req.body, (html) => {
  89. tools.saveFile(req.params.id + '.html', html)
  90. })
  91. }
  92. return Pages.findOneAndUpdate({ _id: req.params.id }, req.body, { upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec()
  93. .then(respondWithResult(res))
  94. .catch(handleError(res))
  95. }
  96. // Updates an existing Pages in the DB
  97. module.exports.patch = (req, res) => {
  98. if (req.body._id) {
  99. delete req.body._id
  100. }
  101. return Pages.findById(req.params.id).exec()
  102. .then(handleEntityNotFound(res))
  103. .then(patchUpdates(req.body))
  104. .then(respondWithResult(res))
  105. .catch(handleError(res))
  106. }
  107. // Deletes a Pages from the DB
  108. module.exports.destroy = (req, res) => {
  109. return Pages.findById(req.params.id).exec()
  110. .then(handleEntityNotFound(res))
  111. .then(removeEntity(res))
  112. .catch(handleError(res))
  113. }