Compare commits
5 Commits
e58aefc3c5
...
a013e0fc22
Author | SHA1 | Date |
---|---|---|
King Kévin | a013e0fc22 | |
King Kévin | 47292d659f | |
King Kévin | d2779976b6 | |
King Kévin | 3e01f7bf45 | |
King Kévin | 71aa43faf0 |
|
@ -236,20 +236,16 @@ function update_part()
|
||||||
// the fields
|
// the fields
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
const input = document.getElementById('part_' + field);
|
const input = document.getElementById('part_' + field);
|
||||||
if (input.tagName == "INPUT") {
|
part[field] = input.value;
|
||||||
part[field] = input.value;
|
|
||||||
} else if (input.tagName == "TEXTAREA") {
|
|
||||||
part[field] = input.innerHTML;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (part["name"].length == 0) {
|
if (part["name"].length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get distributors
|
// get distributors
|
||||||
part.distributors = [];
|
part.distributors = {};
|
||||||
const distributors = document.getElementById('distributors');
|
const distributors = document.getElementById('distributors');
|
||||||
for (const distributor of distributors.rows) {
|
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
|
// get properties
|
||||||
part.properties = {};
|
part.properties = {};
|
||||||
|
|
33
server.rb
33
server.rb
|
@ -74,7 +74,8 @@ after do
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
redirect to('/index.html')
|
content_type 'text/html'
|
||||||
|
send_file File.join(settings.public_folder, 'index.html')
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_part_by_id(id)
|
def get_part_by_id(id)
|
||||||
|
@ -249,20 +250,23 @@ def add_part(part)
|
||||||
# update family
|
# update family
|
||||||
family = nil
|
family = nil
|
||||||
field = "family"
|
field = "family"
|
||||||
part[field] = nil if part[field] and 0 == part[field].length
|
|
||||||
if part[field] then
|
if part[field] then
|
||||||
statement = @db.prepare("SELECT id FROM part WHERE name = ?")
|
if part[field].empty? then
|
||||||
family = statement.execute(part[field]).to_a
|
update = @db.prepare("UPDATE part SET #{field} = NULL WHERE id = ?")
|
||||||
raise StandardError.new("family not existing") if family.empty?
|
update.execute(part["id"])
|
||||||
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
|
else
|
||||||
update.execute(family[0]["id"], part["id"])
|
statement = @db.prepare("SELECT id FROM part WHERE name = ?")
|
||||||
family = get_part_by_id(family[0]["id"])
|
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
|
end
|
||||||
# update fields
|
# update fields
|
||||||
fields_txt = ["name", "description", "details", "mpn", "package", "datasheet", "page"];
|
fields_txt = ["name", "description", "details", "mpn", "package", "datasheet", "page"];
|
||||||
fields_txt.each do |field|
|
fields_txt.each do |field|
|
||||||
next unless part[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]
|
next if family and family[field] == part[field]
|
||||||
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
|
update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?")
|
||||||
update.execute(part[field], part["id"])
|
update.execute(part[field], part["id"])
|
||||||
|
@ -270,7 +274,6 @@ def add_part(part)
|
||||||
# update manufacturer and package
|
# update manufacturer and package
|
||||||
field_ref = ["manufacturer"]
|
field_ref = ["manufacturer"]
|
||||||
field_ref.each do |field|
|
field_ref.each do |field|
|
||||||
part[field] = nil if part[field] and 0 == part[field].length
|
|
||||||
next if family and family[field] == part[field]
|
next if family and family[field] == part[field]
|
||||||
if part[field] then
|
if part[field] then
|
||||||
statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?")
|
statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?")
|
||||||
|
@ -388,20 +391,20 @@ get '/import/lcsc/:lcsc' do
|
||||||
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 name already exists" if existing
|
halt 401, "part name already exists" if existing
|
||||||
puts part
|
|
||||||
begin
|
begin
|
||||||
add_part(part)
|
add_part(part)
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
halt 401, e.message
|
halt 401, e.message
|
||||||
end
|
end
|
||||||
i = 0
|
i = 0
|
||||||
(part["attachments"] + [part["datasheet"]]).each do |attachement|
|
(part["attachments"] + [part["datasheet"]]).each do |attachment|
|
||||||
file = attachement.split("/")[-1]
|
next unless attachment
|
||||||
|
file = attachment.split("/")[-1]
|
||||||
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
|
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
|
||||||
path = "#{dir}/#{i}_#{file}"
|
path = "#{dir}/#{i}_#{file}"
|
||||||
i += 1
|
i += 1
|
||||||
unless File.file?(path) then
|
unless File.file?(path) then
|
||||||
uri = URI(attachement)
|
uri = URI(attachment)
|
||||||
res = Net::HTTP.get_response(uri)
|
res = Net::HTTP.get_response(uri)
|
||||||
if (res.is_a?(Net::HTTPSuccess)) then
|
if (res.is_a?(Net::HTTPSuccess)) then
|
||||||
Dir.mkdir(dir) unless File.directory?(dir)
|
Dir.mkdir(dir) unless File.directory?(dir)
|
||||||
|
@ -411,5 +414,5 @@ get '/import/lcsc/:lcsc' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return 200
|
return 200, "#{part['name']} added"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue