-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
140 lines (121 loc) · 3.17 KB
/
bamazonManager.js
File metadata and controls
140 lines (121 loc) · 3.17 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
135
136
137
138
139
140
//require packages
const mysql = require("mysql"),
inquirer = require("inquirer"),
colors = require("colors");
//create mysql connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'bamazon'
});
connection.connect();
const manager = _ => {
connection.query("SELECT * FROM products", (err, res) => {
if (err) throw (err);
inquirer.prompt([{
type: "list",
name: "options",
message: "What would you like to do Mister Manager?",
choices: ["View Products For Sale", "View Low Inventory", "Add To Inventory", "Add New Product", "Nothing"]
}]).then(response => {
switch (response.options) {
case "View Products For Sale":
viewProducts();
break;
case "View Low Inventory":
viewLowInventory();
break;
case "Nothing":
process.exit();
break;
case "Add New Product":
addNewProduct();
break;
case "Add To Inventory":
increaseStock();
break;
}
})
})
}
const viewProducts = _ => {
connection.query("SELECT * FROM products", (err, res) => {
if (err) throw (err);
console.table(res);
manager();
})
}
const viewLowInventory = _ => {
connection.query("SELECT * FROM products WHERE stock < 20", (err, res) => {
if (err) throw err;
console.table(res);
manager();
})
}
const addNewProduct = _ => {
inquirer.prompt([{
type: "input",
name: "productName",
message: "What is the name of the product you would like to add?"
}, {
type: "input",
name: "departmentName",
message: "What department does this item belong to?"
}, {
type: "input",
name: "price",
message: "How much does this item cost?"
}, {
type: "input",
name: "stock",
message: "How many do we initially have in stock?"
}]).then(res => {
connection.query("INSERT INTO products SET ?", {
productName: res.productName,
departmentName: res.departmentName,
price: res.price,
stock: res.stock
}, (err, res) => {
if (err) throw err;
console.log("This item has been added to our inventory".green);
manager();
})
})
}
const increaseStock = _ => {
inquirer.prompt([{
name: "product",
type: "rawlist",
message: "What product would you like to stock up on?",
choices: _ => {
let productsArray = [];
for (i = 0; i < res.length; i++) {
productsArray.push(res[i].productName);
}
return productsArray
}
}, {
name: "quantity",
type: "input",
message: "How many would you like to add to your stock? (Please enter numerically)"
}]).then(inquirerRes => {
let chosenItem;
for (i = 0; i < res.length; i++) {
if (res[i].productName === inquirerRes.product) {
chosenItem = res[i]
}
}
connection.query("UPDATE products SET ? WHERE ?", [{
stock: chosenItem.stock + parseInt(inquirerRes.quantity)
}, {
id: chosenItem.id
}], err => {
if (err) throw err;
console.log("Stock has been added!".green);
manager();
})
})
}
//first run of application
manager();