/
search.js
164 lines (152 loc) · 4.17 KB
/
search.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Author: Suki Sahota
* Description: Application for Musical Searching
*/
const mysql = require(`mysql`);
const inquirer = require(`inquirer`);
const keys = require(`./keys`);
const connection = mysql.createConnection({
host: keys.connectionInfo.host,
port: keys.connectionInfo.port,
user: keys.connectionInfo.user,
password: keys.connectionInfo.password,
database: keys.connectionInfo.database
});
connection.connect(function(err) {
//if(err) throw err;
runSearch();
});
function runSearch() {
inquirer.prompt({
message: `Hello, what would you like to do today? `,
type: `list`,
name: `decision`,
choices: [
`Search for a specific artist`,
`Find artists who are listed more than once`,
`Find data within a range`,
`Search for a specific song`,
`Exit`
]
}).then(function(answer) {
switch (answer.decision) {
case `Search for a specific artist`:
artistSearch();
break;
case `Find artists who are listed more than once`:
multiSearch();
break;
case `Find data within a range`:
rangeSearch();
break;
case `Search for a specific song`:
songSearch();
break;
case `Exit`:
connection.end();
break;
}
});
}
// Function to search for a specific artist
function artistSearch() {
inquirer.prompt({
message: `Please type in the name of an artist that you would like to search for in our database`,
type: `input`,
name: `artist`
}).then(function(answer) {
connection.query(`SELECT * FROM songs WHERE ?`,
{
artist_name: answer.artist
},
function(err, res) {
//if (err) throw err;
for (let i = 0; i < res.length; i++) {
console.log(`Position: ${ res[i].id } || Song: ${ res[i].song_title } || Year: ${ res[i].release_year }`);
}
// Start process over
runSearch();
});
});
}
// Function to search for artist who appears on list more than once
function multiSearch() {
// `SELECT COUNT(*), artist_name FROM songs GROUP BY artist_name HAVING count(*) > 1`
connection.query(`SELECT artist_name FROM songs GROUP BY artist_name HAVING count(*) > 1`,
function(err, res) {
//if (err) throw err;
for (let i = 0; i < res.length; i++) {
console.log(res[i].artist_name);
}
// Start process over
runSearch();
});
}
// Function to find song data within a specified range
function rangeSearch() {
inquirer.prompt([
{
message: `Please enter a lower bound. `,
type: `input`,
name: `lower`,
validate: function(value) {
if (isNaN(value) === false) {
return true;
}
console.log(`You did not enter a valid number!`);
return false;
}
},
{
message: `Please enter an upper bound. `,
type: `input`,
name: `upper`,
validate: function(value) {
if (isNaN(value) === false) {
return true;
}
console.log(`You did not enter a valid number!`);
return false;
}
}
]).then(function(answer) {
connection.query(`SELECT * FROM songs WHERE id BETWEEN ? AND ?`,
[
answer.lower,
answer.upper
],
function(err, res) {
//if (err) throw err;
for (let i = 0; i < res.length; i++) {
console.log(`Position: ${ res[i].id }
|| Song: ${ res[i].song_title }
|| Artist: ${ res[i].artist_name }
|| Year: ${ res[i].release_year }`);
}
// Start process over
runSearch();
});
})
}
// Function to search for a specific song
function songSearch() {
inquirer.prompt({
message: `Type any song you wish... I might have it in my database `,
type: `input`,
name: `title`
}).then(function(answer) {
connection.query(`SELECT * FROM songs WHERE ?`,
{
song_title: answer.title
},
function(err, res) {
//if (err) throw err;
console.log(`Position: ${ res[0].id }
|| Song: ${ res[0].song_title }
|| Artist: ${ res[0].artist_name }
|| Year: ${ res[0].release_year }`);
// Start process over
runSearch();
});
});
}