]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Map 'full' to api way show action
[rails.git] / app / controllers / api / ways_controller.rb
1 module Api
2   class WaysController < 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 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
30       @way = @way.includes(:nodes => :node_tags) if params[:full]
31       @way = @way.find(params[:id])
32
33       response.last_modified = @way.timestamp unless params[:full]
34
35       if @way.visible
36         if params[:full]
37           @nodes = []
38
39           @way.nodes.uniq.each do |node|
40             @nodes << node if node.visible
41           end
42         end
43
44         respond_to do |format|
45           format.xml
46           format.json
47         end
48       else
49         head :gone
50       end
51     end
52
53     def create
54       way = Way.from_xml(request.raw_post, :create => true)
55
56       # Assume that Way.from_xml has thrown an exception if there is an error parsing the xml
57       way.create_with_history current_user
58       render :plain => way.id.to_s
59     end
60
61     def update
62       way = Way.find(params[:id])
63       new_way = Way.from_xml(request.raw_post)
64
65       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
66
67       way.update_from(new_way, current_user)
68       render :plain => way.version.to_s
69     end
70
71     # This is the API call to delete a way
72     def destroy
73       way = Way.find(params[:id])
74       new_way = Way.from_xml(request.raw_post)
75
76       if new_way && new_way.id == way.id
77         way.delete_with_history!(new_way, current_user)
78         render :plain => way.version.to_s
79       else
80         head :bad_request
81       end
82     end
83
84     ##
85     # returns all the ways which are currently using the node given in the
86     # :id parameter. note that this used to return deleted ways as well, but
87     # this seemed not to be the expected behaviour, so it was removed.
88     def ways_for_node
89       wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
90
91       @ways = Way.where(:id => wayids, :visible => true)
92
93       # Render the result
94       respond_to do |format|
95         format.xml
96         format.json
97       end
98     end
99   end
100 end