bamazonCustomer.js
// Create start function to start app
Create start function to start app
Create start function to start app
function start() {
  const queryString = "SELECT * FROM products";
  connection.query(queryString, function(err, products) {
    if (err) throw err;
    console.log(`\nITEMS FOR SALE`)
    products.forEach(product => console.log(`${product.product_name} || ID: ${product.item_id} || Price: $${product.price}`));
    console.log(`\n`);
    inquirer
      .prompt([
        {
          type: "input",
          message: "Which product would you like to buy? Please enter ID number",
          name: "choice",
          validate: function(value) {
            if (!isNaN(value)) {
              return true;
            };
            return false;
          }
        },
        {
          type: "input",
          message: "How many would you like to buy?",
          name: "amount",
          validate: function(value) {
            if (!isNaN(value)) {
              return true;
            }
            return false;
          }
        },
        {
          type: "confirm",
          message: "Would you like to place the order?",
          name: "confirm",
          default: true
        }
      ]).then(function(res) {
        if (!res.confirm) {
          start();
        };
        const chosenProduct = products.find(product => product.item_id === parseInt(res.choice))
        if (chosenProduct.stock_quantity >= parseInt(res.amount)) {
          const queryString = "UPDATE products SET ? WHERE?";
          connection.query(queryString,
            [
              {
                stock_quantity: (chosenProduct.stock_quantity - parseInt(res.amount))
              },
              {
                product_name: chosenProduct.product_name
              }
            ],
            function(err) {
              if (err) throw err;
              const total = chosenProduct.price * res.amount;
              console.log(`Your total is: $${total}`)
            }
          );
        }
        else {
          console.log(`Insufficient Quantity!`);
          start();
        };
      });
  });
}
function start ( ) { const queryString = "SELECT * FROM products" ; connection . query ( queryString , function ( err , products ) { if ( err ) throw err ; console . log ( ` \n ` ) products . forEach ( product => console . log ( ` ${ product . product_name } ${ product . item_id } ${ product . price } ` ) ) ; console . log ( ` \n ` ) ; inquirer . prompt ( [ { type : "input" , message : "Which product would you like to buy? Please enter ID number" , name : "choice" , validate : function ( value ) { if ( ! isNaN ( value ) ) { return true ; } ; return false ; } } , { type : "input" , message : "How many would you like to buy?" , name : "amount" , validate : function ( value ) { if ( ! isNaN ( value ) ) { return true ; } return false ; } } , { type : "confirm" , message : "Would you like to place the order?" , name : "confirm" , default : true } ] ) . then ( function ( res ) { if ( ! res . confirm ) { start ( ) ; } ; const chosenProduct = products . find ( product => product . item_id === parseInt ( res . choice ) ) if ( chosenProduct . stock_quantity >= parseInt ( res . amount ) ) { const queryString = "UPDATE products SET ? WHERE?" ; connection . query ( queryString , [ { stock_quantity : ( chosenProduct . stock_quantity - parseInt ( res . amount ) ) } , { product_name : chosenProduct . product_name } ] , function ( err ) { if ( err ) throw err ; const total = chosenProduct . price * res . amount ; console . log ( ` ${ total } ` ) } ) ; } else { console . log ( ` ` ) ; start ( ) ; } ; } ) ; } ) ; }
Create start function to start app
Create start function to start app
// Console.log all of the items available for sale // Include ids, names, and prices // Create inquirer prompt // Ask user which product they would like to buy // Ask user how many units they would like to buy // Confirm order // If user doesn't confirm, start over // Use array .find() method to find product user wants to purchase // if amount desired < store_quantity, run an UPDATE query to update the stock_quantity value for that product