-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (84 loc) · 2.57 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
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const PORT = process.env.PORT || 5000
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors())
const getRecords = (request, response) => {
pool.query('SELECT * FROM records', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
const getGlucoseLastRecord = (request, response) => {
pool.query('SELECT * FROM records ORDER BY timeOfMeasure DESC LIMIT 1', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows[0])
})
}
const addRecord = (request, response) => {
const {id, timeOfMeasure, glucoseValue} = request.body
console.log(request.body)
pool.query(
'INSERT INTO records (id, timeofmeasure, glucosevalue) VALUES ($1, $2, $3)',
[id, timeOfMeasure, Math.round(100000*glucoseValue)],
(error) => {
if (error) {
throw error
}
response.status(201).json({status: 'success', message: 'Record added.'})
},
)
}
const getPulseOximeterLastRecord = (request, response) => {
pool.query('SELECT * FROM pulsioximeterRecords ORDER BY timeofmeasurelower DESC LIMIT 1', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows[0])
})
}
const addPulseOximeterRecord = (request, response) => {
const {timeOfMeasureLower, timeOfMeasureTop, O2Value, BPMValue} = request.body
console.log(request.body)
pool.query(
'INSERT INTO pulsioximeterRecords (timeofmeasurelower, timeofmeasuretop, o2value, bpmvalue) VALUES ($1, $2, $3, $4)',
[timeOfMeasureLower, timeOfMeasureTop, O2Value, BPMValue],
(error) => {
if (error) {
throw error
}
response.status(201).json({status: 'success', message: 'Record added.'})
},
)
}
app
.route('/records')
// GET endpoint
.get(getRecords)
// POST endpoint
.post(addRecord)
app
.route('/glucoseLastRecord')
.get(getGlucoseLastRecord)
app
.route('/pulseOximeterRecord')
.get(getPulseOximeterLastRecord)
.post(addPulseOximeterRecord)
// Start server
app.listen(PORT, () => {
console.log(`Server listening`)
})