2017-09-27 04:37:20 -04:00
|
|
|
express = require("express")
|
|
|
|
bodyParser = require "body-parser"
|
|
|
|
app = express()
|
|
|
|
|
2017-09-27 04:53:00 -04:00
|
|
|
module.exports = MockDocStoreApi =
|
2017-09-27 04:37:20 -04:00
|
|
|
docs: {}
|
|
|
|
|
|
|
|
run: () ->
|
|
|
|
app.post "/project/:project_id/doc/:doc_id", bodyParser.json(), (req, res, next) =>
|
|
|
|
{project_id, doc_id} = req.params
|
|
|
|
{lines, version, ranges} = req.body
|
|
|
|
@docs[project_id] ?= {}
|
|
|
|
@docs[project_id][doc_id] = {lines, version, ranges}
|
|
|
|
@docs[project_id][doc_id].rev ?= 0
|
|
|
|
@docs[project_id][doc_id].rev += 1
|
2017-11-27 12:09:51 -05:00
|
|
|
@docs[project_id][doc_id]._id = doc_id
|
2017-09-27 04:37:20 -04:00
|
|
|
res.json {
|
|
|
|
modified: true
|
|
|
|
rev: @docs[project_id][doc_id].rev
|
|
|
|
}
|
|
|
|
|
|
|
|
app.get "/project/:project_id/doc", (req, res, next) =>
|
|
|
|
docs = (doc for doc_id, doc of @docs[req.params.project_id])
|
2019-02-18 08:25:01 -05:00
|
|
|
res.json docs
|
2017-09-27 04:37:20 -04:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
app.get "/project/:project_id/doc/:doc_id", (req, res, next) =>
|
|
|
|
{project_id, doc_id} = req.params
|
|
|
|
doc = @docs[project_id][doc_id]
|
2018-12-03 06:06:31 -05:00
|
|
|
if !doc? or (doc.deleted and !req.query.include_deleted)
|
2018-03-16 08:21:07 -04:00
|
|
|
res.sendStatus 404
|
|
|
|
else
|
2019-02-18 08:25:01 -05:00
|
|
|
res.json doc
|
2018-03-16 08:21:07 -04:00
|
|
|
|
2017-12-14 11:03:57 -05:00
|
|
|
app.delete "/project/:project_id/doc/:doc_id", (req, res, next) =>
|
|
|
|
{project_id, doc_id} = req.params
|
|
|
|
if !@docs[project_id]?
|
2018-03-16 08:21:07 -04:00
|
|
|
res.sendStatus 404
|
2017-12-14 11:03:57 -05:00
|
|
|
else if !@docs[project_id][doc_id]?
|
2018-03-16 08:21:07 -04:00
|
|
|
res.sendStatus 404
|
2017-12-14 11:03:57 -05:00
|
|
|
else
|
2018-03-16 08:21:07 -04:00
|
|
|
@docs[project_id][doc_id].deleted = true
|
|
|
|
res.sendStatus 204
|
2017-12-14 11:03:57 -05:00
|
|
|
|
2017-09-27 04:37:20 -04:00
|
|
|
app.listen 3016, (error) ->
|
|
|
|
throw error if error?
|
2017-10-20 10:31:10 -04:00
|
|
|
.on "error", (error) ->
|
|
|
|
console.error "error starting MockDocStoreApi:", error.message
|
|
|
|
process.exit(1)
|
2017-09-27 04:37:20 -04:00
|
|
|
|
2017-10-20 10:31:10 -04:00
|
|
|
|
|
|
|
MockDocStoreApi.run()
|
2017-09-27 04:37:20 -04:00
|
|
|
|