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