server: put add_part as seperate function

This commit is contained in:
King Kévin 2023-01-29 09:07:09 +01:00
parent 08df82d092
commit 35a865e2db
1 changed files with 21 additions and 13 deletions

View File

@ -230,23 +230,16 @@ get '/delete/:id' do
return 200 return 200
end end
post '/part' do def add_part(part)
request.body.rewind
begin
part = JSON.parse(request.body.read)
rescue
halt 401, "not json"
end
puts part if DEBUG
if part["id"] then if part["id"] then
# ensure part to update exists # ensure part to update exists
statement = @db.prepare("SELECT id FROM part WHERE id = ?") statement = @db.prepare("SELECT id FROM part WHERE id = ?")
halt(401, "id not valid") if statement.execute(part["id"]).to_a.empty? raise StandardError.new("id not valid") if statement.execute(part["id"]).to_a.empty?
else else
# add new part # add new part
halt(401, "name required") unless part["name"] and part["name"].length > 0 raise StandardError.new("name required") unless part["name"] and part["name"].length > 0
statement = @db.prepare("SELECT id FROM part WHERE name = ?") statement = @db.prepare("SELECT id FROM part WHERE name = ?")
halt(401, "name already existing") unless statement.execute(part["name"]).to_a.empty? raise StandardError.new("name already existing") unless statement.execute(part["name"]).to_a.empty?
insert = @db.prepare("INSERT INTO part (name) VALUES (?)"); insert = @db.prepare("INSERT INTO part (name) VALUES (?)");
insert.execute(part["name"]) insert.execute(part["name"])
part["id"] = statement.execute(part["name"]).to_a[0]["id"] part["id"] = statement.execute(part["name"]).to_a[0]["id"]
@ -258,7 +251,7 @@ post '/part' do
if part[field] then if part[field] then
statement = @db.prepare("SELECT id FROM part WHERE name = ?") statement = @db.prepare("SELECT id FROM part WHERE name = ?")
family = statement.execute(part[field]).to_a family = statement.execute(part[field]).to_a
halt(401, "family not existing") if family.empty? raise StandardError.new("family not existing") if family.empty?
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?") update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
update.execute(family[0]["id"], part["id"]) update.execute(family[0]["id"], part["id"])
family = get_part_by_id(family[0]["id"]) family = get_part_by_id(family[0]["id"])
@ -328,7 +321,7 @@ post '/part' do
next unless sku and !sku.empty? next unless sku and !sku.empty?
statement = @db.prepare("SELECT id FROM distributor WHERE LOWER(name) = ?") statement = @db.prepare("SELECT id FROM distributor WHERE LOWER(name) = ?")
ref = statement.execute(distributor.downcase).to_a[0] ref = statement.execute(distributor.downcase).to_a[0]
halt(401, "distributor unknown") unless ref raise StandardError.new("distributor unknown") unless ref
insert = @db.prepare("INSERT INTO distribution (distributor,part,sku) VALUES (?,?,?)"); insert = @db.prepare("INSERT INTO distribution (distributor,part,sku) VALUES (?,?,?)");
insert.execute(ref["id"], part["id"], sku) insert.execute(ref["id"], part["id"], sku)
end end
@ -355,5 +348,20 @@ post '/part' do
end end
end end
end end
end
post '/part' do
request.body.rewind
begin
part = JSON.parse(request.body.read)
rescue
halt 401, "not json"
end
puts part if DEBUG
begin
add_part(part)
rescue StandardError => e
halt 401, e.message
end
return 200 return 200
end end