Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1.  
  2. var mysql = require("mysql");
  3. var inquirer = require("inquirer");
  4.  
  5. var connection = mysql.createConnection({
  6. host: "localhost",
  7. port: 3306,
  8. user: "root",
  9. password: "",
  10. database: "bamazon"
  11. });
  12.  
  13.  
  14. function start(){
  15. connection.connect(function(err){
  16. if (err) throw err;
  17. console.log("Connected as id: " + connection.threadId);
  18.  
  19. console.log("Welcome to Bamazon!");
  20. console.log("Here is our catalog.");
  21.  
  22. connection.query("SELECT * FROM products", function(err, res){
  23. if (err) throw err;
  24.  
  25. for (var i = 0; i < res.length; i++) {
  26. console.log("\n==================================\n");
  27. 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);
  28. };
  29. console.log("\n==================================\n");
  30. purchases();
  31. });
  32. });
  33. };
  34.  
  35.  
  36. function purchases(){
  37. inquirer.prompt([
  38. {
  39. type: "input",
  40. name: "productId",
  41. message: "Enter the Product ID for the item you want to buy.",
  42. validate: function(value) {
  43. if (value && isNaN(value) === false) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. },
  49. {
  50. type: "input",
  51. name: "qty",
  52. message: "How many of this product would you like to buy? Refer to the catalog for stock quantities.",
  53. validate: function(value) {
  54. if (value && isNaN(value) === false) {
  55. return true;
  56. }
  57. return false;
  58. }
  59.  
  60. }
  61. ]).then(function(order){
  62. if (order.qty > res[order.productId].stock_quantity) {
  63. console.log("We're terribly sorry!");
  64. console.log("\nWe do not have " + order.qty + "units of " + res[order.productId].product_name + " in stock.");
  65. console.log("\nThere are only " + res[order.productId].stock_quantity + " units in stock.");
  66. inquirer.prompt([
  67. {
  68. type: "list",
  69. name: "retry",
  70. message: "What would you like to do?",
  71. choices: ["Retry purchasing product", "Display store catalog again", "Exit"]
  72. }
  73. ]).then(function(choice){
  74. if (choice == "Retry purchasing product") {
  75. purchases();
  76. }
  77. else if (choice == "Display store catalog again") {
  78. start();
  79. }
  80. else if (choice == "Exit") {
  81. console.log("Thank you for using Bamazon!");
  82. console.log("\nWe hope to see you again.");
  83. process.exit(-1);
  84. }
  85. })
  86. }
  87. // else {
  88.  
  89. // }
  90. })
  91. };
  92.  
  93.  
  94.  
  95.  
  96.  
  97. start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement