add update part capability
This commit is contained in:
parent
0860ddd4bc
commit
a55b37c7ee
|
@ -42,6 +42,7 @@
|
|||
</table>
|
||||
<div class="hscroll" id="attachments"></div>
|
||||
<button type="button" onclick="delete_part()">delete part (including children)</button>
|
||||
<button type="button" onclick="update_part()">update part (or add if not selected)</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
86
server.rb
86
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
|
||||
|
|
Loading…
Reference in New Issue