]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Merge remote-tracking branch 'upstream/pull/5586'
[rails.git] / app / controllers / api / ways_controller.rb
1 module Api
2   class WaysController < ApiController
3     before_action :check_api_writable, :only => [:create, :update, :delete]
4     before_action :authorize, :only => [:create, :update, :delete]
5
6     authorize_resource
7
8     before_action :require_public_data, :only => [:create, :update, :delete]
9     before_action :set_request_formats, :except => [:create, :update, :delete]
10     before_action :check_rate_limit, :only => [:create, :update, :delete]
11
12     def index
13       raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]" unless params["ways"]
14
15       ids = params["ways"].split(",").collect(&:to_i)
16
17       raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
18
19       @ways = Way.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       @way = Way.find(params[:id])
30
31       response.last_modified = @way.timestamp
32
33       if @way.visible
34         # Render the result
35         respond_to do |format|
36           format.xml
37           format.json
38         end
39       else
40         head :gone
41       end
42     end
43
44     def create
45       way = Way.from_xml(request.raw_post, :create => true)
46
47       # Assume that Way.from_xml has thrown an exception if there is an error parsing the xml
48       way.create_with_history current_user
49       render :plain => way.id.to_s
50     end
51
52     def update
53       way = Way.find(params[:id])
54       new_way = Way.from_xml(request.raw_post)
55
56       raise OSM::APIBadUserInput, "The id in the url (#{way.id}) is not the same as provided in the xml (#{new_way.id})" unless new_way && new_way.id == way.id
57
58       way.update_from(new_way, current_user)
59       render :plain => way.version.to_s
60     end
61
62     # This is the API call to delete a way
63     def delete
64       way = Way.find(params[:id])
65       new_way = Way.from_xml(request.raw_post)
66
67       if new_way && new_way.id == way.id
68         way.delete_with_history!(new_way, current_user)
69         render :plain => way.version.to_s
70       else
71         head :bad_request
72       end
73     end
74
75     def full
76       @way = Way.includes(:nodes => :node_tags).find(params[:id])
77
78       if @way.visible
79         @nodes = []
80
81         @way.nodes.uniq.each do |node|
82           @nodes << node if node.visible
83         end
84
85         # Render the result
86         respond_to do |format|
87           format.xml
88           format.json
89         end
90       else
91         head :gone
92       end
93     end
94
95     ##
96     # returns all the ways which are currently using the node given in the
97     # :id parameter. note that this used to return deleted ways as well, but
98     # this seemed not to be the expected behaviour, so it was removed.
99     def ways_for_node
100       wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
101
102       @ways = Way.where(:id => wayids, :visible => true)
103
104       # Render the result
105       respond_to do |format|
106         format.xml
107         format.json
108       end
109     end
110   end
111 end