route.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Project from './model'
  2. import mongoose from 'mongoose'
  3. const routing = {
  4. /** GET handler (request one or more projects) */
  5. get: (req, res, next) => {
  6. const path = req.params['0']
  7. let id = req.params['1']
  8. console.log(req.params)
  9. /** If the path doesn't match, call the next router */
  10. if (path !== '/project') {
  11. next()
  12. }
  13. /** check whether an id was specified. */
  14. if (typeof id !== 'undefined') {
  15. try {
  16. id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  17. } catch (err) {
  18. res.status(422)
  19. res.send({error: err.message})
  20. return
  21. }
  22. console.log(id)
  23. /** if yes, return the one project */
  24. Project.findOne({ _id: id }, function (err, project) {
  25. if (err) {
  26. res.status(404)
  27. res.send(err)
  28. return
  29. }
  30. res.json(project)
  31. })
  32. } else {
  33. /** if not, return all projects */
  34. /** @todo: add some pagination here */
  35. /** @todo: add some filtering here */
  36. Project.find(function (err, projects) {
  37. if (err) {
  38. res.status(404)
  39. res.send(err)
  40. return
  41. }
  42. res.json(projects)
  43. })
  44. }
  45. },
  46. /** POST handler (insert new projects into database) */
  47. post: (req, res, next) => {
  48. const path = req.params['0']
  49. // const id = req.params['1']
  50. /** If the path doesn't match, call the next router */
  51. if (path !== '/project') {
  52. next()
  53. }
  54. const project = new Project(req.body)
  55. project.save(function (err) {
  56. if (err) {
  57. res.status(422)
  58. res.send(err)
  59. }
  60. res.send({ success: 'Project added.' })
  61. })
  62. },
  63. /** PUT handler (update existing project) */
  64. put: (req, res, next) => {
  65. const path = req.params['0']
  66. const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  67. /** If the path doesn't match, call the next router */
  68. if (path !== '/project') {
  69. next()
  70. }
  71. Project.findOne({ _id: id }, function (err, project) {
  72. if (err) {
  73. res.status(404)
  74. res.send(err)
  75. }
  76. for (let prop in req.body) {
  77. project[prop] = req.body[prop]
  78. }
  79. project.save(function (err) {
  80. if (err) {
  81. res.status(422)
  82. res.send(err)
  83. }
  84. res.json({ message: 'Movie updated.' })
  85. })
  86. })
  87. },
  88. /** DELETE handler (delete project) */
  89. delete: (req, res, next) => {
  90. const path = req.params['0']
  91. const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  92. /** If the path doesn't match, call the next router */
  93. if (path !== '/project') {
  94. next()
  95. }
  96. Project.remove({ _id: id }, function (err, project) {
  97. if (err) {
  98. res.send(err)
  99. }
  100. res.json({ message: 'Movie deleted.' })
  101. })
  102. }
  103. }
  104. export default routing