]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
now you will actually save the changeset on updating ways. Showing the changeset...
[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   session :off
7   before_filter :authorize, :only => [:create, :update, :delete]
8   before_filter :check_write_availability, :only => [:create, :update, :delete]
9   before_filter :check_read_availability, :except => [:create, :update, :delete]
10   after_filter :compress_output
11
12   # Create a node from XML.
13   def create
14     begin
15       if request.put?
16         node = Node.from_xml(request.raw_post, true)
17
18         if node
19           node.create_with_history @user
20           render :text => node.id.to_s, :content_type => "text/plain"
21         else
22           render :nothing => true, :status => :bad_request
23         end
24       else
25         render :nothing => true, :status => :method_not_allowed
26       end
27     rescue OSM::APIError => ex
28       render ex.render_opts
29     end
30   end
31
32   # Dump the details on a node given in params[:id]
33   def read
34     begin
35       node = Node.find(params[:id])
36       if node.visible?
37         response.headers['Last-Modified'] = node.timestamp.rfc822
38         render :text => node.to_xml.to_s, :content_type => "text/xml"
39        else
40         render :text => "", :status => :gone
41       end
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     end
45   end
46   
47   # Dump a specific version of the node based on the given params[:id] and params[:version]
48   def version
49     begin
50       node = Node.find(:first, :conditions => { :id => params[:id], :version => params[:version] } )
51       if node.visible
52         response.headers['Last-Modified'] = node.timestamp.rfc822
53         render :text => node.to_xml.to_s, :content_type => "text/xml"
54       else
55         render :nothing => true, :status => :gone
56       end
57     rescue ActiveRecord::RecordNotFound
58       render :nothing => true, :status => :not_found
59     end
60   end
61
62   # Update a node from given XML
63   def update
64     begin
65       node = Node.find(params[:id])
66       new_node = Node.from_xml(request.raw_post)
67
68       if new_node and new_node.id == node.id
69         node.update_from(new_node, @user)
70         render :text => node.version.to_s, :content_type => "text/plain"
71       else
72         render :nothing => true, :status => :bad_request
73       end
74     rescue OSM::APIVersionMismatchError => ex
75       render :text => "Version mismatch: Provided " + ex.provided.to_s +
76         ", server had: " + ex.latest.to_s, :status => :bad_request
77     rescue ActiveRecord::RecordNotFound
78       render :nothing => true, :status => :not_found
79     end
80   end
81
82   # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
83   # FIXME remove all the fricking SQL
84   def delete
85     begin
86       node = Node.find(params[:id])
87       new_node = Node.from_xml(request.raw_post)
88       # FIXME we no longer care about the user, (or maybe we want to check
89       # that the user of the changeset is the same user as is making this
90       # little change?) we really care about the 
91       # changeset which must be open, and that the version that we have been
92       # given is the one that is currently stored in the database
93       
94       if new_node and new_node.id == node.id
95         node.delete_with_history(new_node, @user)
96         render :nothing => true
97       else
98         render :nothing => true, :status => :bad_request
99       end
100     rescue ActiveRecord::RecordNotFound
101       render :nothing => true, :status => :not_found
102     rescue OSM::APIError => ex
103       render ex.render_opts
104     end
105   end
106
107   # WTF does this do?
108   def nodes
109     ids = params['nodes'].split(',').collect { |n| n.to_i }
110
111     if ids.length > 0
112       doc = OSM::API.new.get_xml_doc
113
114       Node.find(ids).each do |node|
115         doc.root << node.to_xml_node
116       end 
117
118       render :text => doc.to_s, :content_type => "text/xml"
119     else
120       render :nothing => true, :status => :bad_request
121     end
122   end
123 end