]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
moving the check consistency to it's own file so that checks will be able to be loade...
[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         # FIXME move some of this to the model. The controller shouldn't need to
16         # know about the fact that the first version number is 0 on creation
17         # it will also allow use to run a variation on the check_consistency
18         # so that we don't get exceptions thrown when the changesets are not right
19         unless way.preconditions_ok?
20           render :text => "", :status => :precondition_failed
21         else
22           way.version = 0
23           way.save_with_history!
24
25           render :text => way.id.to_s, :content_type => "text/plain"
26         end
27       else
28         render :nothing => true, :status => :bad_request
29       end
30     else
31       render :nothing => true, :status => :method_not_allowed
32     end
33   end
34
35   def read
36     begin
37       way = Way.find(params[:id])
38
39       response.headers['Last-Modified'] = way.timestamp.rfc822
40
41       if way.visible
42         render :text => way.to_xml.to_s, :content_type => "text/xml"
43       else
44         render :text => "", :status => :gone
45       end
46     rescue OSM::APIError => ex
47       render ex.render_opts
48     rescue ActiveRecord::RecordNotFound
49       render :nothing => true, :status => :not_found
50     end
51   end
52
53   def update
54     begin
55       way = Way.find(params[:id])
56       new_way = Way.from_xml(request.raw_post)
57
58       if new_way and new_way.id == way.id
59         way.update_from(new_way, @user)
60         render :text => way.version.to_s, :content_type => "text/plain"
61       else
62         render :nothing => true, :status => :bad_request
63       end
64     rescue OSM::APIError => ex
65       render ex.render_opts
66     rescue ActiveRecord::RecordNotFound
67       render :nothing => true, :status => :not_found
68     end
69   end
70
71   # This is the API call to delete a way
72   def delete
73     begin
74       way = Way.find(params[:id])
75       new_way = Way.from_xml(request.raw_post)
76       if new_way and new_way.id == way.id
77         way.delete_with_history(@user)
78
79         # if we get here, all is fine, otherwise something will catch below.  
80         render :nothing => true
81       else
82         render :nothing => true, :status => :bad_request
83       end
84     rescue OSM::APIError => ex
85       render ex.render_opts
86     rescue ActiveRecord::RecordNotFound
87       render :nothing => true, :status => :not_found
88     end
89   end
90
91   def full
92     begin
93       way = Way.find(params[:id])
94
95       if way.visible
96         nd_ids = way.nds + [-1]
97         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
98
99         # Render
100         doc = OSM::API.new.get_xml_doc
101         nodes.each do |node|
102           doc.root << node.to_xml_node()
103         end
104         doc.root << way.to_xml_node()
105
106         render :text => doc.to_s, :content_type => "text/xml"
107       else
108         render :text => "", :status => :gone
109       end
110     rescue ActiveRecord::RecordNotFound
111       render :nothing => true, :status => :not_found
112     end
113   end
114
115   def ways
116     begin
117       ids = params['ways'].split(',').collect { |w| w.to_i }
118     rescue
119       ids = []
120     end
121
122     if ids.length > 0
123       doc = OSM::API.new.get_xml_doc
124
125       Way.find(ids).each do |way|
126         doc.root << way.to_xml_node
127       end
128
129       render :text => doc.to_s, :content_type => "text/xml"
130     else
131       render :nothing => true, :status => :bad_request
132     end
133   end
134
135   def ways_for_node
136     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
137
138     doc = OSM::API.new.get_xml_doc
139
140     Way.find(wayids).each do |way|
141       doc.root << way.to_xml_node
142     end
143
144     render :text => doc.to_s, :content_type => "text/xml"
145   end
146 end