route.js 2.9 KB

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