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