Add endpoint for getting all docs
This commit is contained in:
@@ -9,6 +9,7 @@ logger.initialize("docstore")
|
|||||||
|
|
||||||
app = express()
|
app = express()
|
||||||
|
|
||||||
|
app.get '/project/:project_id/doc', HttpController.getAllDocs
|
||||||
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
|
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
|
||||||
app.post '/project/:project_id/doc/:doc_id', bodyParser.json(), HttpController.updateDoc
|
app.post '/project/:project_id/doc/:doc_id', bodyParser.json(), HttpController.updateDoc
|
||||||
app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc
|
app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc
|
||||||
|
|||||||
@@ -13,6 +13,14 @@ module.exports = DocManager =
|
|||||||
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc?
|
return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc?
|
||||||
return callback null, doc, mongoPath
|
return callback null, doc, mongoPath
|
||||||
|
|
||||||
|
getAllDocs: (project_id, callback = (error, docs) ->) ->
|
||||||
|
MongoManager.findProject project_id, (error, project) ->
|
||||||
|
return callback(error) if error?
|
||||||
|
return callback new Errors.NotFoundError("No such project: #{project_id}") if !project?
|
||||||
|
DocManager.findAllDocsInProject project, (error, docs) ->
|
||||||
|
return callback(error) if error?
|
||||||
|
return callback null, docs
|
||||||
|
|
||||||
updateDoc: (project_id, doc_id, lines, callback = (error, modified) ->) ->
|
updateDoc: (project_id, doc_id, lines, callback = (error, modified) ->) ->
|
||||||
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
|
DocManager.getDoc project_id, doc_id, (error, doc, mongoPath) ->
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
@@ -39,6 +47,9 @@ module.exports = DocManager =
|
|||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
callback()
|
callback()
|
||||||
|
|
||||||
|
findAllDocsInProject: (project, callback = (error, docs) ->) ->
|
||||||
|
callback null, @_findAllDocsInFolder project.rootFolder[0]
|
||||||
|
|
||||||
findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
|
findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
|
||||||
result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
|
result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
|
||||||
if result?
|
if result?
|
||||||
@@ -58,4 +69,11 @@ module.exports = DocManager =
|
|||||||
result = @_findDocInFolder childFolder, doc_id, "#{currentPath}.folders.#{i}"
|
result = @_findDocInFolder childFolder, doc_id, "#{currentPath}.folders.#{i}"
|
||||||
return result if result?
|
return result if result?
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
|
||||||
|
_findAllDocsInFolder: (folder) ->
|
||||||
|
docs = folder.docs or []
|
||||||
|
for childFolder in folder.folders or []
|
||||||
|
docs = docs.concat @_findAllDocsInFolder childFolder
|
||||||
|
return docs
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,14 @@ module.exports = HttpController =
|
|||||||
if !doc?
|
if !doc?
|
||||||
res.send 404
|
res.send 404
|
||||||
else
|
else
|
||||||
res.json {
|
res.json HttpController._buildDocView(doc)
|
||||||
lines: doc.lines
|
|
||||||
}
|
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?
|
||||||
|
res.json docs.map(HttpController._buildDocView)
|
||||||
|
|
||||||
updateDoc: (req, res, next = (error) ->) ->
|
updateDoc: (req, res, next = (error) ->) ->
|
||||||
project_id = req.params.project_id
|
project_id = req.params.project_id
|
||||||
@@ -38,4 +43,12 @@ module.exports = HttpController =
|
|||||||
logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
|
logger.log project_id: project_id, doc_id: doc_id, "deleting doc"
|
||||||
DocManager.deleteDoc project_id, doc_id, (error) ->
|
DocManager.deleteDoc project_id, doc_id, (error) ->
|
||||||
return next(error) if error?
|
return next(error) if error?
|
||||||
res.send 204
|
res.send 204
|
||||||
|
|
||||||
|
_buildDocView: (doc) ->
|
||||||
|
return {
|
||||||
|
_id: doc._id.toString()
|
||||||
|
lines: doc.lines
|
||||||
|
rev: doc.rev
|
||||||
|
version: doc.version
|
||||||
|
}
|
||||||
@@ -9,7 +9,8 @@
|
|||||||
"mongojs": "0.9.11",
|
"mongojs": "0.9.11",
|
||||||
"express": "~4.1.1",
|
"express": "~4.1.1",
|
||||||
"underscore": "~1.6.0",
|
"underscore": "~1.6.0",
|
||||||
"body-parser": "~1.0.2"
|
"body-parser": "~1.0.2",
|
||||||
|
"async": "~0.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"grunt-execute": "~0.2.1",
|
"grunt-execute": "~0.2.1",
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ chai.should()
|
|||||||
|
|
||||||
DocstoreClient = require "./helpers/DocstoreClient"
|
DocstoreClient = require "./helpers/DocstoreClient"
|
||||||
|
|
||||||
describe "Applying updates to a doc", ->
|
describe "Deleting a doc", ->
|
||||||
beforeEach (done) ->
|
beforeEach (done) ->
|
||||||
@project_id = ObjectId()
|
@project_id = ObjectId()
|
||||||
|
@doc_id = ObjectId()
|
||||||
@lines = ["original", "lines"]
|
@lines = ["original", "lines"]
|
||||||
DocstoreClient.createDoc @project_id, @lines, (error, @doc_id) =>
|
DocstoreClient.createProject @project_id, (error) =>
|
||||||
done()
|
throw error if error?
|
||||||
|
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
|
||||||
|
throw error if error?
|
||||||
|
done()
|
||||||
|
|
||||||
afterEach (done) ->
|
afterEach (done) ->
|
||||||
DocstoreClient.deleteProject @project_id, done
|
DocstoreClient.deleteProject @project_id, done
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
sinon = require "sinon"
|
||||||
|
chai = require("chai")
|
||||||
|
chai.should()
|
||||||
|
{ObjectId} = require "mongojs"
|
||||||
|
async = require "async"
|
||||||
|
|
||||||
|
DocstoreClient = require "./helpers/DocstoreClient"
|
||||||
|
|
||||||
|
describe "Getting all docs", ->
|
||||||
|
beforeEach (done) ->
|
||||||
|
@project_id = ObjectId()
|
||||||
|
@docs = [{
|
||||||
|
_id: ObjectId()
|
||||||
|
lines: ["one"]
|
||||||
|
version: 1
|
||||||
|
rev: 2
|
||||||
|
}, {
|
||||||
|
_id: ObjectId()
|
||||||
|
lines: ["two"]
|
||||||
|
version: 3
|
||||||
|
rev: 4
|
||||||
|
}, {
|
||||||
|
_id: ObjectId()
|
||||||
|
lines: ["three"]
|
||||||
|
version: 5
|
||||||
|
rev: 6
|
||||||
|
}]
|
||||||
|
DocstoreClient.createProject @project_id, (error) =>
|
||||||
|
throw error if error?
|
||||||
|
jobs = for doc in @docs
|
||||||
|
do (doc) =>
|
||||||
|
(callback) => DocstoreClient.createDoc @project_id, doc._id, doc.lines, callback
|
||||||
|
async.series jobs, done
|
||||||
|
|
||||||
|
afterEach (done) ->
|
||||||
|
DocstoreClient.deleteProject @project_id, done
|
||||||
|
|
||||||
|
it "should return all the docs", (done) ->
|
||||||
|
DocstoreClient.getAllDocs @project_id, (error, res, docs) =>
|
||||||
|
throw error if error?
|
||||||
|
docs.length.should.equal @docs.length
|
||||||
|
for doc, i in docs
|
||||||
|
doc.lines.should.deep.equal @docs[i].lines
|
||||||
|
done()
|
||||||
|
|
||||||
@@ -5,12 +5,16 @@ chai.should()
|
|||||||
|
|
||||||
DocstoreClient = require "./helpers/DocstoreClient"
|
DocstoreClient = require "./helpers/DocstoreClient"
|
||||||
|
|
||||||
describe "Applying updates to a doc", ->
|
describe "Getting a doc", ->
|
||||||
beforeEach (done) ->
|
beforeEach (done) ->
|
||||||
@project_id = ObjectId()
|
@project_id = ObjectId()
|
||||||
|
@doc_id = ObjectId()
|
||||||
@lines = ["original", "lines"]
|
@lines = ["original", "lines"]
|
||||||
DocstoreClient.createDoc @project_id, @lines, (error, @doc_id) =>
|
DocstoreClient.createProject @project_id, (error) =>
|
||||||
done()
|
throw error if error?
|
||||||
|
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
|
||||||
|
throw error if error?
|
||||||
|
done()
|
||||||
|
|
||||||
afterEach (done) ->
|
afterEach (done) ->
|
||||||
DocstoreClient.deleteProject @project_id, done
|
DocstoreClient.deleteProject @project_id, done
|
||||||
|
|||||||
@@ -8,10 +8,14 @@ DocstoreClient = require "./helpers/DocstoreClient"
|
|||||||
describe "Applying updates to a doc", ->
|
describe "Applying updates to a doc", ->
|
||||||
beforeEach (done) ->
|
beforeEach (done) ->
|
||||||
@project_id = ObjectId()
|
@project_id = ObjectId()
|
||||||
|
@doc_id = ObjectId()
|
||||||
@originalLines = ["original", "lines"]
|
@originalLines = ["original", "lines"]
|
||||||
@newLines = ["new", "lines"]
|
@newLines = ["new", "lines"]
|
||||||
DocstoreClient.createDoc @project_id, @originalLines, (error, @doc_id) =>
|
DocstoreClient.createProject @project_id, (error) =>
|
||||||
done()
|
throw error if error?
|
||||||
|
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
|
||||||
|
throw error if error?
|
||||||
|
done()
|
||||||
|
|
||||||
afterEach (done) ->
|
afterEach (done) ->
|
||||||
DocstoreClient.deleteProject @project_id, done
|
DocstoreClient.deleteProject @project_id, done
|
||||||
|
|||||||
@@ -2,29 +2,39 @@ request = require("request").defaults(jar: false)
|
|||||||
{db, ObjectId} = require("../../../../app/js/mongojs")
|
{db, ObjectId} = require("../../../../app/js/mongojs")
|
||||||
|
|
||||||
module.exports = DocstoreClient =
|
module.exports = DocstoreClient =
|
||||||
createDoc: (project_id, lines, callback = (error, doc_id) ->) ->
|
createProject: (project_id, callback = (error) ->) ->
|
||||||
doc_id = ObjectId()
|
|
||||||
db.projects.insert {
|
db.projects.insert {
|
||||||
_id: project_id
|
_id: project_id
|
||||||
rootFolder: [{
|
rootFolder: [{ docs: [] }]
|
||||||
docs: [{
|
}, callback
|
||||||
_id: doc_id,
|
|
||||||
|
createDoc: (project_id, doc_id, lines, callback = (error) ->) ->
|
||||||
|
db.projects.update {
|
||||||
|
_id: project_id
|
||||||
|
}, {
|
||||||
|
$push: {
|
||||||
|
"rootFolder.0.docs": {
|
||||||
|
_id: doc_id
|
||||||
lines: lines
|
lines: lines
|
||||||
}]
|
}
|
||||||
}]
|
}
|
||||||
}, (error) ->
|
}, callback
|
||||||
return callback(error) if error?
|
|
||||||
callback null, doc_id
|
|
||||||
|
|
||||||
deleteProject: (project_id, callback = (error, res, body) ->) ->
|
deleteProject: (project_id, callback = (error, res, body) ->) ->
|
||||||
db.projects.remove _id: project_id, callback
|
db.projects.remove _id: project_id, callback
|
||||||
|
|
||||||
getDoc: (project_id, doc_id, callback = (error, doc) ->) ->
|
getDoc: (project_id, doc_id, callback = (error, res, body) ->) ->
|
||||||
request.get {
|
request.get {
|
||||||
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
|
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
|
||||||
json: true
|
json: true
|
||||||
}, callback
|
}, callback
|
||||||
|
|
||||||
|
getAllDocs: (project_id, callback = (error, res, body) ->) ->
|
||||||
|
request.get {
|
||||||
|
url: "http://localhost:3016/project/#{project_id}/doc"
|
||||||
|
json: true
|
||||||
|
}, callback
|
||||||
|
|
||||||
updateDoc: (project_id, doc_id, lines, callback = (error, res, body) ->) ->
|
updateDoc: (project_id, doc_id, lines, callback = (error, res, body) ->) ->
|
||||||
request.post {
|
request.post {
|
||||||
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
|
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ describe "DocManager", ->
|
|||||||
|
|
||||||
describe "when the project does not exist", ->
|
describe "when the project does not exist", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @null)
|
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
|
||||||
@DocManager.findDocInProject = sinon.stub()
|
@DocManager.findDocInProject = sinon.stub()
|
||||||
@DocManager.getDoc @project_id, @doc_id, @callback
|
@DocManager.getDoc @project_id, @doc_id, @callback
|
||||||
|
|
||||||
@@ -70,6 +70,43 @@ describe "DocManager", ->
|
|||||||
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
|
.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
|
describe "getAllDocs", ->
|
||||||
|
describe "when the project exists", ->
|
||||||
|
beforeEach ->
|
||||||
|
@project = { name: "mock-project" }
|
||||||
|
@docs = [{ _id: @doc_id, lines: ["mock-lines"] }]
|
||||||
|
@mongoPath = "mock.mongo.path"
|
||||||
|
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project)
|
||||||
|
@DocManager.findAllDocsInProject = sinon.stub().callsArgWith(1, null, @docs)
|
||||||
|
@DocManager.getAllDocs @project_id, @callback
|
||||||
|
|
||||||
|
it "should get the project from the database", ->
|
||||||
|
@MongoManager.findProject
|
||||||
|
.calledWith(@project_id)
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
it "should find all the docs in the project", ->
|
||||||
|
@DocManager.findAllDocsInProject
|
||||||
|
.calledWith(@project)
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
it "should return the docs", ->
|
||||||
|
@callback.calledWith(null, @docs).should.equal true
|
||||||
|
|
||||||
|
describe "when the project does not exist", ->
|
||||||
|
beforeEach ->
|
||||||
|
@MongoManager.findProject = sinon.stub().callsArgWith(1, null, null)
|
||||||
|
@DocManager.findAllDocsInProject = sinon.stub()
|
||||||
|
@DocManager.getDoc @project_id, @doc_id, @callback
|
||||||
|
|
||||||
|
it "should not try to find the doc in the project", ->
|
||||||
|
@DocManager.findAllDocsInProject.called.should.equal false
|
||||||
|
|
||||||
|
it "should return a NotFoundError", ->
|
||||||
|
@callback
|
||||||
|
.calledWith(new Errors.NotFoundError("No such project: #{@project_id}"))
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
describe "deleteDoc", ->
|
describe "deleteDoc", ->
|
||||||
describe "when the doc exists", ->
|
describe "when the doc exists", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@@ -232,3 +269,25 @@ describe "DocManager", ->
|
|||||||
expect(doc).to.be.null
|
expect(doc).to.be.null
|
||||||
expect(mongoPath).to.be.null
|
expect(mongoPath).to.be.null
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
describe "findAllDocsInProject", ->
|
||||||
|
it "should return all the docs", (done) ->
|
||||||
|
@DocManager.findAllDocsInProject {
|
||||||
|
rootFolder: [{
|
||||||
|
docs: [
|
||||||
|
@doc1 = { _id: ObjectId() }
|
||||||
|
],
|
||||||
|
folders: [{
|
||||||
|
docs: [
|
||||||
|
@doc2 = { _id: ObjectId() }
|
||||||
|
]
|
||||||
|
}, {
|
||||||
|
docs: [
|
||||||
|
@doc3 = { _id: ObjectId() }
|
||||||
|
@doc4 = { _id: ObjectId() }
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}, (error, docs) =>
|
||||||
|
expect(docs).to.deep.equal [@doc1, @doc2, @doc3, @doc4]
|
||||||
|
done()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ chai = require('chai')
|
|||||||
chai.should()
|
chai.should()
|
||||||
expect = chai.expect
|
expect = chai.expect
|
||||||
modulePath = require('path').join __dirname, '../../../app/js/HttpController'
|
modulePath = require('path').join __dirname, '../../../app/js/HttpController'
|
||||||
|
ObjectId = require("mongojs").ObjectId
|
||||||
|
|
||||||
describe "HttpController", ->
|
describe "HttpController", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@@ -18,6 +19,8 @@ describe "HttpController", ->
|
|||||||
@doc = {
|
@doc = {
|
||||||
_id: @doc_id
|
_id: @doc_id
|
||||||
lines: ["mock", "lines"]
|
lines: ["mock", "lines"]
|
||||||
|
version: 42
|
||||||
|
rev: 5
|
||||||
}
|
}
|
||||||
|
|
||||||
describe "getDoc", ->
|
describe "getDoc", ->
|
||||||
@@ -35,7 +38,50 @@ describe "HttpController", ->
|
|||||||
|
|
||||||
it "should return the doc as JSON", ->
|
it "should return the doc as JSON", ->
|
||||||
@res.json
|
@res.json
|
||||||
.calledWith(lines: @doc.lines)
|
.calledWith({
|
||||||
|
_id: @doc_id
|
||||||
|
lines: @doc.lines
|
||||||
|
rev: @doc.rev
|
||||||
|
version: @doc.version
|
||||||
|
})
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
describe "getAllDocs", ->
|
||||||
|
beforeEach ->
|
||||||
|
@req.params =
|
||||||
|
project_id: @project_id
|
||||||
|
@docs = [{
|
||||||
|
_id: ObjectId()
|
||||||
|
lines: ["mock", "lines", "one"]
|
||||||
|
version: 1
|
||||||
|
rev: 2
|
||||||
|
}, {
|
||||||
|
_id: ObjectId()
|
||||||
|
lines: ["mock", "lines", "two"]
|
||||||
|
version: 3
|
||||||
|
rev: 4
|
||||||
|
}]
|
||||||
|
@DocManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
|
||||||
|
@HttpController.getAllDocs @req, @res, @next
|
||||||
|
|
||||||
|
it "should get all the docs", ->
|
||||||
|
@DocManager.getAllDocs
|
||||||
|
.calledWith(@project_id)
|
||||||
|
.should.equal true
|
||||||
|
|
||||||
|
it "should return the doc as JSON", ->
|
||||||
|
@res.json
|
||||||
|
.calledWith([{
|
||||||
|
_id: @docs[0]._id.toString()
|
||||||
|
lines: @docs[0].lines
|
||||||
|
rev: @docs[0].rev
|
||||||
|
version: @docs[0].version
|
||||||
|
}, {
|
||||||
|
_id: @docs[1]._id.toString()
|
||||||
|
lines: @docs[1].lines
|
||||||
|
rev: @docs[1].rev
|
||||||
|
version: @docs[1].version
|
||||||
|
}])
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
describe "updateDoc", ->
|
describe "updateDoc", ->
|
||||||
|
|||||||
Reference in New Issue
Block a user