server: update distribution
This commit is contained in:
parent
423af78241
commit
6f683df850
17
server.rb
17
server.rb
|
@ -229,7 +229,7 @@ post '/part' do
|
|||
rescue
|
||||
halt 401, "not json"
|
||||
end
|
||||
puts part
|
||||
puts part if DEBUG
|
||||
if part["id"] then
|
||||
# ensure part to update exists
|
||||
statement = @db.prepare("SELECT id FROM part WHERE id = ?")
|
||||
|
@ -310,6 +310,21 @@ post '/part' do
|
|||
delete = @db.prepare("DELETE FROM inventory WHERE part = ?")
|
||||
delete.execute(part["id"])
|
||||
end
|
||||
# update distributors
|
||||
field = "distributors"
|
||||
part[field] = nil if part[field] and 0 == part[field].length
|
||||
delete = @db.prepare("DELETE FROM distribution WHERE part = ?")
|
||||
delete.execute(part["id"])
|
||||
if part[field] then
|
||||
part[field].each do |distributor,sku|
|
||||
next unless sku and !sku.empty?
|
||||
statement = @db.prepare("SELECT id FROM distributor WHERE LOWER(name) = ?")
|
||||
ref = statement.execute(name.downcase).to_a[0]
|
||||
halt(401, "distributor unknown") unless ref
|
||||
insert = @db.prepare("INSERT INTO distribution (distributor,part,sku) VALUES (?,?,?)");
|
||||
insert.execute(ref["id"], part["id"], sku)
|
||||
end
|
||||
end
|
||||
# update properties
|
||||
field = "properties"
|
||||
part[field] = nil if part[field] and 0 == part[field].length
|
||||
|
|
Loading…
Reference in New Issue