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