Compare commits

...

3 Commits

2 changed files with 10 additions and 10 deletions

View File

@ -4,8 +4,8 @@ usage
===== =====
Once the service is installed, there is a single page to search, add, delete, or update parts. Once the service is installed, there is a single page to search, add, delete, or update parts.
The only aspect not handled by the web frontend is the attachments. The only aspect not handled by the web frontend is the attachment deletion.
Just add the files directly on the server under `public/attachments/<part_name>/`. Just remove the files directly on the server under `public/attachments/<part_name>/`.
To import an LCSC part, simply go to the `/import/lcsc/Cxxxx` page and the part will be added to the database. To import an LCSC part, simply go to the `/import/lcsc/Cxxxx` page and the part will be added to the database.

View File

@ -252,12 +252,12 @@ def add_part(part)
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 = ?")
raise StandardError.new("id not valid") if statement.execute(part["id"]).to_a.empty? raise ScriptError.new("id not valid") if statement.execute(part["id"]).to_a.empty?
else else
# add new part # add new part
raise StandardError.new("name required") unless part["name"] and part["name"].length > 0 raise ScriptError.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 = ?")
raise StandardError.new("name already existing") unless statement.execute(part["name"]).to_a.empty? raise ScriptError.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"]
@ -273,7 +273,7 @@ def add_part(part)
else else
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
raise StandardError.new("family not existing") if family.empty? raise ScriptError.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"])
@ -384,7 +384,7 @@ def add_part(part)
next unless row["quantity"] next unless row["quantity"]
statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?") statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?")
ref = statement.execute(row["name"].downcase).to_a[0] ref = statement.execute(row["name"].downcase).to_a[0]
#raise StandardError.new("component #{name} does not exist") unless ref #raise ScriptError.new("component #{name} does not exist") unless ref
next unless ref next unless ref
row["quantity"] ||= 0 row["quantity"] ||= 0
insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)"); insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)");
@ -409,7 +409,7 @@ post '/part' do
puts part if DEBUG puts part if DEBUG
begin begin
add_part(part) add_part(part)
rescue StandardError => e rescue ScriptError => e
halt 401, e.message halt 401, e.message
end end
return 200 return 200
@ -431,14 +431,14 @@ get '/import/lcsc/:lcsc' do
part["details"] = result["productIntroEn"] part["details"] = result["productIntroEn"]
part["manufacturer"] = result["brandNameEn"] part["manufacturer"] = result["brandNameEn"]
part["package"] = result["encapStandard"] part["package"] = result["encapStandard"]
part["distributors"] = {"LCSC" => result["productCode"]} part["distributors"] = [{"name" => "LCSC", "sku" => result["productCode"]}]
part["attachments"] = result["productImages"] part["attachments"] = result["productImages"]
part["datasheet"] = result["pdfUrl"] part["datasheet"] = result["pdfUrl"]
existing = get_part_by_name(part["name"]) existing = get_part_by_name(part["name"])
halt 401, "part #{part['name']} already exists" if existing halt 401, "part #{part['name']} already exists" if existing
begin begin
add_part(part) add_part(part)
rescue StandardError => e rescue ScriptError => e
halt 401, e.message halt 401, e.message
end end
i = 0 i = 0