Skip to content

Commit 39cdb3d

Browse files
committed
lots of good stuff again
1 parent e3f0794 commit 39cdb3d

File tree

1 file changed

+135
-13
lines changed

1 file changed

+135
-13
lines changed

server/api/db/index.js

+135-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,45 @@ let express = require('express');
22
let router = express();
33
module.exports = router;
44

5+
function mysqlEscape (str) {
6+
if (typeof str != 'string')
7+
return str;
8+
9+
return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) {
10+
switch (char) {
11+
case "\0":
12+
return "\\0";
13+
case "\x08":
14+
return "\\b";
15+
case "\x09":
16+
return "\\t";
17+
case "\x1a":
18+
return "\\z";
19+
case "\n":
20+
return "\\n";
21+
case "\r":
22+
return "\\r";
23+
case "\"":
24+
case "'":
25+
case "\\":
26+
case "%":
27+
return "\\"+char; // prepends a backslash to backslash, percent,
28+
// and double/single quotes
29+
}
30+
});
31+
}
32+
533
const $filtering = ((req) => {
634
let str = [];
735
if (req.query.limit) {
8-
str.push(`LIMIT ${req.query.limit}`)
36+
str.push(`LIMIT ${mysqlEscape(req.query.limit)}`)
37+
if (req.query.page) {
38+
let n = Number(req.query.page) * Number(req.query.limit);
39+
str.push(`OFFSET ${n}`)
40+
}
41+
}
42+
if (req.query.offset) {
43+
str.push(`OFFSET ${mysqlEscape(req.query.offset)}`)
944
}
1045
return str.join(' ');
1146
});
@@ -27,24 +62,111 @@ const $attempt = ((cb) => {
2762
})
2863
});
2964

65+
router.use((req, res, next) => {
66+
req.$param = function(name) {
67+
return db.connection.escape(req.params[name] ?? req.query[name]);
68+
};
69+
next();
70+
})
71+
72+
let normalizeQuery = function(str) {
73+
return str.split('\n').join(' ').split('\t').join('').trim()
74+
.split(' ').join(' ')
75+
.split(' ').join(' ')
76+
.split(' ').join(' ');
77+
}
78+
79+
// Get all hospitals in the database
3080
router.get('/hospitals.json', $attempt(async (req, res) => {
3181
let query = `SELECT * FROM hospitals`;
32-
return (await db.query(`${query} ${$filtering(req)}`)).results
82+
query = normalizeQuery(`${query} ${$filtering(req)}`);
83+
return {
84+
results: (await db.query(query)).results,
85+
query,
86+
}
3387
}))
3488

35-
router.get(['/hospitals/:hospital_id.json'], $attempt(async (req, res) => {
36-
let query = `SELECT * FROM hospitals WHERE hospital_id = ${req.params.hospital_id}`;
37-
return (await db.query(`${query} ${$filtering(req)}`)).results.shift()
89+
// Get specific hospital by id
90+
router.get('/hospitals/:hospital_id.json', $attempt(async (req, res) => {
91+
let query = `SELECT * FROM hospitals WHERE hospital_id = ${req.$param('hospital_id')}`;
92+
query = normalizeQuery(`${query} ${$filtering(req)}`);
93+
return {
94+
results: (await db.query(query)).results.shift(),
95+
query,
96+
}
3897
}))
3998

40-
router.get(['/hospitals/:hospital_id/departments.json'], $attempt(async (req, res) => {
41-
let query = `SELECT * FROM departments WHERE hospital_id = ${req.params.hospital_id}`;
42-
return (await db.query(`${query} ${$filtering(req)}`)).results
99+
// Get departments from a hospital
100+
router.get('/hospitals/:hospital_id/departments.json', $attempt(async (req, res) => {
101+
let query = `SELECT * FROM departments WHERE hospital_id = ${req.$param('hospital_id')}`;
102+
query = normalizeQuery(`${query} ${$filtering(req)}`);
103+
return {
104+
results: (await db.query(query)).results,
105+
query,
106+
}
43107
}))
44108

45-
router.get(['/hospitals/:hospital_id/nurses.json'], $attempt(async (req, res) => {
46-
let query = `SELECT * FROM nurses WHERE department_id IN (
47-
SELECT employee_id FROM departments WHERE hospital_id = ${req.params.hospital_id}
48-
) `;
49-
return (await db.query(`${query} ${$filtering(req)}`)).results
109+
// Get nurses from a hospital
110+
router.get('/hospitals/:hospital_id/nurses.json', $attempt(async (req, res) => {
111+
let query = `
112+
SELECT nurses.*, employees.first_name, employees.last_name FROM nurses
113+
LEFT JOIN employees ON nurses.employee_id = employees.employee_id
114+
WHERE department_id IN (
115+
SELECT department_id FROM departments WHERE hospital_id = ${req.$param('hospital_id')}
116+
)`;
117+
query = normalizeQuery(`${query} ${$filtering(req)}`);
118+
return {
119+
results: (await db.query(query)).results,
120+
query,
121+
}
122+
}))
123+
124+
// Get physicians from a hospital
125+
router.get('/hospitals/:hospital_id/physicians.json', $attempt(async (req, res) => {
126+
let query = `
127+
SELECT physicians.*, employees.first_name, employees.last_name FROM physicians
128+
LEFT JOIN employees ON physicians.employee_id = employees.employee_id
129+
WHERE department_id IN (
130+
SELECT department_id FROM departments WHERE hospital_id = ${req.$param('hospital_id')}
131+
)`;
132+
query = normalizeQuery(`${query} ${$filtering(req)}`);
133+
return {
134+
results: (await db.query(query)).results,
135+
query,
136+
}
137+
}))
138+
139+
// Get employees from a hospital
140+
router.get('/hospitals/:hospital_id/employees.json', $attempt(async (req, res) => {
141+
let query = `
142+
SELECT * FROM employees WHERE employee_id IN
143+
(
144+
SELECT employee_id FROM nurses WHERE department_id IN (
145+
SELECT department_id FROM departments WHERE hospital_id = ${req.$param('hospital_id')}
146+
)
147+
UNION
148+
SELECT employee_id FROM physicians WHERE department_id IN (
149+
SELECT department_id FROM departments WHERE hospital_id = ${req.$param('hospital_id')}
150+
)
151+
)`;
152+
query = normalizeQuery(`${query} ${$filtering(req)}`);
153+
return {
154+
results: (await db.query(query)).results,
155+
query,
156+
}
157+
}))
158+
159+
// Get specific nurse from hospital
160+
router.get('/hospitals/:hospital_id/nurses/:nurse_id.json', $attempt(async (req, res) => {
161+
let query = `
162+
SELECT nurses.*, employees.first_name, employees.last_name FROM nurses
163+
LEFT JOIN employees ON nurses.employee_id = employees.employee_id
164+
WHERE nurse_id = ${req.$param('nurse_id')} AND department_id IN (
165+
SELECT department_id FROM departments WHERE hospital_id = ${req.$param('hospital_id')}
166+
)`;
167+
query = normalizeQuery(`${query} ${$filtering(req)}`);
168+
return {
169+
results: (await db.query(query)).results,
170+
query,
171+
}
50172
}))

0 commit comments

Comments
 (0)