]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
Potlatch 1.1a
[rails.git] / app / controllers / node_controller.rb
1 # The NodeController is the RESTful interface to Node objects
2
3 class NodeController < ApplicationController
4   require 'xml/libxml'
5
6   before_filter :authorize, :only => [:create, :update, :delete]
7   before_filter :require_public_data, :only => [:create, :update, :delete]
8   before_filter :check_api_writable, :only => [:create, :update, :delete]
9   before_filter :check_api_readable, :except => [:create, :update, :delete]
10   after_filter :compress_output
11   around_filter :api_call_handle_error, :api_call_timeout
12
13   # Create a node from XML.
14   def create
15     assert_method :put
16
17     node = Node.from_xml(request.raw_post, true)
18
19     # Assume that Node.from_xml has thrown an exception if there is an error parsing the xml
20     node.create_with_history @user
21     render :text => node.id.to_s, :content_type => "text/plain"
22   end
23
24   # Dump the details on a node given in params[:id]
25   def read
26     node = Node.find(params[:id])
27     if node.visible?
28       response.headers['Last-Modified'] = node.timestamp.rfc822
29       render :text => node.to_xml.to_s, :content_type => "text/xml"
30     else
31       render :text => "", :status => :gone
32     end
33   end
34   
35   # Update a node from given XML
36   def update
37     node = Node.find(params[:id])
38     new_node = Node.from_xml(request.raw_post)
39        
40     unless new_node and new_node.id == node.id
41       raise OSM::BadUserInput.new("The id in the url (#{node.id}) is not the same as provided in the xml (#{new_node.id})")
42     end
43     node.update_from(new_node, @user)
44     render :text => node.version.to_s, :content_type => "text/plain"
45   end
46
47   # Delete a node. Doesn't actually delete it, but retains its history 
48   # in a wiki-like way. We therefore treat it like an update, so the delete
49   # method returns the new version number.
50   def delete
51     node = Node.find(params[:id])
52     new_node = Node.from_xml(request.raw_post)
53     
54     unless new_node and new_node.id == node.id
55       raise OSM::BadUserInput.new("The id in the url (#{node.id}) is not the same as provided in the xml (#{new_node.id})")
56     end
57     node.delete_with_history!(new_node, @user)
58     render :text => node.version.to_s, :content_type => "text/plain"
59   end
60
61   # Dump the details on many nodes whose ids are given in the "nodes" parameter.
62   def nodes
63     ids = params['nodes'].split(',').collect { |n| n.to_i }
64
65     if ids.length == 0
66       raise OSM::BadUserInput.new("No nodes were given to search for")
67     end
68     doc = OSM::API.new.get_xml_doc
69
70     Node.find(ids).each do |node|
71       doc.root << node.to_xml_node
72     end
73
74     render :text => doc.to_s, :content_type => "text/xml"
75   end
76 end