Skip to content

Commit a7d5ee0

Browse files
committed
Registering patients
1 parent 4f2aa4a commit a7d5ee0

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

app/src/App.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@
2727
}
2828
}
2929
}
30+
31+
input, button {
32+
border: none;
33+
padding: 10px;
34+
margin: 5px;
35+
border-radius: 0.5em;
36+
background-color: rgba(0, 0, 0, 0.15);
37+
}
38+
39+
button:not(:disabled) {
40+
cursor: pointer;
41+
}
3042
</style>

app/src/components/views/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
export default {
22
methods: {
33
async fetchData(url=false) {
4+
if (this.id == 'create' && !url) {
5+
this.query = false;
6+
this.data = {
7+
8+
};
9+
this._data = JSON.stringify(this.data);
10+
this.loaded = true;
11+
if ('afterFetch' in this) {
12+
//await this.afterFetch();
13+
}
14+
return;
15+
}
416
let path = templateString(url || this.path, this);
517
console.log(path);
618
let resp = await (
@@ -40,6 +52,16 @@ export default {
4052
})
4153
).json();
4254
(subject || this).saveResponse = resp;
55+
if (resp.results && 'insertId' in resp.results) {
56+
if (!('id' in this.$route.params)) {
57+
this.$router.push({
58+
name: 'Patient',
59+
params: {
60+
id: resp.results.insertId,
61+
}
62+
})
63+
}
64+
}
4365
await this.fetchData();
4466
return resp;
4567
},

app/src/router/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ const routes = [
4747
// which is lazy-loaded when the route is visited.
4848
component: () => import(/* webpackChunkName: "about" */ '../views/database/Patients.vue')
4949
},
50+
{
51+
path: '/database/patient/register',
52+
name: 'Register Patient',
53+
// route level code-splitting
54+
// this generates a separate chunk (about.[hash].js) for this route
55+
// which is lazy-loaded when the route is visited.
56+
component: () => import(/* webpackChunkName: "about" */ '../views/database/RegisterPatient.vue')
57+
},
5058
{
5159
path: '/database/patient/:id',
5260
name: 'Patient',

app/src/views/database/Patients.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="patients">
33
<PatientsList/>
4-
4+
<button v-on:click="register">Register Patient</button>
55
</div>
66
</template>
77

@@ -13,6 +13,13 @@ export default {
1313
name: 'Patients',
1414
components: {
1515
PatientsList
16-
}
16+
},
17+
methods: {
18+
register() {
19+
this.$router.push({
20+
name: 'Register Patient',
21+
})
22+
}
23+
},
1724
}
1825
</script>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="patient">
3+
<PatientView id="create"/>
4+
5+
</div>
6+
</template>
7+
8+
<script>
9+
// @ is an alias to /src
10+
import PatientView from '@/components/views/Patient.vue'
11+
12+
export default {
13+
name: 'Patient',
14+
components: {
15+
PatientView
16+
}
17+
}
18+
</script>

server/api/db/routes/patients.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ router.post('/patients/:patient_id.json', $attempt(async (req, res) => {
4646
let changes = JSON.parse(req.body);
4747
let fields = Object.keys(changes);
4848
let values = Object.values(changes);
49-
let args = values;
50-
let query = `UPDATE patients SET ${fields.map(o => "" + req.$param(o).slice(0, -1).substr(1) + " = ?").join(',')} WHERE patient_id = ?`;
49+
let args = [];
50+
let query;
51+
if (req.params.patient_id == 'create') {
52+
query = `INSERT INTO patients (${fields.map(o => "\`" + req.$param(o).slice(0, -1).substr(1) + '\`').join(', ')})
53+
VALUES (${values.map(o => '?').join(', ')})`;
54+
args = values;
55+
} else {
56+
query = `UPDATE patients SET ${fields.map(o => "" + req.$param(o).slice(0, -1).substr(1) + " = ?").join(',')} WHERE patient_id = ?`;
57+
args = values;
58+
args.push(Number(req.params.patient_id));
59+
}
5160
query = normalizeQuery(`${query} ${$filtering(req)}`);
52-
args.push(Number(req.params.patient_id));
5361
console.log(query, args);
5462
return {
5563
results: (await db.query(query, args)).results,

0 commit comments

Comments
 (0)