-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
405 lines (375 loc) · 12.6 KB
/
server.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const express = require('express');
const parse = require('csv-parse');
const sqlite3 = require('sqlite3');
const cors = require('cors');
const fs = require('fs');
const {createCountryTable, createGoldMedalTable, goldMedalNumber, bestYear, mostSummerWins, mostWinterWins, bestDiscipline, bestSport, bestEvent, numberMenMedalists, numberWomenMedalists, mostMedaledAthlete, orderedMedals, orderedSports} = require('./sql');
const db = new sqlite3.Database('./gold_medals.sqlite');
const app = express();
app.use(cors());
const lowerCaseObjectKeys = (questionableKeys) => {
// Valid SQL commands are case-insensitive, but JavaScript is case-sensitive
let lowerCaseKeys = {};
for (const prop in questionableKeys) {
if (prop.toLowerCase().indexOf('count') !== -1) {
lowerCaseKeys.count = questionableKeys[prop];
} else {
lowerCaseKeys[prop.toLowerCase()] = questionableKeys[prop];
}
}
return lowerCaseKeys;
};
const fixCountryName = countryName => {
// Fixes case for country names
return countryName.replace(/\w\S*/g, txt => {return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
app.get('/country/:countryName', (req, res, next) => {
const countryName = fixCountryName(req.params.countryName);
const goldMedalQuery = goldMedalNumber(countryName);
const summerWinsQuery = mostSummerWins(countryName);
const winterWinsQuery = mostWinterWins(countryName);
const disciplineQuery = bestDiscipline(countryName);
const sportQuery = bestSport(countryName);
const eventQuery = bestEvent(countryName);
const menMedalistsQuery = numberMenMedalists(countryName);
const womenMedalistsQuery = numberWomenMedalists(countryName);
const yearQuery = bestYear(countryName);
const mostMedalsQuery = mostMedaledAthlete(countryName);
let country = {'name': countryName};
db.serialize(() => {
db.parallelize(() => {
// Add population & gdp
db.get("SELECT * FROM Country WHERE name = $name", {$name: countryName}, (err, row) => {
if (err) {
console.log(`Error while finding country details for ${countryName}.`);
console.log(err);
}
if (row) {
country.gdp = row.gdp;
country.population = row.population;
} else {
country.gdp = '-';
country.population = '-';
}
});
// Add # of gold medals
db.get(goldMedalQuery, (err, row) => {
if (err) {
console.log(`Error while finding number of gold medals for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
country.numberMedals = lowerRow.count;
} else {
country.numberMedals = '-';
}
});
// Add # of gold medals
db.get(yearQuery, (err, row) => {
if (err) {
console.log(`Error while finding the best year for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
const yearString = `${lowerRow.year} (${lowerRow.count} awards)`;
country.bestYear = yearString;
} else {
country.bestYear = '-';
}
});
// Add summer wins
db.get(summerWinsQuery, (err, row) => {
if (err) {
console.log(`Error while finding summer wins for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
const summerString = `${lowerRow.year} (${lowerRow.count} medals)`;
country.bestSummer = summerString;
} else {
country.bestSummer = '-';
}
});
// Add winter wins
db.get(winterWinsQuery, (err, row) => {
if (err) {
console.log(`Error while finding winter wins for ${countryName}.`);
console.log(err);
}
const lowerRow = lowerCaseObjectKeys(row);
if (row) {
const winterString = `${lowerRow.year} (${lowerRow.count} medals)`;
country.bestWinter = winterString;
} else {
country.bestWinter = '-';
}
});
// Add best discipline
db.get(disciplineQuery, (err, row) => {
if (err) {
console.log(`Error while finding best discipline for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
const disciplineString = `${lowerRow.discipline} (${lowerRow.count} medals)`;
country.bestDiscipline = disciplineString;
} else {
country.bestDiscipline = '-';
}
});
// Add best sport
db.get(sportQuery, (err, row) => {
if (err) {
console.log(`Error while finding best sport for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
const sportString = `${lowerRow.sport} (${lowerRow.count} medals)`;
country.bestSport = sportString;
} else {
country.bestSport = '-';
}
});
// Add best event
db.get(eventQuery, (err, row) => {
if (err) {
console.log(`Error while finding best event for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
const eventString = `${lowerRow.event} (${lowerRow.count} medals)`;
country.bestEvent = eventString;
} else {
country.bestEvent = '-';
}
});
// Add men medalists
db.get(menMedalistsQuery, (err, row) => {
if (err) {
console.log(`Error while finding men medalists for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
country.maleMedalists = lowerRow.count;
} else {
country.maleMedalists = '-';
}
});
// Add women medalists
db.get(womenMedalistsQuery, (err, row) => {
if (err) {
console.log(`Error while finding women medalists for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
country.femaleMedalists = lowerRow.count;
} else {
country.femaleMedalists = '-';
}
});
// Add most decorated
db.get(mostMedalsQuery, (err, row) => {
if (err) {
console.log(`Error while finding women medalists for ${countryName}.`);
console.log(err);
}
if (row) {
const lowerRow = lowerCaseObjectKeys(row);
country.mostMedalsAthlete = lowerRow.name;
} else {
country.mostMedalsAthlete = '-';
}
});
});
db.get("SELECT * FROM GoldMedal LIMIT 1;", (err, row) => {
res.json(country);
});
});
});
app.get('/country', (req, res, next) => {
const sortBy = req.query.sort_by;
const isDescending = req.query.ascending === 'y';
let dbQuery = "SELECT name, gdp, population FROM Country;";
if (['name', 'gdp', 'population'].includes(sortBy) && typeof req.query.ascending !== 'undefined') {
const direction = req.query.ascending === 'y' ? 'ASC' : 'DESC';
const sortString = `ORDER BY ${sortBy} ${direction}`;
dbQuery = `SELECT name,gdp, population FROM Country ${sortString};`;
}
db.all(dbQuery, (err, rows) => {
db.serialize(() => {
rows.forEach(row => {
const escapedName = row.name.replace(/'/g, '\'\'');
const getMedalsQuery = goldMedalNumber(escapedName);
if (getMedalsQuery) {
db.get(getMedalsQuery, (err, medalRow) => {
if (err) {
console.log("Error fetching gold medals number!");
console.log(err);
} else {
lowerCaseRow = lowerCaseObjectKeys(medalRow);
row.medals = lowerCaseRow.count;
}
});
} else {
console.log("Could not fetch number of gold medals!");
}
});
db.get("SELECT name FROM Country LIMIT 1;", (err, row) => {
if (sortBy === 'medalNumber' && typeof req.query.ascending !== 'undefined') {
if (req.query.ascending === 'y') {
rows.sort((countryA, countryB) => {
return countryA.medals - countryB.medals;
});
} else {
rows.sort((countryA, countryB) => {
return countryB.medals - countryA.medals;
});
}
}
res.json({countries: rows});
});
});
});
});
app.get('/country/:countryName/medals', (req, res, next) => {
const countryName = fixCountryName(req.params.countryName);
const sortBy = req.query.sort_by;
const isAscending = req.query.ascending === 'y';
const orderedMedalQuery = orderedMedals(countryName, sortBy, isAscending);
db.all(orderedMedalQuery, (err, rows) => {
if (rows) {
res.json({'medals': rows});
} else {
res.json();
}
});
});
const transformSport = sport => {
const loweredSport = lowerCaseObjectKeys(sport);
return {
'sportName': loweredSport.sport,
'numberMedals': loweredSport.count,
'percentageWins': loweredSport.percent,
};
};
app.get('/country/:countryName/sports', (req, res, next) => {
const countryName = fixCountryName(req.params.countryName);
const sortBy = req.query.sort_by;
const isAscending = req.query.ascending === 'y';
const sportsQuery = orderedSports(countryName, sortBy, isAscending);
if (sportsQuery) {
db.all(sportsQuery, (err, rows) => {
if (err) {
console.log("Error while fetching the sports!");
console.log(err);
res.send();
} else {
rows.forEach((row, index, allRows) => {
allRows[index] = transformSport(row);
});
res.json({sports: rows});
}
});
} else {
res.send();
}
});
app.listen(3001, () => {
let errorThrown = false;
db.serialize(() => {
// Drop the tables if they exist
db.run("DROP TABLE IF EXISTS Country;");
db.run("DROP TABLE IF EXISTS GoldMedal;");
const createCountryQuery = createCountryTable();
const createGoldMedalQuery = createGoldMedalTable();
// Create the country table
if (createCountryQuery) {
db.run(createCountryQuery, err => {
if (err) {
console.log("Error while creating the Country table!");
console.log(err);
return;
}
});
}
// Create the GoldMedal table
if (createGoldMedalQuery) {
db.run(createGoldMedalQuery, err => {
if (err) {
console.log("Error while creating the GoldMedal table!");
console.log(err);
return;
}
});
}
// Add the Country data
db.run("DELETE FROM Country;", (err) => {
if (err) {
console.log("Country table not found!");
console.log(err);
return;
} else {
fs.createReadStream('data/country.csv')
.pipe(parse({from: 2}))
.on('data', function(csvrow) {
db.run("INSERT INTO Country (name, code, population, gdp) VALUES ($name, $code, $population, $gdp)", {
$name: csvrow[0],
$code: csvrow[1],
$population: csvrow[2],
$gdp: csvrow[3]
}, (err) => {
if (err) {
console.log("Error while inserting Country data");
console.log(err);
return;
}
});
});
}
});
// Add the GoldMedal data
db.run("DELETE FROM GoldMedal;", (err) => {
if (err) {
console.log("GoldMedal table not found!");
console.log(err);
return;
} else {
let idCounter = 1;
fs.createReadStream('data/goldmedal.csv')
.pipe(parse({from: 2}))
.on('data', function(csvrow) {
db.run("INSERT INTO GoldMedal (id, year, city, sport, discipline, name, country, gender, event, season) VALUES ($id, $year, $city, $sport, $discipline, $name, $country, $gender, $event, $season)", {
$id: idCounter,
$year: csvrow[0],
$city: csvrow[1],
$sport: csvrow[2],
$discipline: csvrow[3],
$name: csvrow[4],
$country: csvrow[5],
$gender: csvrow[6],
$event: csvrow[7],
$season: csvrow[8]
}, (err) => {
if (err) {
console.log("Error while inserting GoldMedal data");
console.log(err);
return;
}
});
idCounter += 1;
});
}
});
db.get("SELECT name FROM GoldMedal LIMIT 1;", (err, row) => {
console.log("Data successfully loaded");
});
});
});