-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonManager.js
169 lines (153 loc) · 4.92 KB
/
bamazonManager.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var inquirer = require('inquirer');
var mysql = require('mysql');
var Table = require('cli-table');
var color = require('colors');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Elvir@93',
port: 3306,
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
displayQuestion();
});
var managerMenu = [
{
type: 'list',
name: 'menu',
message: 'What would you like to do?',
choices: ['View Products for Sale', 'View Low Inventory', 'Add to Inventory', 'Add New Product', 'Exit']
}
];
var addToInventory = [
{
type : 'input',
name: 'items_id',
message: 'Please Enter The Id of the Item you would like to increase: ',
validate: function(value) {
if(isNaN(parseFloat(value))){
return 'Please enter a number';
} else if (value >= 1 && value <= 6) {
return true;
}
return 'Sorry the Id you have entered does not match any of our item Please enter a valid Id';
}
},
{
type: 'input',
name: 'quantityToAdd',
message: 'How many more would you like to add?',
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number
},
];
var newProduct = [
{
type: 'input',
name: 'NewItemName',
message: 'New Product Name:',
},
{
type: 'input',
name: 'NewItemDeparment',
message: 'Departement:',
},
{
type: 'input',
name: 'NewQuantity',
message: 'How many more would you like to add?',
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number
},
{
type: 'input',
name: 'NewItemPrice',
message: 'Price:',
validate: function(value) {
var valid = !isNaN(parseFloat(value));
return valid || 'Please enter a number';
},
filter: Number
},
];
function displayQuestion() {
inquirer.prompt(managerMenu).then(answers => {
switch (answers.menu) {
case 'View Products for Sale':
displayProducts();
break;
case 'View Low Inventory':
displayLowInventory();
break;
case 'Add to Inventory':
increaseInventory();
break;
case 'Add New Product':
addNewProduct();
break;
case 'Exit':
connection.end();
}
});
}
function displayProducts() {
connection.query('SELECT * FROM products', function (err, res) {
if (err) throw err;
var table = new Table({
head: ['Id', 'Items', 'Quantity', 'Prices$'],
colWidths: [10, 40, 10, 10],
colAligns: ['middle', 'left', 'middle', 'right']
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].item_id, res[i].product_name, res[i].stock_quantity, res[i].price]);
}
console.log(table.toString());
displayQuestion();
})
}
function displayLowInventory(){
connection.query('SELECT * FROM products WHERE stock_quantity < 15', function (err, res) {
if (err) throw err;
var table = new Table({
head: ['Id', 'Items', 'Quantity', 'Prices$'],
colWidths: [10, 40, 10, 10],
colAligns: ['middle', 'left', 'middle', 'right']
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].item_id, res[i].product_name, res[i].stock_quantity, res[i].price]);
}
console.log(table.toString());
displayQuestion();
})
}
function increaseInventory(){
inquirer.prompt(addToInventory).then(answers =>{
var id = answers.items_id;
var quan = answers.quantityToAdd;
connection.query('UPDATE products SET stock_quantity = stock_quantity +'+quan+' WHERE item_id = '+id);
console.log('Stock reloaded!');
displayQuestion();
})
}
function addNewProduct(){
inquirer.prompt(newProduct).then(answers =>{
var product_name = JSON.stringify(answers.NewItemName);
var department_name = JSON.stringify(answers.NewItemDeparment);
var price = answers.NewItemPrice;
var stock_quantity = answers.NewQuantity
connection.query("INSERT INTO products(product_name, department_name, price, stock_quantity) VALUES("+product_name+", "+department_name+", "+price+", "+stock_quantity+")");
console.log('~~~~~~~~~~~~~~~~~~~~~~~~');
console.log('~~~~'+'Product added!!!'.green+'~~~~');
console.log('~~~~~~~~~~~~~~~~~~~~~~~~');
displayQuestion();
})
}