From 6a626282cd64095759a91603a3c75e01390c6055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 30 Jan 2023 12:12:40 +0100 Subject: [PATCH] remodel distributors, properties, components --- public/index.html | 2 +- public/partdb.js | 109 ++++++++++++++++++++-------------------------- server.rb | 95 +++++++++++++++++----------------------- 3 files changed, 89 insertions(+), 117 deletions(-) diff --git a/public/index.html b/public/index.html index b4ffe72..3717f0f 100644 --- a/public/index.html +++ b/public/index.html @@ -38,7 +38,7 @@ - +
propertyvalue(s)
propertyvalue
diff --git a/public/partdb.js b/public/partdb.js index 1ab16fe..f9ccda2 100644 --- a/public/partdb.js +++ b/public/partdb.js @@ -109,64 +109,55 @@ function select_part() span.appendChild(a); } } - // set distributors - const distributors = document.getElementById('distributors'); - distributors.innerHTML = null; - for (const distributor of part["distributors"]) { - const tr = document.createElement('tr'); - const td_name = document.createElement('td'); - td_name.innerText = distributor.name; - tr.appendChild(td_name); - const td_sku = document.createElement('td'); - const input = document.createElement('input'); - input.type = "text"; - input.style.width = "95%"; - if (distributor.sku) { - input.value = distributor.sku; - } - td_sku.appendChild(input); - tr.appendChild(td_sku); - const td_url = document.createElement('td'); - if (distributor.url) { - td_url.innerHTML = "link"; - } - tr.appendChild(td_url); - distributors.appendChild(tr); - } - // add properties and components - const kv_tables = ['properties', 'components']; - for (const kv_table of kv_tables) { - const table = document.getElementById(kv_table); - table.innerHTML = null; - part[kv_table][""] = ""; // empty field to add - for (const key in part[kv_table]) { + // add distributors, properties and components + for (const table of tables) { + const tbody = document.getElementById(table); + tbody.innerHTML = null; + part[table].push({name: "", value: ""}); // empty field to add + for (const row of part[table]) { const tr = document.createElement('tr'); let td = document.createElement('td'); let input = document.createElement('input'); input.type = "text"; input.style.width = "95%"; - input.value = key; + input.value = row.name; td.appendChild(input); tr.appendChild(td); - td = document.createElement('td'); - input = document.createElement('input'); - if ('components' == kv_table) { + if (null != row.value) { + td = document.createElement('td'); + input = document.createElement('input'); + input.type = "text"; + input.value = row.value; + input.style.width = "95%"; + td.appendChild(input); + tr.appendChild(td); + } + if (row.sku) { + td = document.createElement('td'); + input = document.createElement('input'); + input.type = "text"; + input.value = row.sku; + input.style.width = "95%"; + td.appendChild(input); + tr.appendChild(td); + } + if (null != row.quantity) { + td = document.createElement('td'); + input = document.createElement('input'); input.type = "number"; input.min = "0"; input.step = "1"; - input.value = part[kv_table][key] - } else { - input.type = "text"; - if ("object" == typeof part[kv_table][key]) { - input.value = part[kv_table][key].join(","); - } else { - input.value = part[kv_table][key]; - } + input.value = row.quantity; + input.style.width = "95%"; + td.appendChild(input); + tr.appendChild(td); } - input.style.width = "95%"; - td.appendChild(input); - tr.appendChild(td); - table.appendChild(tr); + if (row.url) { + td = document.createElement('td'); + td.innerHTML = "link"; + tr.appendChild(td); + } + tbody.appendChild(tr); } } // add attachments @@ -226,32 +217,28 @@ function update_part() return; } // get distributors - part.distributors = {}; + part.distributors = []; const distributors = document.getElementById('distributors'); for (const distributor of distributors.rows) { - part.distributors[distributor.cells[0].innerText] = distributor.cells[1].firstChild.value; + part.distributors.push({name: distributor.cells[0].firstChild.value, sku: distributor.cells[1].firstChild.value}); } // get properties - part.properties = {}; + part.properties = []; const properties = document.getElementById('properties'); for (const prop of properties.rows) { - const name = prop.cells[0].firstChild.value; - const value = prop.cells[1].firstChild.value; - if (name && name.length > 0 && value && value.length > 0) { - part.properties[name] = value.split(","); - } + part.properties.push({name: prop.cells[0].firstChild.value, value: prop.cells[1].firstChild.value}); } // get components - part.components = {}; + part.components = []; const components = document.getElementById('components'); for (const component of components.rows) { - const name = component.cells[0].firstChild.value; - const quantity = component.cells[1].firstChild.value; - if (name && name.length > 0) { - part.components[name] = parseInt(quantity); + const nam = component.cells[0].firstChild.value; + const q = component.cells[1].firstChild.value; + if (nam && nam.length > 0) { + part.components.push({name: nam, quantity: parseInt(q)}); } } - //console.log(part); + console.log(part); var post = new XMLHttpRequest(); post.open("POST", "part"); diff --git a/server.rb b/server.rb index cf63317..27df509 100755 --- a/server.rb +++ b/server.rb @@ -90,22 +90,13 @@ def get_part_by_id(id) part[k] ||= parent[k] end end - # add all distributors - distributors = @db.query("SELECT * FROM distributor").to_a - statement = @db.prepare("SELECT * FROM distribution WHERE part = ?") - distributions = statement.execute(id).to_a - distributors.each do |distributor| - distributions.each do |distribution| - if distribution["distributor"] == distributor["id"] then - distributor["sku"] = distribution["sku"] - distributor["url"] = distributor["product_page"].gsub("%s", distribution["sku"]) - end - end - distributor.delete("id") - distributor.delete("homepage") - distributor.delete("product_page") + # add distributors + part["distributors"] = [] + statement = @db.prepare("SELECT distribution.sku AS sku, distributor.name AS name, distributor.product_page AS url FROM distribution LEFT JOIN distributor ON distributor.id = distribution.distributor WHERE distribution.part = ?") + statement.execute(id).each do |distribution| + distribution["url"].gsub!("%s", distribution["sku"]) + part["distributors"] << distribution end - part["distributors"] = distributors # add inventory statement = @db.prepare("SELECT location.name AS location, inventory.quantity AS stock FROM inventory LEFT JOIN location ON location.id = inventory.location WHERE inventory.part = ? ORDER BY inventory.quantity DESC LIMIT 1") inventory = statement.execute(id).to_a[0] @@ -114,17 +105,12 @@ def get_part_by_id(id) part["stock"] = inventory["stock"] end # add properties - part["properties"] = {} + part["properties"] = [] statement = @db.prepare("SELECT property.name AS name, property_value.value AS value FROM property_value JOIN property ON property.id = property_value.property WHERE property_value.part = ?") statement.execute(id).each do |row| - part["properties"][row["name"]] ||= [] - part["properties"][row["name"]] << row["value"] - end - if parent then - parent["properties"].each do |k,v| - part["properties"][k] ||= v - end + part["properties"] << row end + part["properties"] += parent["properties"] if parent # add attachments part["attachments"] = [] dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"].gsub("/", "_") @@ -136,20 +122,14 @@ def get_part_by_id(id) end end part["attachments"].sort! - if parent then - part["attachments"] += parent["attachments"] - end + part["attachments"] += parent["attachments"] if parent # add components for assembly - part["components"] = {} - if parent then - parent["components"].each do |k,v| - part["components"][k] ||= v - end - end + part["components"] = [] statement = @db.prepare("SELECT part.name AS name, assembly.quantity AS quantity FROM assembly JOIN part ON part.id = assembly.component WHERE assembly.assembled = ?") statement.execute(id).each do |row| - part["components"][row["name"]] = row["quantity"] + part["components"] << row end + part["components"] += parent["components"] if parent # clean up delete = ["parent"] delete.each do |k| @@ -345,7 +325,7 @@ def add_part(part) statement = @db.prepare("SELECT id FROM inventory WHERE part = ? AND location = ?") ref_inv = statement.execute(part["id"], ref["id"]).to_a[0] unless ref_inv then - insert = @db.prepare("INSERT INTO inventory (part, location, quantity) VALUES (?,?,?)"); + insert = @db.prepare("INSERT INTO inventory (part, location, quantity) VALUES (?,?,?)") insert.execute(part["id"], ref["id"], part["stock"].to_i) end ref_inv = statement.execute(part["id"], ref["id"]).to_a[0] @@ -361,13 +341,18 @@ def add_part(part) delete = @db.prepare("DELETE FROM distribution WHERE part = ?") delete.execute(part["id"]) if part[field] then - part[field].each do |distributor,sku| - next unless sku and !sku.empty? + part[field].each do |row| + next unless row["name"] and !row["name"].empty? + next unless row["sku"] and !row["sku"].empty? statement = @db.prepare("SELECT id FROM distributor WHERE LOWER(name) = ?") - ref = statement.execute(distributor.downcase).to_a[0] - raise StandardError.new("distributor unknown") unless ref - insert = @db.prepare("INSERT INTO distribution (distributor,part,sku) VALUES (?,?,?)"); - insert.execute(ref["id"], part["id"], sku) + ref = statement.execute(row["name"].downcase).to_a[0] + unless ref then + insert = @db.prepare("INSERT INTO distributor (name) VALUES (?)"); + insert.execute(row["name"]) + ref = statement.execute(row["name"].downcase).to_a[0] + end + insert = @db.prepare("INSERT INTO distribution (distributor,part,sku) VALUES (?,?,?)") + insert.execute(ref["id"], part["id"], row["sku"]) end end # update properties @@ -375,20 +360,19 @@ def add_part(part) if part[field] then delete = @db.prepare("DELETE FROM property_value WHERE part = ?") delete.execute(part["id"]) - part[field].each do |name,values| - next unless values and !values.empty? + part[field].each do |row| + next unless row["name"] and !row["name"].empty? + next unless row["value"] and !row["value"].empty? + next if family and family["properties"] and family["properties"].include?(row) statement = @db.prepare("SELECT id FROM property WHERE LOWER(name) = ?") - ref = statement.execute(name.downcase).to_a[0] + ref = statement.execute(row["name"].downcase).to_a[0] unless ref then insert = @db.prepare("INSERT INTO property (name) VALUES (?)"); - insert.execute(name) - end - ref = statement.execute(name.downcase).to_a[0] - insert = @db.prepare("INSERT INTO property_value (property,part,value) VALUES (?,?,?)"); - values.each do |value| - next if family and family["properties"] and family["properties"][name] and family["properties"][name].include?(value) - insert.execute(ref["id"], part["id"], value) + insert.execute(row["name"]) + ref = statement.execute(row["name"].downcase).to_a[0] end + insert = @db.prepare("INSERT INTO property_value (property,part,value) VALUES (?,?,?)") + insert.execute(ref["id"], part["id"], row["value"]) end end # update components @@ -396,15 +380,16 @@ def add_part(part) if part[field] then delete = @db.prepare("DELETE FROM assembly WHERE assembled = ?") delete.execute(part["id"]) - part[field].each do |name,quantity| - next unless name + part[field].each do |row| + next unless row["name"] and !row["name"].empty? + next unless row["quantity"] statement = @db.prepare("SELECT id FROM part WHERE LOWER(name) = ?") - ref = statement.execute(name.downcase).to_a[0] + ref = statement.execute(row["name"].downcase).to_a[0] #raise StandardError.new("component #{name} does not exist") unless ref next unless ref - quantity ||= 0 + row["quantity"] ||= 0 insert = @db.prepare("INSERT INTO assembly (assembled,component,quantity) VALUES (?,?,?)"); - insert.execute(part["id"], ref["id"], quantity) + insert.execute(part["id"], ref["id"], row["quantity"]) end end end