HttpController.coffee 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. DocManager = require "./DocManager"
  2. logger = require "logger-sharelatex"
  3. module.exports = HttpController =
  4. getDoc: (req, res, next = (error) ->) ->
  5. project_id = req.params.project_id
  6. doc_id = req.params.doc_id
  7. logger.log project_id: project_id, doc_id: doc_id, "getting doc"
  8. DocManager.getDoc project_id, doc_id, (error, doc) ->
  9. return next(error) if error?
  10. if !doc?
  11. res.send 404
  12. else
  13. res.json HttpController._buildDocView(doc)
  14. getAllDocs: (req, res, next = (error) ->) ->
  15. project_id = req.params.project_id
  16. logger.log project_id: project_id, "getting all docs"
  17. DocManager.getAllDocs project_id, (error, docs = []) ->
  18. return next(error) if error?
  19. docViews = []
  20. for doc in docs
  21. if doc? # There can end up being null docs for some reason :( (probably a race condition)
  22. docViews.push HttpController._buildDocView(doc)
  23. res.json docViews
  24. updateDoc: (req, res, next = (error) ->) ->
  25. project_id = req.params.project_id
  26. doc_id = req.params.doc_id
  27. lines = req.body?.lines
  28. if !lines? or lines not instanceof Array
  29. logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
  30. res.send 400 # Bad Request
  31. return
  32. logger.log project_id: project_id, doc_id: doc_id, "updating doc"
  33. DocManager.updateDoc project_id, doc_id, lines, (error, modified) ->
  34. return next(error) if error?
  35. res.json {
  36. modified: modified
  37. }
  38. deleteDoc: (req, res, next = (error) ->) ->
  39. project_id = req.params.project_id
  40. doc_id = req.params.doc_id
  41. logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
  42. DocManager.deleteDoc project_id, doc_id, (error) ->
  43. return next(error) if error?
  44. res.send 204
  45. _buildDocView: (doc) ->
  46. return {
  47. _id: doc._id.toString()
  48. lines: doc.lines
  49. rev: doc.rev
  50. version: doc.version
  51. }