]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
api06: Merge changesets code from Frederik.
[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
17       if node
18         node.version = 0
19         node.user_id = @user.id
20         node.visible = true
21         node.save_with_history!
22
23         render :text => node.id.to_s, :content_type => "text/plain"
24       else
25         render :nothing => true, :status => :bad_request
26       end
27     else
28       render :nothing => true, :status => :method_not_allowed
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.user_id = @user.id
55         node.latitude = new_node.latitude 
56         node.longitude = new_node.longitude
57         node.tags = new_node.tags
58         node.visible = true
59         node.save_with_history!
60
61         render :nothing => true
62       else
63         render :nothing => true, :status => :bad_request
64       end
65     rescue ActiveRecord::RecordNotFound
66       render :nothing => true, :status => :not_found
67     end
68   end
69
70   # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
71   # FIXME remove all the fricking SQL
72   def delete
73     begin
74       node = Node.find(params[:id])
75
76       if node.visible
77         if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = 1 AND current_way_nodes.node_id = ?", node.id ])
78           render :text => "", :status => :precondition_failed
79         elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='node' and member_id=?", params[:id]])
80           render :text => "", :status => :precondition_failed
81         else
82           node.user_id = @user.id
83           node.visible = 0
84           node.save_with_history!
85
86           render :nothing => true
87         end
88       else
89         render :text => "", :status => :gone
90       end
91     rescue ActiveRecord::RecordNotFound
92       render :nothing => true, :status => :not_found
93     end
94   end
95
96   # WTF does this do?
97   def nodes
98     ids = params['nodes'].split(',').collect { |n| n.to_i }
99
100     if ids.length > 0
101       doc = OSM::API.new.get_xml_doc
102
103       Node.find(ids).each do |node|
104         doc.root << node.to_xml_node
105       end 
106
107       render :text => doc.to_s, :content_type => "text/xml"
108     else
109       render :nothing => true, :status => :bad_request
110     end
111   end
112 end