Compare commits

...

5 Commits

2 changed files with 21 additions and 22 deletions

View File

@ -236,20 +236,16 @@ function update_part()
// the fields
for (const field of fields) {
const input = document.getElementById('part_' + field);
if (input.tagName == "INPUT") {
part[field] = input.value;
} else if (input.tagName == "TEXTAREA") {
part[field] = input.innerHTML;
}
part[field] = input.value;
}
if (part["name"].length == 0) {
return;
}
// get distributors
part.distributors = [];
part.distributors = {};
const distributors = document.getElementById('distributors');
for (const distributor of distributors.rows) {
part.distributors.push({"distributor": distributor.cells[0].innerText, "sku": distributor.cells[1].firstChild.value});
part.distributors[distributor.cells[0].innerText] = distributor.cells[1].firstChild.value;
}
// get properties
part.properties = {};

View File

@ -74,7 +74,8 @@ after do
end
get '/' do
redirect to('/index.html')
content_type 'text/html'
send_file File.join(settings.public_folder, 'index.html')
end
def get_part_by_id(id)
@ -249,20 +250,23 @@ def add_part(part)
# update family
family = nil
field = "family"
part[field] = nil if part[field] and 0 == part[field].length
if part[field] then
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?
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
update.execute(family[0]["id"], part["id"])
family = get_part_by_id(family[0]["id"])
if part[field].empty? then
update = @db.prepare("UPDATE part SET #{field} = NULL WHERE id = ?")
update.execute(part["id"])
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?
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
update.execute(family[0]["id"], part["id"])
family = get_part_by_id(family[0]["id"])
end
end
# update fields
fields_txt = ["name", "description", "details", "mpn", "package", "datasheet", "page"];
fields_txt.each do |field|
next unless part[field]
part[field] = nil if part[field].kind_of?(String) and 0 == part[field].length
next if family and family[field] == part[field]
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
update.execute(part[field], part["id"])
@ -270,7 +274,6 @@ def add_part(part)
# update manufacturer and package
field_ref = ["manufacturer"]
field_ref.each do |field|
part[field] = nil if part[field] and 0 == part[field].length
next if family and family[field] == part[field]
if part[field] then
statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?")
@ -388,20 +391,20 @@ get '/import/lcsc/:lcsc' do
part["datasheet"] = result["pdfUrl"]
existing = get_part_by_name(part["name"])
halt 401, "part name already exists" if existing
puts part
begin
add_part(part)
rescue StandardError => e
halt 401, e.message
end
i = 0
(part["attachments"] + [part["datasheet"]]).each do |attachement|
file = attachement.split("/")[-1]
(part["attachments"] + [part["datasheet"]]).each do |attachment|
next unless attachment
file = attachment.split("/")[-1]
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
path = "#{dir}/#{i}_#{file}"
i += 1
unless File.file?(path) then
uri = URI(attachement)
uri = URI(attachment)
res = Net::HTTP.get_response(uri)
if (res.is_a?(Net::HTTPSuccess)) then
Dir.mkdir(dir) unless File.directory?(dir)
@ -411,5 +414,5 @@ get '/import/lcsc/:lcsc' do
end
end
end
return 200
return 200, "#{part['name']} added"
end