server: add update properties

This commit is contained in:
King Kévin 2023-01-27 23:15:41 +01:00
parent 8db9c34830
commit f5000e0914
1 changed files with 22 additions and 0 deletions

View File

@ -307,4 +307,26 @@ post '/part' do
delete = @db.prepare("DELETE FROM inventory WHERE part = ?")
delete.execute(part["id"])
end
# update properties
field = "properties"
part[field] = nil if part[field] and 0 == part[field].length
delete = @db.prepare("DELETE FROM property_value WHERE part = ?")
delete.execute(part["id"])
if part[field] then
part[field].each do |name,values|
next if values.empty?
statement = @db.prepare("SELECT id FROM property WHERE LOWER(name) = ?")
ref = statement.execute(name.downcase).to_a[0]
unless ref then
insert = @db.prepare("INSERT INTO property (name) VALUES (?)");
insert.execute(name)
end
ref = statement.execute(name.downcase).to_a[0]
insert = @db.prepare("INSERT INTO property_value (property,part,value) VALUES (?,?,?)");
values.each do |value|
insert.execute(ref["id"], part["id"], value)
end
end
end
return 200
end