]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
some more progress towards making amf_controller do version checking of nodes before...
[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   # Update a node from given XML
48   def update
49     begin
50       node = Node.find(params[:id])
51       new_node = Node.from_xml(request.raw_post)
52
53       if new_node and new_node.id == node.id
54         node.update_from(new_node, @user)
55         render :text => node.version.to_s, :content_type => "text/plain"
56       else
57         render :nothing => true, :status => :bad_request
58       end
59     rescue OSM::APIError => ex
60       render ex.render_opts
61     rescue ActiveRecord::RecordNotFound
62       render :nothing => true, :status => :not_found
63     end
64   end
65
66   # Delete a node. Doesn't actually delete it, but retains its history 
67   # in a wiki-like way. We therefore treat it like an update, so the delete
68   # method returns the new version number.
69   def delete
70     begin
71       node = Node.find(params[:id])
72       new_node = Node.from_xml(request.raw_post)
73       
74       if new_node and new_node.id == node.id
75         node.delete_with_history!(new_node, @user)
76         render :text => node.version.to_s, :content_type => "text/plain"
77       else
78         render :nothing => true, :status => :bad_request
79       end
80     rescue ActiveRecord::RecordNotFound
81       render :nothing => true, :status => :not_found
82     rescue OSM::APIError => ex
83       render ex.render_opts
84     end
85   end
86
87   # Dump the details on many nodes whose ids are given in the "nodes" parameter.
88   def nodes
89     ids = params['nodes'].split(',').collect { |n| n.to_i }
90
91     if ids.length > 0
92       doc = OSM::API.new.get_xml_doc
93
94       Node.find(ids).each do |node|
95         doc.root << node.to_xml_node
96       end 
97
98       render :text => doc.to_s, :content_type => "text/xml"
99     else
100       render :nothing => true, :status => :bad_request
101     end
102   end
103 end