more way bits
authorSteve Coast <steve@asklater.com>
Sat, 26 Aug 2006 21:36:21 +0000 (21:36 +0000)
committerSteve Coast <steve@asklater.com>
Sat, 26 Aug 2006 21:36:21 +0000 (21:36 +0000)
app/controllers/node_controller.rb
app/controllers/segment_controller.rb
app/controllers/way_controller.rb
app/controllers/way_segment_controller.rb [new file with mode: 0644]
app/helpers/way_segment_helper.rb [new file with mode: 0644]
app/models/way.rb
app/models/way_segment.rb [new file with mode: 0644]
config/routes.rb
db/migrate/011_create_way_segments.rb [new file with mode: 0644]

index ae0d00c75c5dc6901bb2bf57bce54779f07cc96b..428d4b9c2005541846a781346856a9b42bf987e3 100644 (file)
@@ -28,7 +28,7 @@ class NodeController < ApplicationController
 
   def rest
     unless Node.exists?(params[:id])
-      render :nothing => true, :status => 400
+      render :nothing => true, :status => 404
       return
     end
 
index 2bc4f07c740dceea5c0ea92a143def42740281b3..966ede72af52e1c310bf63af4afb98dea8d5e4c1 100644 (file)
@@ -36,7 +36,7 @@ class SegmentController < ApplicationController
 
   def rest
     unless Segment.exists?(params[:id])
-      render :nothing => true, :status => 400
+      render :nothing => true, :status => 404
       return
     end
 
index 2c948214e57b5c1267ed92fc247a7e900a00ebc8..49a3b3bfec3d7de50266e9dce7dbc29b1a67c40a 100644 (file)
@@ -26,4 +26,33 @@ class WayController < ApplicationController
     render :nothing => true, :status => 500 # something went very wrong
   end
 
+  def rest
+    unless Way.exists?(params[:id])
+      render :nothing => true, :status => 404
+      return
+    end
+
+    way = Way.find(params[:id])
+    case request.method
+   
+    when :get
+      unless way.visible
+        render :nothing => true, :status => 410
+        return
+      end
+      render :text => way.to_xml.to_s
+
+    when :delete
+      unless way.visible
+        render :nothing => true, :status => 410
+        return
+      end
+
+      way.visible = false
+      way.save_with_history
+
+    end
+  end
+
 end
diff --git a/app/controllers/way_segment_controller.rb b/app/controllers/way_segment_controller.rb
new file mode 100644 (file)
index 0000000..fb3c09c
--- /dev/null
@@ -0,0 +1,2 @@
+class WaySegmentController < ApplicationController
+end
diff --git a/app/helpers/way_segment_helper.rb b/app/helpers/way_segment_helper.rb
new file mode 100644 (file)
index 0000000..f2ebd39
--- /dev/null
@@ -0,0 +1,2 @@
+module WaySegmentHelper
+end
index 2773ecc7b87ed83faec5e871d95d34f95b14fb42..9ae45d40fe07dc5ab123ce645b2d3e30223f4829 100644 (file)
@@ -2,6 +2,10 @@ class Way < ActiveRecord::Base
   require 'xml/libxml'
   
   belongs_to :user
+
+  has_many :way_segments, :foreign_key => 'id'
+  has_many :way_tags, :foreign_key => 'id'
+
   set_table_name 'current_ways'
 
   def self.from_xml(xml, create=false)
@@ -32,13 +36,43 @@ class Way < ActiveRecord::Base
       pt.find('seg').each do |seg|
         way.add_seg_num(seg['id'])
       end
 
     end
 
     return way
   end
 
+  def to_xml
+    doc = XML::Document.new
+    doc.encoding = 'UTF-8' 
+    root = XML::Node.new 'osm'
+    root['version'] = '0.4'
+    root['generator'] = 'OpenStreetMap server'
+
+    doc.root = root
+    
+    el1 = XML::Node.new 'way'
+    el1['id'] = self.id.to_s
+    el1['visible'] = self.visible.to_s
+    el1['timestamp'] = self.timestamp.xmlschema
+    
+    self.way_segments.each do |seg| # FIXME need to make sure they come back in the right order
+      e = XML::Node.new 'seg'
+      e['id'] = seg.segment_id.to_s
+      el1 << e
+    end
+    self.way_tags.each do |tag|
+      e = XML::Node.new 'tag'
+      e['k'] = tag.k
+      e['v'] = tag.v
+      el1 << e
+    end
+  
+    root << el1
+    return doc
+  end
+
   def segs
     @segs = Array.new unless @segs
     @segs
@@ -74,6 +108,18 @@ class Way < ActiveRecord::Base
       tag.save
     end
 
+    WaySegment.delete_all(['id = ?', self.id])
+    
+    i = 0
+    self.segs.each do |n|
+      seg = WaySegment.new
+      seg.id = self.id
+      seg.segment_id = n
+      seg.sequence_id = i
+      seg.save
+      i += 1
+    end
+
     old_way = OldWay.from_way(self)
     old_way.save
   end
diff --git a/app/models/way_segment.rb b/app/models/way_segment.rb
new file mode 100644 (file)
index 0000000..502174d
--- /dev/null
@@ -0,0 +1,3 @@
+class WaySegment < ActiveRecord::Base
+  set_table_name 'current_way_segments'
+end
index 7ba5ad9027fe68d5eda6832cb3ad9fc73542cb5e..5c95e148dea86e04807f59e3c8e4af4f5477e6d5 100644 (file)
@@ -10,7 +10,7 @@ ActionController::Routing::Routes.draw do |map|
   map.connect 'api/0.4/segment/:id', :controller => 'segment', :action => 'rest'
 
   map.connect 'api/0.4/way/create', :controller => 'way', :action => 'create'
-  
+  map.connect 'api/0.4/way/:id', :controller => 'way', :action => 'rest'
   
   # misc site stuff
 
diff --git a/db/migrate/011_create_way_segments.rb b/db/migrate/011_create_way_segments.rb
new file mode 100644 (file)
index 0000000..d00ed6e
--- /dev/null
@@ -0,0 +1,11 @@
+class CreateWaySegments < ActiveRecord::Migration
+  def self.up
+    create_table :way_segments do |t|
+      # t.column :name, :string
+    end
+  end
+
+  def self.down
+    drop_table :way_segments
+  end
+end