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