-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbamazonManager.js
More file actions
146 lines (130 loc) · 3.37 KB
/
bamazonManager.js
File metadata and controls
146 lines (130 loc) · 3.37 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table");
var connection = mysql.createConnection({
host:"localhost",
port:3306,
user:"root",
password:"",
database:"bamazon"
});
connection.connect(function(err){
if(err)throw err;
console.log("connected as id" + connection.threadId);
});
function displayInventory(){
connection.query('SELECT * FROM Products', function(err, res){
if(err){console.log(err)};
var theDisplayTable = new Table({
head: ['Item ID', 'Product Name', 'Category', 'Price', 'Quantity'],
colWidths: [10,25,25,10,14]
});
for(i=0; i<res.length;i++){
theDisplayTable.push(
[res[i].item_id,res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity]
);
}
console.log(theDisplayTable.toString());
inquirerForUpdates();
});
};
function inquirerForUpdates(){
inquirer.prompt([{
name:"action",
type: "list",
message: "Choose an option below to manage current inventory:",
choices: ["Restock Inventory", "Add New Product", "Remove An Existing Product"]
}]).then(function(answers){
switch(answers.action){
case 'Restock Inventory':
restockRequest();
break;
case 'Add New Product':
addRequest();
break;
case 'Remove An Existing Product':
removeRequest();
break;
}
});
};
function restockRequest(){
inquirer.prompt([
{
name:"ID",
type:"input",
message:"What is the item number of the item you would like to restock?"
},
{
name:"Quantity",
type:"input",
message:"What is the quantity you would like to add?"
},
]).then(function(answers){
var quantityAdded = answers.Quantity;
var IDOfProduct = answers.ID;
restockInventory(IDOfProduct, quantityAdded);
});
};
function restockInventory(id, quant){
connection.query('SELECT * FROM Products WHERE item_id = '+id, function(err,res){
if(err){console.log(err)};
connection.query('UPDATE Products SET stock_quantity = stock_quantity + ' +stock_quantity+ 'WHERE item_id =' +item_id);
displayInventory();
});
};
function addRequest(){
inquirer.prompt([
{
name: "ID",
type: "input",
message: "Add ID Number"
},
{
name: "Name",
type: "input",
message: "What is name of product you would like to stock?"
},
{
name:"Category",
type:"input",
message:"What is the category for product?"
},
{
name:"Price",
type:"input",
message:"What is the price for item?"
},
{
name:"Quantity",
type:"input",
message:"What is the quantity you would like to add?"
},
]).then(function(answers){
var id = answers.Id;
var name = answers.Name;
var category = answers.Category;
var price = answers.Price;
var quantity = answers.Quantity;
buildNewItem(id,name,category,price,quantity);
});
};
function buildNewItem(name,category,price,quantity){
connection.query('INSERT INTO products (item_id,product_name,department_name,price,stock_quantity) VALUES("' + id + '","' + name + '","' + category + '",' + price + ',' + quantity + ')');
displayInventory();
};
function removeRequest(){
inquirer.prompt([{
name:"ID",
type:"input",
message:"What is the item number of the item you would like to remove?"
}]).then(function(answer){
var id = answers.ID;
removeInventory(id);
});
};
function removeInventory(id){
connection.query('DELETE FROM Products WHERE item_id = ' + id);
displayInventory();
};
displayInventory();