Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require("mysql");
- var inquirer = require("inquirer");
- var connection = mysql.createConnection({
- host: "localhost",
- port: 3306,
- user: "root",
- password: "",
- database: "bamazon"
- });
- function start(){
- connection.connect(function(err){
- if (err) throw err;
- console.log("Connected as id: " + connection.threadId);
- console.log("Welcome to Bamazon!");
- console.log("Here is our catalog.");
- connection.query("SELECT * FROM products", function(err, res){
- if (err) throw err;
- for (var i = 0; i < res.length; i++) {
- console.log("\n==================================\n");
- console.log("Product ID: " + res[i].item_id + "\nProduct: " + res[i].product_name + "\nDepartment: " + res[i].department_name + "\nPrice Per Unit: " + res[i].price + "\nUnits In Stock: " + res[i].stock_quantity);
- };
- console.log("\n==================================\n");
- purchases();
- });
- });
- };
- function purchases(){
- inquirer.prompt([
- {
- type: "input",
- name: "productId",
- message: "Enter the Product ID for the item you want to buy.",
- validate: function(value) {
- if (value && isNaN(value) === false) {
- return true;
- }
- return false;
- }
- },
- {
- type: "input",
- name: "qty",
- message: "How many of this product would you like to buy? Refer to the catalog for stock quantities.",
- validate: function(value) {
- if (value && isNaN(value) === false) {
- return true;
- }
- return false;
- }
- }
- ]).then(function(order){
- if (order.qty > res[order.productId].stock_quantity) {
- console.log("We're terribly sorry!");
- console.log("\nWe do not have " + order.qty + "units of " + res[order.productId].product_name + " in stock.");
- console.log("\nThere are only " + res[order.productId].stock_quantity + " units in stock.");
- inquirer.prompt([
- {
- type: "list",
- name: "retry",
- message: "What would you like to do?",
- choices: ["Retry purchasing product", "Display store catalog again", "Exit"]
- }
- ]).then(function(choice){
- if (choice == "Retry purchasing product") {
- purchases();
- }
- else if (choice == "Display store catalog again") {
- start();
- }
- else if (choice == "Exit") {
- console.log("Thank you for using Bamazon!");
- console.log("\nWe hope to see you again.");
- process.exit(-1);
- }
- })
- }
- // else {
- // }
- })
- };
- start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement