]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/relations_controller.rb
Refactoring
[rails.git] / app / controllers / api / relations_controller.rb
1 module Api
2   class RelationsController < ApiController
3     require "xml/libxml"
4
5     before_action :authorize, :only => [:create, :update, :delete]
6
7     authorize_resource
8
9     before_action :require_public_data, :only => [:create, :update, :delete]
10     before_action :check_api_writable, :only => [:create, :update, :delete]
11     before_action :check_api_readable, :except => [:create, :update, :delete]
12     around_action :api_call_handle_error, :api_call_timeout
13
14     def create
15       assert_method :put
16
17       relation = Relation.from_xml(request.raw_post, true)
18
19       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
20       relation.create_with_history current_user
21       render :plain => relation.id.to_s
22     end
23
24     def show
25       @relation = Relation.find(params[:id])
26       response.last_modified = @relation.timestamp
27       if @relation.visible
28         # Render the result
29         respond_to do |format|
30           format.xml
31         end
32       else
33         head :gone
34       end
35     end
36
37     def update
38       logger.debug request.raw_post
39
40       relation = Relation.find(params[:id])
41       new_relation = Relation.from_xml(request.raw_post)
42
43       raise OSM::APIBadUserInput, "The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})" unless new_relation && new_relation.id == relation.id
44
45       relation.update_from new_relation, current_user
46       render :plain => relation.version.to_s
47     end
48
49     def delete
50       relation = Relation.find(params[:id])
51       new_relation = Relation.from_xml(request.raw_post)
52       if new_relation && new_relation.id == relation.id
53         relation.delete_with_history!(new_relation, current_user)
54         render :plain => relation.version.to_s
55       else
56         head :bad_request
57       end
58     end
59
60     # -----------------------------------------------------------------
61     # full
62     #
63     # input parameters: id
64     #
65     # returns XML representation of one relation object plus all its
66     # members, plus all nodes part of member ways
67     # -----------------------------------------------------------------
68     def full
69       relation = Relation.find(params[:id])
70
71       if relation.visible
72
73         # first find the ids of nodes, ways and relations referenced by this
74         # relation - note that we exclude this relation just in case.
75
76         node_ids = relation.members.select { |m| m[0] == "Node" }.map { |m| m[1] }
77         way_ids = relation.members.select { |m| m[0] == "Way" }.map { |m| m[1] }
78         relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.map { |m| m[1] }
79
80         # next load the relations and the ways.
81
82         relations = Relation.where(:id => relation_ids).includes(:relation_tags)
83         ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
84
85         # now additionally collect nodes referenced by ways. Note how we
86         # recursively evaluate ways but NOT relations.
87
88         way_node_ids = ways.collect do |way|
89           way.way_nodes.collect(&:node_id)
90         end
91         node_ids += way_node_ids.flatten
92         nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
93
94         visible_nodes = {}
95
96         @nodes = []
97         nodes.each do |node|
98           next unless node.visible? # should be unnecessary if data is consistent.
99
100           @nodes << node
101           visible_nodes[node.id] = node
102         end
103
104         @ways = []
105         ways.each do |way|
106           next unless way.visible? # should be unnecessary if data is consistent.
107
108           @ways << way
109         end
110
111         @relations = []
112         relations.each do |rel|
113           next unless rel.visible? # should be unnecessary if data is consistent.
114
115           @relations << rel
116         end
117
118         # finally add self
119         @relations << relation
120
121         # Render the result
122         respond_to do |format|
123           format.xml
124         end
125       else
126         head :gone
127       end
128     end
129
130     def index
131       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
132
133       ids = params["relations"].split(",").collect(&:to_i)
134
135       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
136
137       @relations = Relation.find(ids)
138
139       # Render the result
140       respond_to do |format|
141         format.xml
142       end
143     end
144
145     def relations_for_way
146       relations_for_object("Way")
147     end
148
149     def relations_for_node
150       relations_for_object("Node")
151     end
152
153     def relations_for_relation
154       relations_for_object("Relation")
155     end
156
157     private
158
159     def relations_for_object(objtype)
160       relationids = RelationMember.where(:member_type => objtype, :member_id => params[:id]).collect(&:relation_id).uniq
161
162       @relations = []
163
164       Relation.find(relationids).each do |relation|
165         @relations << relation if relation.visible
166       end
167
168       # Render the result
169       respond_to do |format|
170         format.xml
171       end
172     end
173   end
174 end