route.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. return
  60. }
  61. res.send({ success: 'Project added.' })
  62. })
  63. },
  64. /** PUT handler (update existing project) */
  65. put: (req, res, next) => {
  66. const path = req.params['0']
  67. const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  68. /** If the path doesn't match, call the next router */
  69. if (path !== '/project') {
  70. next()
  71. }
  72. Project.findOne({ _id: id }, function (err, project) {
  73. if (err) {
  74. res.status(404)
  75. res.send(err)
  76. }
  77. for (let prop in req.body) {
  78. project[prop] = req.body[prop]
  79. }
  80. project.save(function (err) {
  81. if (err) {
  82. res.status(422)
  83. res.send(err)
  84. }
  85. res.json({ message: 'Movie updated.' })
  86. })
  87. })
  88. },
  89. /** DELETE handler (delete project) */
  90. delete: (req, res, next) => {
  91. const path = req.params['0']
  92. const id = mongoose.Types.ObjectId(req.params['1'].substring(1))
  93. /** If the path doesn't match, call the next router */
  94. if (path !== '/project') {
  95. next()
  96. }
  97. Project.remove({ _id: id }, function (err, project) {
  98. if (err) {
  99. res.send(err)
  100. }
  101. res.json({ message: 'Movie deleted.' })
  102. })
  103. }
  104. }
  105. export default routing