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