Advertisement
Guest User

4---j@s0n

a guest
Oct 17th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var inquirer = require('inquirer');
  2. var mysql = require('mysql');
  3.  
  4. // Define the MySQL connection parameters
  5. var connection = mysql.createConnection({
  6.     host: 'localhost',
  7.     port: 3306,
  8.  
  9.     // Your username
  10.     user: 'root',
  11.  
  12.     // Your password
  13.     password: 'root',
  14.     database: 'top5000'
  15. });
  16.  
  17. // Ask the user to make a selection
  18. function promptUserInput() {
  19.     inquirer.prompt([{
  20.         type: 'list',
  21.         name: 'filter',
  22.         message: 'What kind of search would you like to do?',
  23.         choices: ['By Artist', 'By Song Count', 'By Year Range', 'By Song Name', 'Song and Album']
  24.     }]).then(function (input) {
  25.         console.log('User selected ' + input.filter);
  26.  
  27.         // Perform the appropriate query
  28.         if (input.filter === 'By Artist') {
  29.  
  30.             queryByArtist();
  31.  
  32.         } else if (input.filter === 'By Song Count') {
  33.  
  34.             queryBySongCount();
  35.  
  36.         } else if (input.filter === 'By Year Range') {
  37.  
  38.             queryByYearRange();
  39.  
  40.         } else if (input.filter === 'By Song Name') {
  41.  
  42.             queryBySongName();
  43.  
  44.         } else if (input.filter === 'Song and Album')
  45.             queryBySongAlbum();
  46.     })
  47. }
  48.  
  49. function queryByArtist() {
  50.     // console.log('___queryByArtist___');
  51.  
  52.     inquirer.prompt([{
  53.         type: 'input',
  54.         name: 'artist',
  55.         message: 'Please enter the artist of your choice.'
  56.     }]).then(function (input) {
  57.         queryStr = 'SELECT * FROM topsongs WHERE ?';
  58.         connection.query(queryStr, {
  59.             artist: input.artist
  60.         }, function (err, data) {
  61.             if (err) throw err;
  62.  
  63.             console.log('Songs Returned: ');
  64.             console.log('..................\n');
  65.  
  66.             for (var i = 0; i < data.length; i++) {
  67.                 console.log([
  68.                     data[i].ranked,
  69.                     data[i].artist,
  70.                     data[i].song,
  71.                     data[i].year
  72.                 ].join(" | "));
  73.             }
  74.  
  75.             console.log("\n---------------------------------------------------------------------\n");
  76.             promptUserInput();
  77.         })
  78.     })
  79. }
  80.  
  81. function queryBySongCount() {
  82.     // console.log('___queryBySongCount___');
  83.  
  84.     inquirer.prompt([{
  85.         type: 'input',
  86.         name: 'count',
  87.         message: 'Please enter the minumum count value for artist appearance.'
  88.     }]).then(function (input) {
  89.         queryStr = 'SELECT artist FROM topsongs GROUP BY artist HAVING COUNT(*) > ' + input.count;
  90.         connection.query(queryStr, function (err, data) {
  91.             if (err) throw err;
  92.  
  93.             console.log('Artists Returned: ');
  94.             console.log('....................\n');
  95.  
  96.             for (var i = 0; i < data.length; i++) {
  97.                 console.log([
  98.                     data[i].artist
  99.                 ].join(" | "));
  100.             }
  101.  
  102.             console.log("\n---------------------------------------------------------------------\n");
  103.             promptUserInput();
  104.         })
  105.     })
  106. }
  107.  
  108. function queryByYearRange() {
  109.     // console.log('___queryByYearRange___');
  110.  
  111.     inquirer.prompt([{
  112.             type: 'input',
  113.             name: 'begin',
  114.             message: 'Please enter the start year.'
  115.         },
  116.         {
  117.             type: 'input',
  118.             name: 'end',
  119.             message: 'Please enter the end year.'
  120.         }
  121.     ]).then(function (input) {
  122.         queryStr = 'SELECT * FROM topsongs WHERE year BETWEEN ? AND ?';
  123.         connection.query(queryStr, [input.begin, input.end], function (err, data) {
  124.             if (err) throw err;
  125.  
  126.             console.log('Songs Returned: ');
  127.             console.log('..................\n');
  128.  
  129.             for (var i = 0; i < data.length; i++) {
  130.                 console.log([
  131.                     data[i].ranked,
  132.                     data[i].artist,
  133.                     data[i].song,
  134.                     data[i].year
  135.                 ].join(" | "));
  136.             }
  137.  
  138.             console.log("\n---------------------------------------------------------------------\n");
  139.             promptUserInput();
  140.         })
  141.     })
  142. }
  143.  
  144. function queryBySongName() {
  145.     // console.log('___queryBySongName___');
  146.  
  147.     inquirer.prompt([{
  148.         type: 'input',
  149.         name: 'song',
  150.         message: 'Please enter the name of the song.'
  151.     }]).then(function (input) {
  152.         queryStr = 'SELECT * FROM topsongs WHERE ?';
  153.         connection.query(queryStr, {
  154.             song: input.song
  155.         }, function (err, data) {
  156.             if (err) throw err;
  157.  
  158.             console.log('Songs Information: ');
  159.             console.log('.....................\n');
  160.  
  161.             for (var i = 0; i < data.length; i++) {
  162.                 console.log([
  163.                     data[i].ranked,
  164.                     data[i].artist,
  165.                     data[i].song,
  166.                     data[i].year
  167.                 ].join(" | "));
  168.             }
  169.  
  170.             console.log("\n---------------------------------------------------------------------\n");
  171.             promptUserInput();
  172.         })
  173.     })
  174. }
  175.  
  176. function queryBySongAlbum() {
  177.     inquirer.prompt([{
  178.         type: 'input',
  179.         name: 'songArtist',
  180.         message: 'Please enter the name of the Artist.'
  181.     }]).then(function (input) {
  182.     queryStr = 'SELECT * FROM topsongs WHERE year WHERE ?';
  183.    
  184.     connection.query()
  185.     })
  186. }
  187.    
  188.     promptUserInput();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement