]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
c3640609a695ec755c762182679c6a12149c14db
[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     if request.put?
12       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         if !way.preconditions_ok?
16           render :text => "", :status => :precondition_failed
17         else
18           way.user_id = @user.id
19           way.save_with_history!
20
21           render :text => way.id.to_s, :content_type => "text/plain"
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       response.headers['Last-Modified'] = way.timestamp.rfc822
36
37       if way.visible
38         render :text => way.to_xml.to_s, :content_type => "text/xml"
39       else
40         render :text => "", :status => :gone
41       end
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     end
45   end
46
47   def update
48     begin
49       way = Way.find(params[:id])
50       new_way = Way.from_xml(request.raw_post)
51
52       if new_way and new_way.id == way.id
53         if !new_way.preconditions_ok?
54           render :text => "", :status => :precondition_failed
55         else
56           way.user_id = @user.id
57           way.tags = new_way.tags
58           way.nds = new_way.nds
59           way.visible = true
60           way.save_with_history!
61
62           render :nothing => true
63         end
64       else
65         render :nothing => true, :status => :bad_request
66       end
67     rescue ActiveRecord::RecordNotFound
68       render :nothing => true, :status => :not_found
69     end
70   end
71
72   # This is the API call to delete a way
73   def delete
74     begin
75       way = Way.find(params[:id])
76       way.delete_with_relations_and_history(@user)
77
78       # if we get here, all is fine, otherwise something will catch below.  
79       render :nothing => true
80       return
81     rescue OSM::APIAlreadyDeletedError
82       render :text => "", :status => :gone
83     rescue OSM::APIPreconditionFailedError
84       render :text => "", :status => :precondition_failed
85     rescue ActiveRecord::RecordNotFound
86       render :nothing => true, :status => :not_found
87     end
88   end
89
90   def full
91     begin
92       way = Way.find(params[:id])
93
94       if way.visible
95         nd_ids = way.nds + [-1]
96         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
97
98         # Render
99         doc = OSM::API.new.get_xml_doc
100         nodes.each do |node|
101           doc.root << node.to_xml_node()
102         end
103         doc.root << way.to_xml_node()
104
105         render :text => doc.to_s, :content_type => "text/xml"
106       else
107         render :text => "", :status => :gone
108       end
109     rescue ActiveRecord::RecordNotFound
110       render :nothing => true, :status => :not_found
111     end
112   end
113
114   def ways
115     begin
116       ids = params['ways'].split(',').collect { |w| w.to_i }
117     rescue
118       ids = []
119     end
120
121     if ids.length > 0
122       doc = OSM::API.new.get_xml_doc
123
124       Way.find(ids).each do |way|
125         doc.root << way.to_xml_node
126       end
127
128       render :text => doc.to_s, :content_type => "text/xml"
129     else
130       render :nothing => true, :status => :bad_request
131     end
132   end
133
134   def ways_for_node
135     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
136
137     doc = OSM::API.new.get_xml_doc
138
139     Way.find(wayids).each do |way|
140       doc.root << way.to_xml_node
141     end
142
143     render :text => doc.to_s, :content_type => "text/xml"
144   end
145 end