]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Merge pull request #5932 from tomhughes/frozen-strings
[rails.git] / app / controllers / api / ways_controller.rb
1 # frozen_string_literal: true
2
3 module Api
4   class WaysController < ApiController
5     before_action :check_api_writable, :only => [:create, :update, :destroy]
6     before_action :authorize, :only => [:create, :update, :destroy]
7
8     authorize_resource
9
10     before_action :require_public_data, :only => [:create, :update, :destroy]
11     before_action :set_request_formats, :except => [:create, :update, :destroy]
12     before_action :check_rate_limit, :only => [:create, :update, :destroy]
13
14     def index
15       raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]" unless params["ways"]
16
17       ids = params["ways"].split(",").collect(&:to_i)
18
19       raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
20
21       @ways = Way.includes(:nodes, :element_tags).find(ids)
22
23       # Render the result
24       respond_to do |format|
25         format.xml
26         format.json
27       end
28     end
29
30     def show
31       @way = Way
32       @way = @way.includes(:nodes, :element_tags)
33       @way = @way.includes(:nodes => :element_tags) if params[:full]
34       @way = @way.find(params[:id])
35
36       response.last_modified = @way.timestamp unless params[:full]
37
38       if @way.visible
39         if params[:full]
40           @nodes = []
41
42           @way.nodes.uniq.each do |node|
43             @nodes << node if node.visible
44           end
45         end
46
47         respond_to do |format|
48           format.xml
49           format.json
50         end
51       else
52         head :gone
53       end
54     end
55
56     def create
57       way = Way.from_xml(request.raw_post, :create => true)
58
59       Changeset.transaction do
60         way.changeset&.lock!
61         way.create_with_history current_user
62       end
63       render :plain => way.id.to_s
64     end
65
66     def update
67       way = Way.find(params[:id])
68       new_way = Way.from_xml(request.raw_post)
69
70       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
71
72       Changeset.transaction do
73         new_way.changeset&.lock!
74         way.update_from(new_way, current_user)
75       end
76       render :plain => way.version.to_s
77     end
78
79     # This is the API call to delete a way
80     def destroy
81       way = Way.find(params[:id])
82       new_way = Way.from_xml(request.raw_post)
83
84       if new_way && new_way.id == way.id
85         Changeset.transaction do
86           new_way.changeset&.lock!
87           way.delete_with_history!(new_way, current_user)
88         end
89         render :plain => way.version.to_s
90       else
91         head :bad_request
92       end
93     end
94   end
95 end