]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Redirect to inbox after marking a message as read/unread disregarding referer
[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 :check_api_readable, :except => [:create, :update, :delete]
5     before_action :authorize, :only => [:create, :update, :delete]
6
7     authorize_resource
8
9     before_action :require_public_data, :only => [:create, :update, :delete]
10     around_action :api_call_handle_error, :api_call_timeout
11
12     before_action :set_request_formats, :except => [:create, :update, :delete]
13     before_action :check_rate_limit, :only => [:create, :update, :delete]
14
15     def index
16       raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]" unless params["ways"]
17
18       ids = params["ways"].split(",").collect(&:to_i)
19
20       raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
21
22       @ways = Way.find(ids)
23
24       # Render the result
25       respond_to do |format|
26         format.xml
27         format.json
28       end
29     end
30
31     def show
32       @way = Way.find(params[:id])
33
34       response.last_modified = @way.timestamp
35
36       if @way.visible
37         # Render the result
38         respond_to do |format|
39           format.xml
40           format.json
41         end
42       else
43         head :gone
44       end
45     end
46
47     def create
48       way = Way.from_xml(request.raw_post, :create => true)
49
50       # Assume that Way.from_xml has thrown an exception if there is an error parsing the xml
51       way.create_with_history current_user
52       render :plain => way.id.to_s
53     end
54
55     def update
56       way = Way.find(params[:id])
57       new_way = Way.from_xml(request.raw_post)
58
59       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
60
61       way.update_from(new_way, current_user)
62       render :plain => way.version.to_s
63     end
64
65     # This is the API call to delete a way
66     def delete
67       way = Way.find(params[:id])
68       new_way = Way.from_xml(request.raw_post)
69
70       if new_way && new_way.id == way.id
71         way.delete_with_history!(new_way, current_user)
72         render :plain => way.version.to_s
73       else
74         head :bad_request
75       end
76     end
77
78     def full
79       @way = Way.includes(:nodes => :node_tags).find(params[:id])
80
81       if @way.visible
82         visible_nodes = {}
83
84         @nodes = []
85
86         @way.nodes.uniq.each do |node|
87           if node.visible
88             @nodes << node
89             visible_nodes[node.id] = node
90           end
91         end
92
93         # Render the result
94         respond_to do |format|
95           format.xml
96           format.json
97         end
98       else
99         head :gone
100       end
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       @ways = Way.where(:id => wayids, :visible => true)
111
112       # Render the result
113       respond_to do |format|
114         format.xml
115         format.json
116       end
117     end
118   end
119 end