From a55b37c7eeaa0fedcd3ec13b080c8226bb812a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 27 Jan 2023 02:04:03 +0100 Subject: [PATCH] add update part capability --- public/index.html | 1 + public/partdb.js | 52 ++++++++++++++++++++++++++++ server.rb | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) diff --git a/public/index.html b/public/index.html index a45f5fa..79a9b41 100644 --- a/public/index.html +++ b/public/index.html @@ -42,6 +42,7 @@
+ diff --git a/public/partdb.js b/public/partdb.js index b11298e..bb96cad 100644 --- a/public/partdb.js +++ b/public/partdb.js @@ -204,3 +204,55 @@ function delete_part() }; xhr.send(); } + +function update_part() +{ + // the part to update + let part = {}; + // its id + const results = document.getElementById('results'); + if (results.selectedIndex >= 0) { + part.id = parseInt(results.options[results.selectedIndex].value); + } + // 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; + } + } + if (part["name"].length == 0) { + return; + } + // get 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}); + } + // get 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(","); + } + } + console.log(part); + + var post = new XMLHttpRequest(); + post.open("POST", "part"); + post.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + post.onload = function() { + clear(); + search(); // refresh search + }; + post.onerror = function() { + console.log("update part failed"); + }; + post.send(JSON.stringify(part)); +} diff --git a/server.rb b/server.rb index 3e1025b..d3ac046 100755 --- a/server.rb +++ b/server.rb @@ -222,3 +222,89 @@ get '/delete/:id' do delete_part(params['id']) return 200 end + +post '/part' do + request.body.rewind + begin + part = JSON.parse(request.body.read) + rescue + halt 401, "not json" + end + puts part + if part["id"] then + # ensure part to update exists + statement = @db.prepare("SELECT id FROM part WHERE id = ?") + halt(401, "id not valid") if statement.execute(part["id"]).to_a.empty? + else + # add new part + halt(401, "name required") unless part["name"] and part["name"].length > 0 + statement = @db.prepare("SELECT id FROM part WHERE name = ?") + halt(401, "name already existing") unless part = 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"] + end + # update fields + fields_txt = ["name", "description", "details", "package", "pincount", "family", "datasheet", "page"]; + fields_txt.each do |field| + halt(401, "#{field} missing") unless part[field] + part[field] = nil if 0 == part[field].length + update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?") + update.execute(part["id"], part[field]) + end + fields_int = ["pincount"] + fields_int.each do |field| + halt(401, "#{field} missing") unless part[field] + if 0 == part[field].length then + part[field] = nil + else + part[field] = part[field].to_i + end + update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?") + update.execute(part["id"], part[field]) + end + # update manufacturer and package + field_ref = ["manufacturer", "package"] + field_ref.each do |field| + part[field] = nil if part[field] and 0 == part[field].length + if part[field] then + statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?") + ref = statement.execute(part[field].downcase).to_a[0] + unless ref then + insert = @db.prepare("INSERT INTO #{field} (name) VALUES (?)"); + insert.execute(part[field]) + end + ref = statement.execute(part[field]).to_a[0] + update = @db.prepare("UPDATE part SET #{field} = ? WHERE id = ?") + update.execute(ref["id"], part["id"]) + else + update = @db.prepare("UPDATE part SET #{field} = NULL WHERE id = ?") + update.execute(part["id"]) + end + end + # update inventory + field = "location" + part[field] = nil if part[field] and 0 == part[field].length + part["location"] = nil if part["stock"] and 0 == part["stock"].length + if part[field] then + statement = @db.prepare("SELECT id FROM #{field} WHERE LOWER(name) = ?") + ref = statement.execute(part[field].downcase).to_a[0] + unless ref then + insert = @db.prepare("INSERT INTO #{field} (name) VALUES (?)"); + insert.execute(part[field]) + end + ref = statement.execute(part[field]).to_a[0] + 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) VALUES (?,?)"); + insert.execute(part["id"], ref["id"]) + end + ref_inv = statement.execute(part["id"], ref["id"]).to_a[0] + update = @db.prepare("UPDATE inventory SET quantity = ? WHERE id = ?") + update.execute(part["stock"].to_i, ref_inv["id"]) + else + delete = @db.prepare("DELETE FROM inventory WHERE part = ?") + delete.execute(part["id"]) + end +end