-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
167 lines (145 loc) · 4.12 KB
/
router.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var models = require('./Public/Model/User');
var express = require('express');
var router = express.Router();
module.exports = function (app, bodyParser) {
var doctor, patient;
router.use(bodyParser.json());
//router to login doctor and chec if it exist or not
/*router.route('/getDoctor').get(function (req, res) {
var firstName = req.query.firstName;
var lastName = req.query.lastName;
models.Doctor.findOne({
firstName: firstName,
lastName: lastName
}, function (err, data) {
if (err) {
console.log(err);
} else {
res.status(200).send(data);
}
})
});*/
// get all the doctor and send it back to angular
router.route('/getAllDoctor').get(function (req, res) {
var q = models.Doctor.find({});
q.exec(function (err, data) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
})
})
//get patient and and send data back to whoever made the request
router.route('/getPatient').get(function (req, res) {
var q = models.Patient.find({});
q.exec(function (err, data) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
})
});
// handeles search patient with patient lastName or doctor lastName
router.route('/searchPatient').get(function (req, res) {
var lastName = req.query.data;
models.Patient.find({
$or: [{
"lastName": lastName
}, {
"familyDoctor.lastName": lastName
}]
}, function (err, data) {
if (err) {
console.log(err);
} else {
res.end(JSON.stringify(data));
}
})
})
//save the doctor. Basically register a new doctor
router.route('/submitDoctor').post(function (req, res) {
//create doctor model;
doctor = new models.Doctor({
"username":req.body.username,
"password":req.body.password,
"firstName": req.body.firstName,
"lastName": req.body.lastName
});
//save doctor in doctor model
doctor.save(function (err, data) {
if (err) {
console.log(err);
} else
console.log(data);
res.sendStatus(200);
});
});
// delete the patient based on the firstName and lastName
router.route('/deletePatient').delete(function (req, res) {
var firstName = req.query.firstName;
var lastName = req.query.lastName;
models.Patient.remove({
firstName: firstName,
lastName: lastName
}, function (err, data) {
if (err) {
console.log(err);
} else {
console.log("Patient Deleted") // if success then output patient deleted message
res.status(200).send(firstName + ' ' + lastName + " was deleted");
}
})
})
//register a patient in patient model
router.route('/submitPatient').post(function (req, res) {
//create patient model
patient = new models.Patient({
"firstName": req.body.firstName,
"lastName": req.body.lastName,
"visits": {
"complaint": req.body.complaint,
"billingAmount": req.body.billingAmount
},
"age": req.body.age,
"familyDoctor": [{
firstName: req.body.familyDoctor.firstName,
lastName: req.body.familyDoctor.lastName,
}],
"createdAt": req.body.createdAt,
"lastModified": req.body.lastModified
});
//save the patient
patient.save(function (err, data) {
if (err) {
console.log(err); // show the err
} else
console.log(data)
res.sendStatus(200); //if succeed then send 200 status
});
});
// update the patient based on the ID that was sent from angular
router.route('/updatePatient').post(function (req, res) {
var id = req.body._id;
models.Patient.update({
_id: id
}, {
"firstName": req.body.firstName,
"lastName": req.body.lastName,
visits: {
complaint: req.body.visits.complaint,
billingAmount: req.body.visits.billingAmount
},
age: req.body.age,
familyDoctor: [{
firstName: req.body.familyDoctor[0].firstName,
lastName: req.body.familyDoctor[0].lastName
}],
createdAt: req.body.createdAt,
lastModified: req.body.lastModified
}, function (err, data) {
if (err) {
console.log(err);
} else {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify("Patient was updated")); // if success then end the response and send success message
}
});
});
app.use('/', router);
};