-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.js
More file actions
166 lines (158 loc) · 4.08 KB
/
Copy pathmanager.js
File metadata and controls
166 lines (158 loc) · 4.08 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
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
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "insecure",
database: "bamazon"
});
connection.connect(function(err) {
if (err) {
return console.log(err);
}
adminMenu();
});
var adminMenu = function() {
inquirer.prompt([
{
type: "list",
message: "Please select:",
choices: [
"View Products for Sale",
"View Low Inventory",
"Add to Inventory",
"Add New Product",
"Exit"
],
name: "choice"
}
]).then(function(res) {
switch (res.choice) {
case ("Exit"):
connection.end();
return;
case ("View Products for Sale"):
adminQuery();
break;
case ("View Low Inventory"):
adminLowInventory();
break;
case ("Add to Inventory"):
adminAddInvetory();
break;
case ("Add New Product"):
adminAddItem();
break;
}
});
function adminQuery() {
console.log("Items up for sale");
console.log("------------------");
connection.query("SELECT * FROM products", function(err, res) {
for (var i = 0; i < res.length; i++) {
console.log(res[i].item_id + " | " + res[i].product_name + " | $" + res[i].price + " | " + res[i].quantity);
console.log("------------------");
}
adminMenu();
});
}
var adminAddInvetory = function() {
inquirer.prompt([
{
type: "input",
message: "enter the item id of the product you would like to add into",
name: "item"
}, {
type: "input",
message: "how many do you want to add?",
name: "quantity"
}
]).then(function(argument) {
connection.query("SELECT quantity FROM products WHERE item_id = ?", [argument.item], function(err, res) {
var numToAdd = parseInt(argument.quantity);
var newInventory = parseInt(res[0].quantity) + numToAdd;
if (err) {
return console.log(err);
}
connection.query("UPDATE products SET quantity = ? WHERE item_id = ?", [newInventory, argument.item]);
console.log(argument.quantity + " units added to inventory.");
adminMenu();
});
});
};
var adminAddItem = function() {
var depts = [
"Electronics",
"Decor",
"Fashion",
"Furniture",
"Luggage",
"Office Supplies",
"Personal Hygiene",
"Sporting Goods",
"Food",
"Toys and Games",
"Art",
"Home Goods",
"Footwear",
"Automotive",
"Seasonal",
"Tools and Hardware",
"Appliances",
"Alcohol",
"Books",
"Movies",
"Music",
"Basics"
];
inquirer.prompt([
{
type: "input",
message: "enter the name of the item you would like to add",
name: "name"
}, {
type: "list",
message: "enter the department you would to add it to",
choices: depts,
name: "department"
}, {
type: "input",
message: "enter the item price",
name: "price"
}, {
type: "input",
message: "enter the initial inventory quantity",
name: "inventory"
}
]).then(function(item) {
connection.query("INSERT INTO products SET ?", {
product_name: item.name,
dept_name: item.department,
price: item.price,
quantity: item.inventory
}, function(err, result) {
if (err) {
return console.log(err);
}
console.log("Item added to products.");
adminQuery();
});
});
};
var adminLowInventory = function() {
connection.query("SELECT * FROM products", function(err, res) {
if (err) {
return console.log(err);
}
console.log("Low Inventory Items");
console.log("----------------------");
for (var j = 0; j < res.length; j++) {
if (res[j].quantity < 5) {
console.log(res[j].product_name + " | Units remaining: " + res[j].quantity);
}
}
adminMenu();
});
};
};