api06: Move version-checking into the models, raising an exception on mismatch
(still not implemented for delete requests though.)
This commit is contained in:
parent
ec916b7429
commit
86b4d1bc2c
7 changed files with 26 additions and 9 deletions
|
@ -49,10 +49,6 @@ class NodeController < ApplicationController
|
|||
begin
|
||||
node = Node.find(params[:id])
|
||||
new_node = Node.from_xml(request.raw_post)
|
||||
if new_node.version != node.version
|
||||
render :text => "Version mismatch: Provided " + new_node.version.to_s + ", server had: " + node.version.to_s, :status => :bad_request
|
||||
return
|
||||
end
|
||||
|
||||
if new_node and new_node.id == node.id
|
||||
node.update_from(new_node, @user)
|
||||
|
@ -60,6 +56,9 @@ class NodeController < ApplicationController
|
|||
else
|
||||
render :nothing => true, :status => :bad_request
|
||||
end
|
||||
rescue OSM::APIVersionMismatchError ex
|
||||
render :text => "Version mismatch: Provided " + ex.provided.to_s +
|
||||
", server had: " + ex.latest.to_s, :status => :bad_request
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render :nothing => true, :status => :not_found
|
||||
end
|
||||
|
|
|
@ -64,6 +64,9 @@ class RelationController < ApplicationController
|
|||
render :nothing => true, :status => :not_found
|
||||
rescue OSM::APIPreconditionFailedError
|
||||
render :text => "", :status => :precondition_failed
|
||||
rescue OSM::APIVersionMismatchError => ex
|
||||
render :text => "Version mismatch: Provided " + ex.provided.to_s +
|
||||
", server had: " + ex.latest.to_s, :status => :bad_request
|
||||
rescue
|
||||
render :nothing => true, :status => :internal_server_error
|
||||
end
|
||||
|
|
|
@ -49,11 +49,6 @@ class WayController < ApplicationController
|
|||
begin
|
||||
way = Way.find(params[:id])
|
||||
new_way = Way.from_xml(request.raw_post)
|
||||
if new_way.version != way.version
|
||||
render :text => "Version mismatch: Provided " + new_way.version.to_s + ", server had: " + way.version.to_s, :status => :bad_request
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
if new_way and new_way.id == way.id
|
||||
way.update_from(new_way, @user)
|
||||
|
@ -63,6 +58,9 @@ class WayController < ApplicationController
|
|||
end
|
||||
rescue OSM::APIPreconditionFailedError
|
||||
render :text => "", :status => :precondition_failed
|
||||
rescue OSM::APIVersionMismatchError => ex
|
||||
render :text => "Version mismatch: Provided " + ex.provided.to_s +
|
||||
", server had: " + ex.latest.to_s, :status => :bad_request
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render :nothing => true, :status => :not_found
|
||||
end
|
||||
|
|
|
@ -140,6 +140,10 @@ class Node < GeoRecord
|
|||
end
|
||||
|
||||
def update_from(new_node, user)
|
||||
if new_node.version != version
|
||||
raise OSM::APIVersionMismatchError.new(new_node.version, version)
|
||||
end
|
||||
|
||||
self.user_id = user.id
|
||||
self.latitude = new_node.latitude
|
||||
self.longitude = new_node.longitude
|
||||
|
|
|
@ -224,6 +224,8 @@ class Relation < ActiveRecord::Base
|
|||
def update_from(new_relation, user)
|
||||
if !new_relation.preconditions_ok?
|
||||
raise OSM::APIPreconditionFailedError.new
|
||||
elsif new_relation.version != version
|
||||
raise OSM::APIVersionMismatchError.new(new_relation.version, version)
|
||||
else
|
||||
self.user_id = user.id
|
||||
self.tags = new_relation.tags
|
||||
|
|
|
@ -199,6 +199,8 @@ class Way < ActiveRecord::Base
|
|||
def update_from(new_way, user)
|
||||
if !new_way.preconditions_ok?
|
||||
raise OSM::APIPreconditionFailedError.new
|
||||
elsif new_way.version != version
|
||||
raise OSM::APIVersionMismatchError.new(new_way.version, version)
|
||||
else
|
||||
self.user_id = user.id
|
||||
self.tags = new_way.tags
|
||||
|
|
|
@ -24,6 +24,15 @@ module OSM
|
|||
class APIAlreadyDeletedError < APIError
|
||||
end
|
||||
|
||||
# Raised when the provided version is not equal to the latest in the db.
|
||||
class APIVersionMismatchError < APIError
|
||||
def initialize(provided, latest)
|
||||
@provided, @latest = provided, latest
|
||||
end
|
||||
|
||||
attr_reader :provided, :latest
|
||||
end
|
||||
|
||||
# Helper methods for going to/from mercator and lat/lng.
|
||||
class Mercator
|
||||
include Math
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue