]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
Include a reply link in email notifications of messages.
[rails.git] / app / controllers / node_controller.rb
1 class NodeController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :check_availability, :only => [:create, :update, :delete]
7   after_filter :compress_output
8
9   def create
10     if request.put?
11       node = Node.from_xml(request.raw_post, true)
12
13       if node
14         node.user_id = @user.id
15         node.visible = true
16         node.save_with_history!
17
18         render :text => node.id.to_s, :content_type => "text/plain"
19       else
20         render :nothing => true, :status => :bad_request
21       end
22     else
23       render :nothing => true, :status => :method_not_allowed
24     end
25   end
26
27   def read
28     begin
29       node = Node.find(params[:id])
30
31       if node.visible
32         render :text => node.to_xml.to_s, :content_type => "text/xml"
33        else
34         render :nothing => true, :status => :gone
35       end
36     rescue ActiveRecord::RecordNotFound
37       render :nothing => true, :status => :not_found
38     end
39   end
40
41   def update
42     begin
43       node = Node.find(params[:id])
44
45       if node.visible
46         new_node = Node.from_xml(request.raw_post)
47
48         if new_node and new_node.id == node.id
49           node.user_id = @user.id
50           node.latitude = new_node.latitude 
51           node.longitude = new_node.longitude
52           node.tags = new_node.tags
53           node.save_with_history!
54
55           render :nothing => true
56         else
57           render :nothing => true, :status => :bad_request
58         end
59       else
60         render :nothing => true, :status => :gone
61       end
62     rescue ActiveRecord::RecordNotFound
63       render :nothing => true, :status => :not_found
64     end
65   end
66
67   def delete
68     begin
69       node = Node.find(params[:id])
70
71       if node.visible
72         if Segment.find(:first, :conditions => [ "visible = 1 and (node_a = ? or node_b = ?)", node.id, node.id])
73           render :nothing => true, :status => :precondition_failed
74         else
75           node.user_id = @user.id
76           node.visible = 0
77           node.save_with_history!
78
79           render :nothing => true
80         end
81       else
82         render :nothing => true, :status => :gone
83       end
84     rescue ActiveRecord::RecordNotFound
85       render :nothing => true, :status => :not_found
86     end
87   end
88
89   def nodes
90     ids = params['nodes'].split(',').collect { |n| n.to_i }
91
92     if ids.length > 0
93       doc = OSM::API.new.get_xml_doc
94
95       Node.find(ids).each do |node|
96         doc.root << node.to_xml_node
97       end 
98
99       render :text => doc.to_s, :content_type => "text/xml"
100     else
101       render :nothing => true, :status => :bad_request
102     end
103   end
104 end