]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Split the rest action into sparate read, update and delete actions thus
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   before_filter :authorize, :only => [:create, :update, :destroy]
5   after_filter :compress_output
6
7   def create
8     if request.put?
9       way = Way.from_xml(request.raw_post, true)
10
11       if way
12         if !way.preconditions_ok?
13           render :nothing => true, :status => :precondition_failed
14         else
15           way.user_id = @user.id
16
17           if way.save_with_history
18             render :text => way.id.to_s, :content_type => "text/plain"
19           else
20             render :nothing => true, :status => :internal_server_error
21           end
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
33       way = Way.find(params[:id])
34
35       if way.visible
36         render :text => way.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :nothing => true, :status => :gone
39       end
40     rescue ActiveRecord::RecordNotFound
41       render :nothing => true, :status => :not_found
42     rescue
43       render :nothing => true, :status => :internal_server_error
44     end
45   end
46
47   def update
48     begin
49       way = Way.find(params[:id])
50
51       if way.visible
52         new_way = Way.from_xml(request.raw_post)
53
54         if new_way and new_way.id == way.id
55           if !new_way.preconditions_ok?
56             render :nothing => true, :status => :precondition_failed
57           else
58             way.user_id = @user.id
59             way.tags = new_way.tags
60             way.segs = new_way.segs
61             way.timestamp = new_way.timestamp
62             way.visible = true
63
64             if way.save_with_history
65               render :nothing => true
66             else
67               render :nothing => true, :status => :internal_server_error
68             end
69           end
70         else
71           render :nothing => true, :status => :bad_request
72         end
73       else
74         render :nothing => true, :status => :gone
75       end
76     rescue ActiveRecord::RecordNotFound
77       render :nothing => true, :status => :not_found
78     rescue
79       render :nothing => true, :status => :internal_server_error
80     end
81   end
82
83   def delete
84     begin
85       way = Way.find(params[:id])
86
87       if way.visible
88         way.user_id = @user.id
89         way.visible = false
90
91         if way.save_with_history
92           render :nothing => true
93         else
94           render :nothing => true, :status => :internal_server_error
95         end
96       else
97         render :nothing => true, :status => :gone
98       end
99     rescue ActiveRecord::RecordNotFound
100       render :nothing => true, :status => :not_found
101     rescue
102       render :nothing => true, :status => :internal_server_error
103     end
104   end
105
106   def full
107     begin
108       way = Way.find(params[:id])
109
110       if way.visible
111         # In future, we might want to do all the data fetch in one step
112         seg_ids = way.segs + [-1]
113         segments = Segment.find_by_sql "select * from current_segments where visible = 1 and id IN (#{seg_ids.join(',')})"
114
115         node_ids = segments.collect {|segment| segment.node_a }
116         node_ids += segments.collect {|segment| segment.node_b }
117         node_ids += [-1]
118         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{node_ids.join(',')})")
119
120         # Render
121         doc = OSM::API.new.get_xml_doc
122         nodes.each do |node|
123           doc.root << node.to_xml_node()
124         end
125         segments.each do |segment|
126           doc.root << segment.to_xml_node()
127         end
128         doc.root << way.to_xml_node()
129
130         render :text => doc.to_s, :content_type => "text/xml"
131       else
132         render :nothing => true, :status => :gone
133       end
134     rescue ActiveRecord::RecordNotFound
135       render :nothing => true, :status => :not_found
136     rescue
137       render :nothing => true, :status => :internal_server_error
138     end
139   end
140
141   def ways
142     ids = params['ways'].split(',').collect { |w| w.to_i }
143
144     if ids.length > 0
145       doc = OSM::API.new.get_xml_doc
146
147       Way.find(ids).each do |way|
148         doc.root << way.to_xml_node
149       end
150
151       render :text => doc.to_s, :content_type => "text/xml"
152     else
153       render :nothing => true, :status => :bad_request
154     end
155   end
156
157   def ways_for_segment
158     wayids = WaySegment.find(:all, :conditions => ['segment_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
159
160     if wayids.length > 0
161       doc = OSM::API.new.get_xml_doc
162
163       Way.find(wayids).each do |way|
164         doc.root << way.to_xml_node
165       end
166
167       render :text => doc.to_s, :content_type => "text/xml"
168     else
169       render :nothing => true, :status => :bad_request
170     end
171   end
172 end