Просмотр исходного кода

Add in DocManager.deleteDoc method

James Allen 12 лет назад
Родитель
Сommit
885d5de191

+ 8 - 0
services/docstore/app/coffee/DocManager.coffee

@@ -31,6 +31,14 @@ module.exports = DocManager =
 					return callback(error) if error?
 					callback null, true
 
+	deleteDoc: (project_id, doc_id, callback = (error) ->) ->
+		DocManager.getDoc project_id, doc_id, (error, doc) ->
+			return callback(error) if error?
+			return callback new Errors.NotFoundError("No such project/doc: #{project_id}/#{doc_id}") if !doc?
+			MongoManager.insertDoc project_id, doc_id, { lines: doc.lines, deleted: true }, (error) ->
+				return callback(error) if error?
+				callback()
+
 	findDocInProject: (project, doc_id, callback = (error, doc, mongoPath) ->) ->
 		result = @_findDocInFolder project.rootFolder[0], doc_id, "rootFolder.0"
 		if result?

+ 6 - 1
services/docstore/app/coffee/MongoManager.coffee

@@ -12,4 +12,9 @@ module.exports = MongoManager =
 		update.$set["#{docPath}.lines"] = lines
 		update.$inc["#{docPath}.rev"] = 1
 
-		db.projects.update _id: ObjectId(project_id), update, callback
+		db.projects.update _id: ObjectId(project_id), update, callback
+
+	insertDoc: (project_id, doc_id, attributes, callback = (error) ->) ->
+		attributes._id = ObjectId(doc_id)
+		attributes.project_id = ObjectId(project_id)
+		db.projects.insert attributes, callback

+ 38 - 0
services/docstore/test/unit/coffee/DocManagerTests.coffee

@@ -70,6 +70,44 @@ describe "DocManager", ->
 					.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
 					.should.equal true
 
+	describe "deleteDoc", ->
+		describe "when the doc exists", ->
+			beforeEach ->
+				@lines = ["mock", "doc", "lines"]
+				@DocManager.getDoc = sinon.stub().callsArgWith(2, null, lines: @lines)
+				@MongoManager.insertDoc = sinon.stub().callsArg(3)
+				@DocManager.deleteDoc @project_id, @doc_id, @callback
+
+			it "should get the doc", ->
+				@DocManager.getDoc
+					.calledWith(@project_id, @doc_id)
+					.should.equal true
+
+			it "insert the doc as a deleted doc", ->
+				@MongoManager.insertDoc
+					.calledWith(@project_id, @doc_id, {
+						lines: @lines
+						deleted: true
+					})
+					.should.equal true
+
+			it "should return the callback", ->
+				@callback.called.should.equal true
+
+		describe "when the doc does not exist", ->
+			beforeEach -> 
+				@DocManager.getDoc = sinon.stub().callsArgWith(2, null, null)
+				@MongoManager.insertDoc = sinon.stub().callsArg(3)
+				@DocManager.deleteDoc @project_id, @doc_id, @callback
+
+			it "should not try to insert a deleted doc", ->
+				@MongoManager.insertDoc.called.should.equal false
+
+			it "should return a NotFoundError", ->
+				@callback
+					.calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}"))
+					.should.equal true
+
 	describe "updateDoc", ->
 		beforeEach ->
 			@oldDocLines = ["old", "doc", "lines"]

+ 19 - 0
services/docstore/test/unit/coffee/MongoManagerTests.coffee

@@ -50,3 +50,22 @@ describe "MongoManager", ->
 
 		it "should call the callback with the project", ->
 			@callback.called.should.equal true
+
+	describe "insertDoc", ->
+		beforeEach ->
+			@doc_id = ObjectId().toString()
+			@lines = ["mock-lines"]
+			@db.projects.insert = sinon.stub().callsArg(1)
+			@MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback
+
+		it "should insert the attributes with the given doc and project id", ->
+			@db.projects.insert
+				.calledWith({
+					_id: ObjectId(@doc_id)
+					project_id: ObjectId(@project_id)
+					lines: @lines
+				})
+				.should.equal true
+
+		it "should call the callback", ->
+			@callback.called.should.equal true