-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonMethods.js
92 lines (78 loc) · 2.78 KB
/
bamazonMethods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const mysql = require("mysql");
const colors = require('colors');
// Connnect to database
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "Fodder165*",
database: "inventory_db"
});
connection.connect(function (err) {
if (err) throw (err);
});
const handelInventoryList = () => {
connection.query("SELECT * FROM products", function (err, res) {
if (err) throw (err);
console.log(`\nAvailable Inventory\n`)
// console.log(res);
res.forEach((product) => {
console.log(`Product ID: ${product.item_id} || Product Name: ${product.product_name} || Price: $${product.price.toFixed(2)} || Quantity: ${product.stock_quantity}`);
})
})
// connection.end()
}
const handleLowInventory = () => {
connection.query("SELECT * FROM products WHERE stock_quantity < 5", function (err, res) {
if (err) throw (err);
console.log(`\nList of products with quantity less than five\n`.red.bold);
res.forEach((product) => {
console.log(`Product ID: ${product.item_id} || Product Name: ${product.product_name} || Price: $${product.price.toFixed(2)} || Quantity: ${product.stock_quantity}`);
})
})
};
const handleAddInventory = (id, quantity) => {
console.log('invoke handle low inventory', id, quantity)
// Get the product to increase the quantity
connection.query("SELECT * FROM products WHERE item_id=?", [id], function (err, res) {
console.log(res);
// Get item current stock quantity
const currentQuantity = res[0].stock_quantity;
// Calculate new stock quantity
const updateQuantity = parseInt(currentQuantity) + parseInt(quantity);
console.log('updated quantity', updateQuantity);
// Update database stock quantity
connection.query("UPDATE products SET ? WHERE ?", [{ stock_quantity: updateQuantity }, { item_id: id }], function (err, res) {
console.log('stock been updated')
handelInventoryList();
connection.end();
});
})
};
const handleAddNewProductDatabase = (name, department, price, quantity) => {
console.log(name, department, price, quantity)
connection.query("INSERT INTO products SET ?",
{
product_name: name,
department_name: department,
price: price,
stock_quantity: quantity
},
function (err, res) {
console.log(res.affectedRows + " product inserted!\n");
// handelInventoryList();
// handleEndConnection();
});
}
const handleEndConnection = () => {
// console.log('connection ended')
connection.end()
}
// Export functions
module.exports = {
handelInventoryList: handelInventoryList,
handleLowInventory: handleLowInventory,
handleAddInventory: handleAddInventory,
handleAddNewProductDatabase: handleAddNewProductDatabase,
handleEndConnection: handleEndConnection
};