-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (188 loc) · 6.76 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const express = require('express');
const hbs = require('express-handlebars');
const app = express();
const path = require('path');
const PORT = process.env.PORT || 5000;
/* Database Connection [heroku config update in .env for local running] */
const {Pool} = require('pg');
const pool = new Pool({
connectionString : process.env.configS,
ssl: true
});
/* Body parser for post requests */
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
/* Set app engine to handlebars */
app.use(express.static(path.join(__dirname, '/public')));
app.set('views', path.join(__dirname, 'views'));
app.engine('hbs', hbs({
extname: 'hbs',
defaultLayout: 'main',
layoutsDir: __dirname + '/views/pages/',
partialsDir: __dirname + '/views/partials/'
}));
app.set('view engine', 'hbs');
/* Home Page */
app.get('/', function (req, res) {
res.render('./pages/home');
});
app.get('/Color-Scheme', function (req, res) {
});
/* Labels */
app.get('/Labels', async (req, res) => {
let resultC = null;
try {
const clientTwo = await pool.connect();
resultC = await clientTwo.query('SELECT c.country_id AS id, c.country_name AS country \
FROM countries c \
ORDER BY country_name ASC;');
clientTwo.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
try {
const client = await pool.connect();
const result = await client.query('SELECT c.country_id AS id, label_name AS name, country_name AS country, label_bc AS bandcamp, \
label_sc AS soundcloud, label_fb AS facebook, label_id AS labelid, avatar AS avatar\
FROM labels \
INNER JOIN countries c ON country_id = label_country \
ORDER BY label_name ASC;');
const results = {'label': (result) ? result.rows : null, 'countryL': (resultC) ? resultC.rows : null};
res.render('./pages/labels', results);
client.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
});
/* Labels Search */
app.post('/Label-Search', async (req, res) => {
try {
const client = await pool.connect();
var query = "SELECT c.country_id AS id, label_name AS name, country_name AS country, label_bc AS bandcamp, \
label_sc AS soundcloud, label_fb AS facebook, avatar AS avatar\
FROM labels \
INNER JOIN countries c ON country_id = label_country \
WHERE label_name ILIKE '" + req.body.searchTerm + "%'\
ORDER BY label_name ASC";
const result = await client.query(query);
let results = {'label': (result) ? result.rows : null};
res.render('./partials/label-search', results, (err, html) => {
if (err)
return console.error(err);
res.send(html);
});
client.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
});
/* Labels Filter By Country */
app.post('/Label-Country', async (req, res) => {
try {
const client = await pool.connect();
var query = 'SELECT c.country_id AS id, label_name AS name, country_name AS country, label_bc AS bandcamp, \
label_sc AS soundcloud, label_fb AS facebook, avatar AS avatar\
FROM labels \
INNER JOIN countries c ON country_id = label_country \
WHERE country_id = ' + req.body.searchTerm + ' \
ORDER BY label_name ASC';
const result = await client.query(query);
let results = {'label': (result) ? result.rows : null};
res.render('./partials/label-search', results, (err, html) => {
if (err)
return console.error(err);
res.send(html);
});
client.release()
} catch
(err) {
console.error(err);
res.send("Error " + err)
}
});
/* Podcasts */
app.get('/Podcasts', async (req, res) => {
// Making these two variables global for the async promises
result = null;
resultC = null;
try {
const clientTwo = await pool.connect()
resultC = await clientTwo.query('SELECT c.country_id AS id, c.country_name AS country \
FROM countries c \
ORDER BY country_name ASC');
clientTwo.release();
} catch (err) {
console.error(err);
res.send("Error " + err)
}
try {
const client = await pool.connect();
const result = await client.query('SELECT podcast_name AS name, country_name AS country, podcast_sc AS soundcloud, \
podcast_fb AS facebook, avatar AS avatar\
FROM podcasts \
INNER JOIN countries ON country_id = podcast_country \
ORDER BY podcast_name ASC');
results = {'podcast': (result) ? result.rows : null, 'countryL': (resultC) ? resultC.rows : null};
res.render('./pages/podcasts', results);
client.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
});
/* Podcasts Search */
app.post('/Podcast-Search', async (req, res) => {
try {
const client = await pool.connect();
var query = "SELECT podcast_name AS name, country_name AS country, podcast_sc AS soundcloud, \
podcast_fb AS facebook, avatar AS avatar\
FROM podcasts \
INNER JOIN countries ON country_id = podcast_country \
WHERE podcast_name ILIKE '" + req.body.searchTerm + "%'\
ORDER BY podcast_name ASC";
const result = await client.query(query);
let results = {'podcast': (result) ? result.rows : null};
res.render('./partials/podcast-search', results, (err, html) => {
if (err)
return console.error(err);
res.send(html);
});
client.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
});
/* Labels Filter By Country */
app.post('/Podcast-Country', async (req, res) => {
try {
const client = await pool.connect();
var query = 'SELECT podcast_name AS name, country_name AS country, podcast_sc AS soundcloud, \
podcast_fb AS facebook, avatar AS avatar\
FROM podcasts \
INNER JOIN countries ON country_id = podcast_country \
WHERE country_id = ' + req.body.searchTerm + ' \
ORDER BY podcast_name ASC';
const result = await client.query(query);
let results = {'podcast': (result) ? result.rows : null};
res.render('./partials/podcast-search', results, (err, html) => {
if (err)
return console.error(err);
res.send(html);
});
client.release()
} catch (err) {
console.error(err);
res.send("Error " + err)
}
});
// app.get('/About', function (req, res) {
// res.render('./pages/about')
// });
app.listen(PORT, () => console.log(`Listening on ${PORT}`));