Compare commits
3 Commits
76fec09c4b
...
efeaebce59
Author | SHA1 | Date |
---|---|---|
King Kévin | efeaebce59 | |
King Kévin | 5fc71e50db | |
King Kévin | 920c90e4ac |
|
@ -4,8 +4,8 @@ usage
|
|||
=====
|
||||
|
||||
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.
|
||||
Just add the files directly on the server under `public/attachments/<part_name>/`.
|
||||
The only aspect not handled by the web frontend is the attachment deletion.
|
||||
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.
|
||||
|
||||
|
|
16
server.rb
16
server.rb
|
@ -252,12 +252,12 @@ def add_part(part)
|
|||
if part["id"] then
|
||||
# ensure part to update exists
|
||||
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
|
||||
# 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 = ?")
|
||||
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.execute(part["name"])
|
||||
part["id"] = statement.execute(part["name"]).to_a[0]["id"]
|
||||
|
@ -273,7 +273,7 @@ def add_part(part)
|
|||
else
|
||||
statement = @db.prepare("SELECT id FROM part WHERE name = ?")
|
||||
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.execute(family[0]["id"], part["id"])
|
||||
family = get_part_by_id(family[0]["id"])
|
||||
|
@ -384,7 +384,7 @@ def add_part(part)
|
|||
next unless row["quantity"]
|
||||
statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?")
|
||||
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
|
||||
row["quantity"] ||= 0
|
||||
insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)");
|
||||
|
@ -409,7 +409,7 @@ post '/part' do
|
|||
puts part if DEBUG
|
||||
begin
|
||||
add_part(part)
|
||||
rescue StandardError => e
|
||||
rescue ScriptError => e
|
||||
halt 401, e.message
|
||||
end
|
||||
return 200
|
||||
|
@ -431,14 +431,14 @@ get '/import/lcsc/:lcsc' do
|
|||
part["details"] = result["productIntroEn"]
|
||||
part["manufacturer"] = result["brandNameEn"]
|
||||
part["package"] = result["encapStandard"]
|
||||
part["distributors"] = {"LCSC" => result["productCode"]}
|
||||
part["distributors"] = [{"name" => "LCSC", "sku" => result["productCode"]}]
|
||||
part["attachments"] = result["productImages"]
|
||||
part["datasheet"] = result["pdfUrl"]
|
||||
existing = get_part_by_name(part["name"])
|
||||
halt 401, "part #{part['name']} already exists" if existing
|
||||
begin
|
||||
add_part(part)
|
||||
rescue StandardError => e
|
||||
rescue ScriptError => e
|
||||
halt 401, e.message
|
||||
end
|
||||
i = 0
|
||||
|
|
Loading…
Reference in New Issue