]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
03ae64f41939a4c9f4e4dcd92895a3ab33044b40
[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     if request.put?
15       node = Node.from_xml(request.raw_post, true)
16       # FIXME remove debug
17       logger.debug request.raw_post
18       logger.debug node
19
20       if node
21         node.version = 0
22         #node.changeset_id = node.changeset
23         node.visible = true
24         node.save_with_history!
25
26         render :text => node.id.to_s, :content_type => "text/plain"
27       else
28         render :nothing => true, :status => :bad_request
29       end
30     else
31       render :nothing => true, :status => :method_not_allowed
32     end
33   end
34
35   # Dump the details on a node given in params[:id]
36   def read
37     begin
38       node = Node.find(params[:id])
39       if node.visible
40         response.headers['Last-Modified'] = node.timestamp.rfc822
41         render :text => node.to_xml.to_s, :content_type => "text/xml"
42        else
43         render :text => "", :status => :gone
44       end
45     rescue ActiveRecord::RecordNotFound
46       render :nothing => true, :status => :not_found
47     end
48   end
49   
50   # Dump a specific version of the node based on the given params[:id] and params[:version]
51   def version
52     begin
53       node = Node.find(:first, :conditions => { :id => params[:id], :version => params[:version] } )
54       if node.visible
55         response.headers['Last-Modified'] = node.timestamp.rfc822
56         render :text => node.to_xml.to_s, :content_type => "text/xml"
57       else
58         render :nothing => true, :status => :gone
59       end
60     rescue ActiveRecord::RecordNotFound
61       render :nothing => true, :status => :not_found
62     end
63   end
64
65   # Update a node from given XML
66   def update
67     begin
68       node = Node.find(params[:id])
69       new_node = Node.from_xml(request.raw_post)
70
71       if new_node and new_node.id == node.id
72         node.update_from(new_node, @user)
73         render :text => node.version.to_s, :content_type => "text/plain"
74       else
75         render :nothing => true, :status => :bad_request
76       end
77     rescue OSM::APIVersionMismatchError => ex
78       render :text => "Version mismatch: Provided " + ex.provided.to_s +
79         ", server had: " + ex.latest.to_s, :status => :bad_request
80     rescue ActiveRecord::RecordNotFound
81       render :nothing => true, :status => :not_found
82     end
83   end
84
85   # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
86   # FIXME remove all the fricking SQL
87   def delete
88     begin
89       node = Node.find(params[:id])
90       node.delete_with_history(@user)
91
92       render :nothing => true
93     rescue ActiveRecord::RecordNotFound
94       render :nothing => true, :status => :not_found
95     rescue OSM::APIError => ex
96       render ex.render_opts
97     end
98   end
99
100   # WTF does this do?
101   def nodes
102     ids = params['nodes'].split(',').collect { |n| n.to_i }
103
104     if ids.length > 0
105       doc = OSM::API.new.get_xml_doc
106
107       Node.find(ids).each do |node|
108         doc.root << node.to_xml_node
109       end 
110
111       render :text => doc.to_s, :content_type => "text/xml"
112     else
113       render :nothing => true, :status => :bad_request
114     end
115   end
116 end