-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.js
More file actions
134 lines (118 loc) · 3.61 KB
/
Manager.js
File metadata and controls
134 lines (118 loc) · 3.61 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Manager View
const mysql = require('mysql');
const inq = require('inquirer');
const Table = require('easy-table');
let managerView = () => {
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'bamazon_db'
});
const LOW_INVENTORY = 30;
let menu = () => {
inq.prompt([
{
'type': 'list',
'message': 'What do you want to do?',
'choices': [
'View Products',
'View Low Inventory',
'Add to Inventory',
'Add New Product'
],
'name': 'action'
}
]).then((menu) => {
switch (menu.action) {
case 'View Products':
showProducts();
break;
case 'View Low Inventory':
getLowInventory();
break;
case 'Add to Inventory':
addInventory();
break;
case 'Add New Product':
addNewProduct();
break;
default: break;
}
});
};
// manager - view all products
let showProducts = () => {
connection.query('SELECT * FROM products', (err, data) => {
if (err) throw err;
console.log(Table.print(data));
menu();
})
};
// manager - see products with low inventories
let getLowInventory = () => {
let insert = [LOW_INVENTORY];
let sqlQuery = 'SELECT * FROM products where stock < ?';
connection.query(sqlQuery, insert, (err, res) => {
if (err) throw err;
console.log(Table.print(res));
menu();
});
};
// manager - replenish inventory to a selected item
let addInventory = () => {
inq.prompt([
{
'name': 'id',
'message': 'Which product? (ID)'
},
{
'name': 'quantity',
'message': 'How much do you want to add?'
}
]).then((selection) => {
let inserts = [parseInt(selection.quantity), parseInt(selection.id)];
let sqlQuery = 'UPDATE products SET stock = stock + ? WHERE id = ?';
connection.query(sqlQuery, inserts, (err, res) => {
if (err) throw err;
// just lets the manager know that the product's inventory update was successful
console.log(`
Product Updated!
`);
menu();
});
});
};
// manager - add a new product to the store
let addNewProduct = () => {
inq.prompt([
{
'name': 'product',
'message': 'Product Name',
},
{
'name': 'department',
'message': 'Department',
},
{
'name': 'price',
'message': 'Price',
},
{
'name': 'stock',
'message': 'Stock'
}
]).then((newItem) => {
newItem.price = parseInt(newItem.price);
newItem.stock = parseInt(newItem.stock);
connection.query('INSERT INTO products SET ?', newItem, (err, res) => {
if (err) throw err;
console.log(`
Product Added!
`);
menu();
});
});
};
menu();
};
module.exports = managerView;