Add endpoint for getting all docs

This commit is contained in:
James Allen
2014-04-30 13:06:12 +01:00
parent 984999ec61
commit fa049ff4eb
11 changed files with 232 additions and 27 deletions
@@ -5,12 +5,16 @@ chai.should()
DocstoreClient = require "./helpers/DocstoreClient"
describe "Applying updates to a doc", ->
describe "Deleting a doc", ->
beforeEach (done) ->
@project_id = ObjectId()
@doc_id = ObjectId()
@lines = ["original", "lines"]
DocstoreClient.createDoc @project_id, @lines, (error, @doc_id) =>
done()
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()
afterEach (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"
describe "Applying updates to a doc", ->
describe "Getting a doc", ->
beforeEach (done) ->
@project_id = ObjectId()
@doc_id = ObjectId()
@lines = ["original", "lines"]
DocstoreClient.createDoc @project_id, @lines, (error, @doc_id) =>
done()
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()
afterEach (done) ->
DocstoreClient.deleteProject @project_id, done
@@ -8,10 +8,14 @@ DocstoreClient = require "./helpers/DocstoreClient"
describe "Applying updates to a doc", ->
beforeEach (done) ->
@project_id = ObjectId()
@doc_id = ObjectId()
@originalLines = ["original", "lines"]
@newLines = ["new", "lines"]
DocstoreClient.createDoc @project_id, @originalLines, (error, @doc_id) =>
done()
DocstoreClient.createProject @project_id, (error) =>
throw error if error?
DocstoreClient.createDoc @project_id, @doc_id, @lines, (error) =>
throw error if error?
done()
afterEach (done) ->
DocstoreClient.deleteProject @project_id, done
@@ -2,29 +2,39 @@ request = require("request").defaults(jar: false)
{db, ObjectId} = require("../../../../app/js/mongojs")
module.exports = DocstoreClient =
createDoc: (project_id, lines, callback = (error, doc_id) ->) ->
doc_id = ObjectId()
createProject: (project_id, callback = (error) ->) ->
db.projects.insert {
_id: project_id
rootFolder: [{
docs: [{
_id: doc_id,
rootFolder: [{ docs: [] }]
}, callback
createDoc: (project_id, doc_id, lines, callback = (error) ->) ->
db.projects.update {
_id: project_id
}, {
$push: {
"rootFolder.0.docs": {
_id: doc_id
lines: lines
}]
}]
}, (error) ->
return callback(error) if error?
callback null, doc_id
}
}
}, callback
deleteProject: (project_id, callback = (error, res, body) ->) ->
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 {
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
json: true
}, 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) ->) ->
request.post {
url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"