-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
143 lines (127 loc) · 4.37 KB
/
bamazonManager.js
File metadata and controls
143 lines (127 loc) · 4.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
//add dotenv package for hiding private data
require("dotenv").config();
const keys = require("./keys.js");
const inquirer = require("inquirer");
var mysql = require('mysql');
//hiding private data
var connection = mysql.createConnection(keys.data);
// Creates the connection with the server and loads the product data upon a successful connection
connection.connect(function (err) {
if (err) {
console.error("error connecting: " + err.stack);
}
console.log("-----------------------------------------------------------");
console.log("| WELCOME TO HARRY POTTER'S BAMAZON STORE MANAGER'S VIEW! |");
console.log("-----------------------------------------------------------");
whatDoYouWantToDo();
});
//main menu
function whatDoYouWantToDo() {
inquirer
.prompt([{
type: "list",
message: "what do you want to do?",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product", "quit"],
name: "what_to_do"
}
])
.then(function (resp) {
switch (resp.what_to_do) {
case "View Products for Sale":
loadProducts();
break;
case "View Low Inventory":
viewLowInventory();
break;
case "Add to Inventory":
promptAddInventory();
break;
case "Add New Product":
promtAddItem();
break;
case "quit":
console.log('Thank you! Have a nice day!');
connection.end();
break;
default:
loadProducts();
break;
}
});
}
//load a table with products
function loadProducts() {
// Selects all of the data from the MySQL products table
connection.query("SELECT * FROM products", function (err, res, fields) {
// Draw the table in the terminal using the response
console.table(res);
whatDoYouWantToDo();
});
};
//load a teble with products with quantity less than 5
function viewLowInventory() {
connection.query("SELECT * FROM products WHERE stock_quantity < 5", function (err, res, fields) {
console.table(res);
whatDoYouWantToDo();
})
};
//manager can add products to database
function addInventory(count, iid) {
connection.query("UPDATE products SET stock_quantity = stock_quantity + ? WHERE item_id = ?", [count, iid], function (err, res, field) {
console.log("Success!");
whatDoYouWantToDo();
})
};
//promt user what product and how many user wants to add
function promptAddInventory() {
inquirer
.prompt([
{
type: "input",
message: "Please write ID number of item",
name: "itemId"
},
{
type: "input",
message: "How many items do you want to add?",
name: "count"
}
]).then(answers => {
addInventory(answers.count, answers.itemId);
})
}
//promt user what's new product's name, quantity, price amd department
function promtAddItem(){
inquirer
.prompt([
{
type: "input",
message: "What's name of the product?",
name: "itemName"
},
{
type: "input",
message: "Please write ID of department that will control this item?",
name: "departmentName"
},
{
type: "input",
message: "What's the quantity?",
name: "quantity"
},
{
type: "input",
message: "How much is this product?",
name: "price"
}
]).then(answers => {
addItem(answers.itemName, answers.departmentName, answers.quantity, answers.price);
})
};
//updating product in database
function addItem(name, department, quantity, price){
connection.query("INSERT INTO products(product_name, department_id, price, stock_quantity) VALUES (?, ?, ?, ?)", [name, department, price, quantity], function(err, res, field){
console.log("Success!");
whatDoYouWantToDo();
})
}