-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.js
64 lines (55 loc) · 1.57 KB
/
queries.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
const Pool = require('pg').Pool;
const dotenv = require('dotenv');
dotenv.config();
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
});
const getAllHeadings = (request, response) => {
pool.query('SELECT * FROM headings', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
};
const postHeadings = (request, response) => {
const { patientId, heading, data } = request.body;
const date = new Date().toISOString().slice(0, 10);
const json = JSON.stringify(data);
pool.query('INSERT INTO headings (patientId, heading, data, created_at) VALUES ($1, $2, $3, $4)',
[patientId, heading, json, date],
(error) => {
if (error) {
throw error
}
response.status(201).send(`User added new ${heading} heading`)
})
};
const getHeadingByPatientId = (request, response) => {
const patientId = request.params.patientId;
pool.query('SELECT * FROM headings WHERE patientId = $1', [patientId], (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
};
const headingSearch = (request, response) => {
const search = request.params.search;
pool.query(`SELECT * FROM headings WHERE heading LIKE '${search}%'`, (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
};
module.exports = {
getAllHeadings,
postHeadings,
getHeadingByPatientId,
headingSearch
};