]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Potlatch for API 0.6 now saving ways happily with node versioning... I think
[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_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     begin
12       if request.put?
13         way = Way.from_xml(request.raw_post, true)
14
15         if way
16           way.create_with_history @user
17           render :text => way.id.to_s, :content_type => "text/plain"
18         else
19           render :nothing => true, :status => :bad_request
20         end
21       else
22         render :nothing => true, :status => :method_not_allowed
23       end
24     rescue OSM::APIError => ex
25       logger.warn request.raw_post
26       render ex.render_opts
27     end
28   end
29
30   def read
31     begin
32       way = Way.find(params[:id])
33
34       response.headers['Last-Modified'] = way.timestamp.rfc822
35
36       if way.visible
37         render :text => way.to_xml.to_s, :content_type => "text/xml"
38       else
39         render :text => "", :status => :gone
40       end
41     rescue OSM::APIError => ex
42       render ex.render_opts
43     rescue ActiveRecord::RecordNotFound
44       render :nothing => true, :status => :not_found
45     end
46   end
47
48   def update
49     begin
50       way = Way.find(params[:id])
51       new_way = Way.from_xml(request.raw_post)
52
53       if new_way and new_way.id == way.id
54         way.update_from(new_way, @user)
55         render :text => way.version.to_s, :content_type => "text/plain"
56       else
57         render :nothing => true, :status => :bad_request
58       end
59     rescue OSM::APIError => ex
60       logger.warn request.raw_post
61       render ex.render_opts
62     rescue ActiveRecord::RecordNotFound
63       render :nothing => true, :status => :not_found
64     end
65   end
66
67   # This is the API call to delete a way
68   def delete
69     begin
70       way = Way.find(params[:id])
71       new_way = Way.from_xml(request.raw_post)
72
73       if new_way and new_way.id == way.id
74         way.delete_with_history!(new_way, @user)
75         render :text => way.version.to_s, :content_type => "text/plain"
76       else
77         render :nothing => true, :status => :bad_request
78       end
79     rescue OSM::APIError => ex
80       render ex.render_opts
81     rescue ActiveRecord::RecordNotFound
82       render :nothing => true, :status => :not_found
83     end
84   end
85
86   def full
87     begin
88       way = Way.find(params[:id])
89
90       if way.visible
91         nd_ids = way.nds + [-1]
92         nodes = Node.find(:all, :conditions => ["visible = ? AND id IN (#{nd_ids.join(',')})", true])
93
94         # Render
95         doc = OSM::API.new.get_xml_doc
96         nodes.each do |node|
97           doc.root << node.to_xml_node()
98         end
99         doc.root << way.to_xml_node()
100
101         render :text => doc.to_s, :content_type => "text/xml"
102       else
103         render :text => "", :status => :gone
104       end
105     rescue ActiveRecord::RecordNotFound
106       render :nothing => true, :status => :not_found
107     end
108   end
109
110   def ways
111     begin
112       ids = params['ways'].split(',').collect { |w| w.to_i }
113     rescue
114       ids = []
115     end
116
117     if ids.length > 0
118       doc = OSM::API.new.get_xml_doc
119
120       Way.find(ids).each do |way|
121         doc.root << way.to_xml_node
122       end
123
124       render :text => doc.to_s, :content_type => "text/xml"
125     else
126       render :nothing => true, :status => :bad_request
127     end
128   end
129
130   ##
131   # returns all the ways which are currently using the node given in the 
132   # :id parameter. note that this used to return deleted ways as well, but
133   # this seemed not to be the expected behaviour, so it was removed.
134   def ways_for_node
135     wayids = WayNode.find(:all, 
136                           :conditions => ['node_id = ?', params[:id]]
137                           ).collect { |ws| ws.id[0] }.uniq
138
139     doc = OSM::API.new.get_xml_doc
140
141     Way.find(wayids).each do |way|
142       doc.root << way.to_xml_node if way.visible
143     end
144
145     render :text => doc.to_s, :content_type => "text/xml"
146   end
147 end