From 98ff17a07c62b7c400fd46095ee653e991336ac1 Mon Sep 17 00:00:00 2001 From: Martin Kleppmann Date: Wed, 17 Jun 2009 13:21:40 +0100 Subject: [PATCH] Delete nodes via REST; added specs --- lib/rest/rest.rb | 17 ++++++++++++++++- test/rest/rest_spec.rb | 30 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/rest/rest.rb b/lib/rest/rest.rb index 93e19f87a..67f893c57 100644 --- a/lib/rest/rest.rb +++ b/lib/rest/rest.rb @@ -140,6 +140,13 @@ def self.included(c) response end + Sinatra::Application.delete("/nodes/#{classname}/:id") do + node = Neo4j.load(params[:id]) + return 404, "Can't find node with id #{params[:id]}" if node.nil? + node.delete + "" + end + Sinatra::Application.post("/nodes/#{classname}") do p = c.new data = JSON.parse(request.body.read) @@ -149,7 +156,15 @@ def self.included(c) Sinatra::Application.get("/nodes/#{classname}") do content_type :json - c.all.map{|rel| rel.end_node.props}.to_json + Neo4j::Transaction.run { + resources = if params[:search].nil? + c.all.map{|rel| rel.end_node} + else + puts "Search string: #{params[:search]}" + c.find(params[:search]) + end + resources.map{|res| res.props}.to_json + } end end end diff --git a/test/rest/rest_spec.rb b/test/rest/rest_spec.rb index 83326df95..c0bc34544 100644 --- a/test/rest/rest_spec.rb +++ b/test/rest/rest_spec.rb @@ -170,6 +170,35 @@ class Foo status.should == 404 end + it "should be possible to set all properties on PUT nodes/Person/" do + # given + p = Person.new + p.name = 'sune123' + + # when + data = {:name => 'blah', :dynamic_property => 'cool stuff'} + put "/nodes/Person/#{p.neo_node_id}", data.to_json + + # then + status.should == 200 + p.name.should == 'blah' + p.props['dynamic_property'].should == 'cool stuff' + end + + it "should be possible to delete a node on DELETE nodes/Person/" do + # given + p = Person.new + p.name = 'asdf' + id = p.neo_node_id + + # when + delete "/nodes/Person/#{id}" + + # then + status.should == 200 + Neo4j.load(id).should be_nil + end + it "should be possible to get a property on GET nodes/Person//" do # given p = Person.new @@ -197,5 +226,4 @@ class Foo status.should == 200 p.name.should == 'new-name' end - end \ No newline at end of file