routeGen.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import mongoose from 'mongoose'
  2. import { History } from './basicSchema'
  3. function routeGen (model) {
  4. /** GET handler (request one item or a list of items) */
  5. function get (req, res, next) {
  6. const path = req.params['0']
  7. /** If the path doesn't match, call the next router */
  8. if (path !== model.modelName.toLowerCase()) {
  9. next()
  10. }
  11. let id = req.params['1']
  12. /** check whether an id was specified. */
  13. if (typeof id === 'string' && id.length > 1) {
  14. // remove leading slash
  15. id = id.substring(1)
  16. try {
  17. // try to convert the id string to a valid ObjectId
  18. id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  19. } catch (err) {
  20. // If id couldn't be converted, return an error message.
  21. res.status(422)
  22. res.send({error: err.message})
  23. return
  24. }
  25. // if yes, return the one item
  26. model.findOne({ _id: id }, function (err, item) {
  27. if (err) {
  28. res.status(404)
  29. res.send(err)
  30. return
  31. }
  32. res.json(item)
  33. })
  34. } else {
  35. // if not, return a list of items
  36. // limit the number of returned items and use an offset
  37. const limit = req.query.limit ? Math.min(Math.max(parseInt(req.query.limit), 1), 100) : 20
  38. const offset = req.query.offset ? Math.min(Math.max(parseInt(req.query.offset), 1), 100) : 20
  39. model.find({}, {limit: limit, skip: offset}, function (err, items) {
  40. if (err) {
  41. res.status(404)
  42. res.send(err)
  43. return
  44. }
  45. res.json(items)
  46. })
  47. }
  48. }
  49. /** POST handler (insert a new item into the database) */
  50. function post (req, res, next) {
  51. const path = req.params['0']
  52. // If the path doesn't match, call the next router
  53. if (path !== model.modelName.toLowerCase()) {
  54. next()
  55. }
  56. // create the new item
  57. const item = new model(req.body)
  58. // Check, if the model supports revision control
  59. if (typeof model.schema.obj.__history !== 'undefined') {
  60. item.__history = new History({
  61. author: '',
  62. created: Date(),
  63. tag: '',
  64. reference: [],
  65. head: true
  66. })
  67. }
  68. // save the item
  69. item.save(function (err) {
  70. if (err) {
  71. res.status(422)
  72. res.send(err)
  73. return
  74. }
  75. res.send({ success: `${model.modelName} added.` })
  76. })
  77. }
  78. /** PUT handler (update existing project) */
  79. function put (req, res, next) {
  80. const path = req.params['0']
  81. /** If the path doesn't match, call the next router */
  82. if (path !== model.modelName.toLowerCase()) {
  83. next()
  84. }
  85. let id = req.params['1']
  86. /** check whether an id was specified. */
  87. if (typeof id === 'string' && id.length > 1) {
  88. // remove leading slash
  89. id = id.substring(1)
  90. try {
  91. // try to convert the id string to a valid ObjectId
  92. id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  93. } catch (err) {
  94. // If id couldn't be converted, return an error message.
  95. res.status(422)
  96. res.send({error: err.message})
  97. return
  98. }
  99. }
  100. // Find the object in the database
  101. model.findOne({ _id: id }, function (err, item) {
  102. if (err) {
  103. res.status(404)
  104. res.send(err)
  105. }
  106. // Check, if the model supports revision control
  107. if (typeof model.schema.obj.__history !== 'undefined') {
  108. // If yes, don't update the item, but create a new one based on the original
  109. const newItem = model(item)
  110. // replace the requested elements
  111. for (let prop in req.body) {
  112. newItem[prop] = req.body[prop]
  113. }
  114. // give it a new history with updated reference array. Set the head-indicator to true
  115. newItem.__history = new History({
  116. author: '',
  117. created: Date(),
  118. tag: '',
  119. reference: [ ...item.__history.reference, item._id ],
  120. head: true
  121. })
  122. // set the original item's head indicator to false
  123. item.__history.head = false
  124. // save the documents
  125. newItem.save(function (err) {
  126. if (err) {
  127. res.status(422)
  128. res.send(err)
  129. }
  130. res.json({ message: `${model.modelName} updated.` })
  131. })
  132. } else {
  133. for (let prop in req.body) {
  134. item[prop] = req.body[prop]
  135. }
  136. }
  137. item.save(function (err) {
  138. if (err) {
  139. res.status(422)
  140. res.send(err)
  141. }
  142. res.json({ message: `${model.modelName} updated.` })
  143. })
  144. })
  145. },
  146. /** DELETE handler (delete project) */
  147. delete: (req, res, next) => {
  148. const path = req.params['0']
  149. const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  150. /** If the path doesn't match, call the next router */
  151. if (path !== '/project') {
  152. next()
  153. }
  154. Project.remove({ _id: id }, function (err, project) {
  155. if (err) {
  156. res.send(err)
  157. }
  158. res.json({ message: 'Movie deleted.' })
  159. })
  160. }
  161. }
  162. export default routeGen