-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (102 loc) · 2.95 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
const alasql = require("alasql");
const express = require("express");
const app = express();
const DATA = require("./DATA.json");
let PORT = process.env.PORT || 3000;
// Home route
app.get("/", (req, res) => {
res.send(
'<h1>This is an API without a DATABASE, Please see <a href= "https://www.github.com/code-reaper08/NAPI">here</a> for more deatails of the routes to request</h1>'
);
});
// get data with name
app.get("/name/:name", (req, res) => {
try {
DATA.laureates.forEach((laureate) => {
const fname = laureate.firstname;
const sname = laureate.surname;
const fullname = fname.concat(sname).toUpperCase();
const usrqryname = req.params.name.toUpperCase();
if (usrqryname == fullname) {
res.status(200).send(laureate);
}
});
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// get all data based on born country code
app.get("/bornat/:CC", (req, res) => {
try {
const CCqry = req.params.CC;
var values = Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE bornCountryCode = ? ", [
values,
CCqry,
]);
res.status(200).json(result);
} catch (err) {
res.status(404).json({ message: err.message });
}
});
// get all data based on died at country code
app.get("/diedat/:DCC", (req, res) => {
try {
const DCCqry = req.params.DCC;
var values = Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE diedCountryCode = ? ", [
values,
DCCqry,
]);
res.status(200).json(result);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// get all data based on Gender
app.get("/gender/:GEN", (req, res) => {
try {
GENqry = req.params.GEN;
values = Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE gender = ? ", [
values,
GENqry,
]);
res.status(200).json(result);
} catch (err) {
res.status(404).json({ message: err.message });
}
});
// get all data based on gender and Country
app.get("/gender/:GEN/:CC", (req, res) => {
try {
GENqry = req.params.GEN;
CCqry = req.params.CC;
values = Object.values(DATA.laureates);
const result = alasql(
"SELECT * FROM ? WHERE gender = ? AND bornCountryCode = ?",
[values, GENqry, CCqry]
);
res.status(200).json(result);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
// get all data based on born cc and died cc
app.get("/bornat/:BCC/diedat/:DCC", (req, res) => {
try {
BCCqry = req.params.BCC;
DCCqry = req.params.DCC;
values = Object.values(DATA.laureates);
const result = alasql(
"SELECT * FROM ? WHERE bornCountryCode = ? AND diedCountryCode = ?",
[values, BCCqry, DCCqry]
);
res.status(200).json(result);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
app.listen(PORT, () => {
console.log(`server running on http://localhost:${PORT}`);
});