]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
some changes suggested by TomH / yet untested
[rails.git] / app / controllers / way_controller.rb
1 class WayController < 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       way = Way.from_xml(request.raw_post, true)
12
13       if way
14         if !way.preconditions_ok?
15           render :nothing => true, :status => :precondition_failed
16         else
17           way.user_id = @user.id
18
19           if way.save_with_history
20             render :text => way.id.to_s, :content_type => "text/plain"
21           else
22             render :nothing => true, :status => :internal_server_error
23           end
24         end
25       else
26         render :nothing => true, :status => :bad_request
27       end
28     else
29       render :nothing => true, :status => :method_not_allowed
30     end
31   end
32
33   def read
34     begin
35       way = Way.find(params[:id])
36
37       if way.visible
38         render :text => way.to_xml.to_s, :content_type => "text/xml"
39       else
40         render :nothing => true, :status => :gone
41       end
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     rescue
45       render :nothing => true, :status => :internal_server_error
46     end
47   end
48
49   def update
50     begin
51       way = Way.find(params[:id])
52
53       if way.visible
54         new_way = Way.from_xml(request.raw_post)
55
56         if new_way and new_way.id == way.id
57           if !new_way.preconditions_ok?
58             render :nothing => true, :status => :precondition_failed
59           else
60             way.user_id = @user.id
61             way.tags = new_way.tags
62             way.nds = new_way.nds
63             way.visible = true
64
65             if way.save_with_history
66               render :nothing => true
67             else
68               render :nothing => true, :status => :internal_server_error
69             end
70           end
71         else
72           render :nothing => true, :status => :bad_request
73         end
74       else
75         render :nothing => true, :status => :gone
76       end
77     rescue ActiveRecord::RecordNotFound
78       render :nothing => true, :status => :not_found
79     rescue
80       render :nothing => true, :status => :internal_server_error
81     end
82   end
83
84   def delete
85     begin
86       way = Way.find(params[:id])
87
88       if way.visible
89         if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='way' and member_id=?", params[:id]])
90           render :nothing => true, :status => :precondition_failed
91         else
92           way.user_id = @user.id
93           way.tags = []
94           way.nds = []
95           way.visible = false
96
97           if way.save_with_history
98             render :nothing => true
99           else
100             render :nothing => true, :status => :internal_server_error
101           end
102         end
103       else
104         render :nothing => true, :status => :gone
105       end
106     rescue ActiveRecord::RecordNotFound
107       render :nothing => true, :status => :not_found
108     rescue
109       render :nothing => true, :status => :internal_server_error
110     end
111   end
112
113   def full
114     begin
115       way = Way.find(params[:id])
116
117       if way.visible
118         nd_ids = way.nds + [-1]
119         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
120
121         # Render
122         doc = OSM::API.new.get_xml_doc
123         nodes.each do |node|
124           doc.root << node.to_xml_node()
125         end
126         doc.root << way.to_xml_node()
127
128         render :text => doc.to_s, :content_type => "text/xml"
129       else
130         render :nothing => true, :status => :gone
131       end
132     rescue ActiveRecord::RecordNotFound
133       render :nothing => true, :status => :not_found
134     rescue
135       render :nothing => true, :status => :internal_server_error
136     end
137   end
138
139   def ways
140     begin
141       ids = params['ways'].split(',').collect { |w| w.to_i }
142     rescue
143       ids = []
144     end
145
146     if ids.length > 0
147       doc = OSM::API.new.get_xml_doc
148
149       Way.find(ids).each do |way|
150         doc.root << way.to_xml_node
151       end
152
153       render :text => doc.to_s, :content_type => "text/xml"
154     else
155       render :nothing => true, :status => :bad_request
156     end
157   end
158
159   def ways_for_node
160     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
161
162     if wayids.length > 0
163       doc = OSM::API.new.get_xml_doc
164
165       Way.find(wayids).each do |way|
166         doc.root << way.to_xml_node
167       end
168
169       render :text => doc.to_s, :content_type => "text/xml"
170     else
171       render :nothing => true, :status => :bad_request
172     end
173   end
174 end