2014-04-28 11:45:59 -04:00
|
|
|
DocManager = require "./DocManager"
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
|
|
|
module.exports = HttpController =
|
|
|
|
getDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
2014-04-29 06:49:09 -04:00
|
|
|
doc_id = req.params.doc_id
|
2014-04-28 11:45:59 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "getting doc"
|
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
|
|
|
return next(error) if error?
|
|
|
|
if !doc?
|
|
|
|
res.send 404
|
|
|
|
else
|
2014-04-30 08:06:12 -04:00
|
|
|
res.json HttpController._buildDocView(doc)
|
|
|
|
|
2014-05-20 08:04:33 -04:00
|
|
|
getRawDoc: (req, res, next = (error)->)->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
|
|
|
|
DocManager.getDoc project_id, doc_id, (error, doc) ->
|
|
|
|
return next(error) if error?
|
|
|
|
if !doc?
|
|
|
|
res.send 404
|
|
|
|
else
|
|
|
|
res.setHeader('content-type', 'text/plain')
|
|
|
|
res.send HttpController._buildRawDocView(doc)
|
|
|
|
|
2014-04-30 08:06:12 -04:00
|
|
|
getAllDocs: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
logger.log project_id: project_id, "getting all docs"
|
|
|
|
DocManager.getAllDocs project_id, (error, docs = []) ->
|
|
|
|
return next(error) if error?
|
2014-05-07 11:24:47 -04:00
|
|
|
docViews = []
|
|
|
|
for doc in docs
|
|
|
|
if doc? # There can end up being null docs for some reason :( (probably a race condition)
|
|
|
|
docViews.push HttpController._buildDocView(doc)
|
2014-05-07 11:38:10 -04:00
|
|
|
else
|
|
|
|
logger.error err: new Error("null doc"), project_id: project_id, "encountered null doc"
|
2014-05-07 11:24:47 -04:00
|
|
|
res.json docViews
|
2014-04-29 06:49:09 -04:00
|
|
|
|
|
|
|
updateDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
lines = req.body?.lines
|
|
|
|
|
|
|
|
if !lines? or lines not instanceof Array
|
|
|
|
logger.error project_id: project_id, doc_id: doc_id, "no doc lines provided"
|
|
|
|
res.send 400 # Bad Request
|
|
|
|
return
|
|
|
|
|
2014-05-16 08:06:35 -04:00
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "updating doc"
|
|
|
|
DocManager.updateDoc project_id, doc_id, lines, (error, modified, rev) ->
|
2014-04-29 06:49:09 -04:00
|
|
|
return next(error) if error?
|
|
|
|
res.json {
|
|
|
|
modified: modified
|
2014-05-08 10:43:08 -04:00
|
|
|
rev: rev
|
2014-04-29 11:36:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteDoc: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params.project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
|
|
|
|
DocManager.deleteDoc project_id, doc_id, (error) ->
|
|
|
|
return next(error) if error?
|
2014-04-30 08:06:12 -04:00
|
|
|
res.send 204
|
|
|
|
|
|
|
|
_buildDocView: (doc) ->
|
|
|
|
return {
|
2014-05-29 07:04:18 -04:00
|
|
|
_id: doc._id?.toString()
|
2014-04-30 08:06:12 -04:00
|
|
|
lines: doc.lines
|
|
|
|
rev: doc.rev
|
2014-05-20 08:04:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_buildRawDocView: (doc)->
|
2014-05-29 06:18:31 -04:00
|
|
|
return (doc?.lines or []).join("\n")
|