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