]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Potlatch on Rails, first working version
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController\r
2   require 'xml/libxml'
3
4   before_filter :authorize
5   after_filter :compress_output
6   \r
7   def create
8     if request.put?
9       way = Way.from_xml(request.raw_post, true)
10
11       if way
12         way.user_id = @user.id
13         unless way.preconditions_ok? # are the segments (and their nodes) visible?
14           render :nothing => true, :status => 412
15           return
16         end
17
18         if way.save_with_history
19           render :text => way.id.to_s
20           return
21         else
22           render :nothing => true, :status => 500
23           return
24         end
25         return
26       else
27         render :nothing => true, :status => 400 # if we got here the doc didnt parse
28         return
29       end
30     end
31
32     render :nothing => true, :status => 500 # something went very wrong
33   end
34
35   def full\r
36     unless Way.exists?(params[:id])
37       render :nothing => true, :status => 404
38       return
39     end
40
41     way = Way.find(params[:id])
42
43     unless way.visible
44       render :nothing => true, :status => 410
45       return
46     end
47
48     # In future, we might want to do all the data fetch in one step
49     seg_ids = way.segs + [-1]
50     segments = Segment.find_by_sql "select * from current_segments where visible = 1 and id IN (#{seg_ids.join(',')})"
51
52     node_ids = segments.collect {|segment| segment.node_a }
53     node_ids += segments.collect {|segment| segment.node_b }
54     node_ids += [-1]
55     nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{node_ids.join(',')})")
56
57     # Render
58     doc = OSM::API.new.get_xml_doc
59     nodes.each do |node|
60       doc.root << node.to_xml_node()
61     end
62     segments.each do |segment|
63       doc.root << segment.to_xml_node()
64     end
65     doc.root << way.to_xml_node()
66
67     render :text => doc.to_s
68   end
69
70   def rest
71     unless Way.exists?(params[:id])
72       render :nothing => true, :status => 404
73       return
74     end
75
76     way = Way.find(params[:id])
77     case request.method
78
79     when :get
80       unless way.visible
81         render :nothing => true, :status => 410
82         return
83       end
84       render :text => way.to_xml.to_s
85
86     when :delete
87       unless way.visible
88         render :nothing => true, :status => 410
89         return
90       end
91
92       way.visible = false
93       way.save_with_history
94       render :nothing => true
95       return
96     when :put
97       way = Way.from_xml(request.raw_post)
98
99       if way
100         way_in_db = Way.find(way.id)
101         if way_in_db
102           way_in_db.user_id = @user.id
103           way_in_db.tags = way.tags
104           way_in_db.segs = way.segs
105           way_in_db.timestamp = way.timestamp
106           way_in_db.visible = true
107           if way_in_db.save_with_history
108             render :text => way.id
109           else
110             render :nothing => true, :status => 500
111           end
112           return
113         else
114           render :nothing => true, :status => 404 # way doesn't exist yet
115         end
116       else
117         render :nothing => true, :status => 400 # if we got here the doc didnt parse
118         return
119       end
120     end
121   end
122
123   def ways
124     response.headers["Content-Type"] = 'text/xml'
125     ids = params['ways'].split(',').collect {|w| w.to_i }
126     if ids.length > 0
127       waylist = Way.find(ids)
128       doc = OSM::API.new.get_xml_doc
129       waylist.each do |way|
130         doc.root << way.to_xml_node
131       end 
132       render :text => doc.to_s
133     else
134       render :nothing => true, :status => 400
135     end
136   end
137
138 end