]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
rails API support for history of node/segment/way + various bugfixes and cleanups
[rails.git] / app / controllers / node_controller.rb
1 class NodeController < ApplicationController
2   require 'xml/libxml'
3
4   before_filter :authorize
5
6   def create
7     response.headers["Content-Type"] = 'application/xml'
8     if request.put?
9       node = nil
10       begin
11         node = Node.from_xml(request.raw_post, true)
12       rescue
13         render :text => "XML didn't parse", :status => 400 # if we got here the doc didnt parse
14         return
15       end
16
17       if node
18         node.user_id = @user.id
19         node.visible = 1
20         if node.save_with_history
21           render :text => node.id
22         else
23           render :nothing => true, :status => 500
24         end
25         return
26
27       else
28         render :nothing => true, :status => 400 # if we got here the doc didnt parse
29         return
30       end
31     end
32
33     render :nothing => true, :status => 500 # something went very wrong
34   end
35
36   def rest
37     response.headers["Content-Type"] = 'application/xml'
38     unless Node.exists?(params[:id])
39       render :nothing => true, :status => 404
40       return
41     end
42
43     node = Node.find(params[:id])
44
45     case request.method
46
47     when :get
48       unless node
49         render :nothing => true, :status => 500
50         return
51       end
52
53       unless node.visible
54         render :nothing => true, :status => 410
55         return
56       end
57
58       render :text => node.to_xml.to_s
59       return
60
61     when :delete
62       if node.visible
63         node.visible = 0
64         node.save_with_history
65         render :nothing => true
66       else
67         render :nothing => true, :status => 410
68       end
69
70     when :put
71       new_node = Node.from_xml(request.raw_post)
72
73       node.timestamp = Time.now
74       node.user_id = @user.id
75
76       node.latitude = new_node.latitude 
77       node.longitude = new_node.longitude
78       node.tags = new_node.tags
79
80       if node.id == new_node.id and node.save_with_history
81         render :nothing => true, :status => 200
82       else
83         render :nothing => true, :status => 500
84       end
85       return
86     end
87
88   end
89
90 end