server: update distribution

This commit is contained in:
King Kévin 2023-01-28 00:34:12 +01:00
parent 423af78241
commit 6f683df850
1 changed files with 16 additions and 1 deletions

View File

@ -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