-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedicalTests.js
96 lines (76 loc) · 2.59 KB
/
medicalTests.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
import { hospitalData } from "./hospitalData.js";
import express from "express";
const app = express();
import { v4 as uuidv4 } from "uuid";
app.use(express.json());
const medicalTests = hospitalData.medicalStaff.medicalTests;
//********************************************************************************************** */
app.get("/medical-tests", (req, res) => {
const { patientId, testName } = req.query;
let filteredMedicalTests = [...medicalTests];
if (patientId) {
filteredMedicalTests = filteredMedicalTests.filter(
(test) => test.patientId === parseInt(patientId)
);
}
if (testName) {
filteredMedicalTests = filteredMedicalTests.filter(
(test) => test.testName === testName
);
}
res.json({ medicalTests: filteredMedicalTests });
});
//********************************************************************************************** */
app.post("/medical-tests", (req, res) => {
const newMedicalTest = req.body;
// Input Validation:
if (!newMedicalTest.patientId || !newMedicalTest.testName) {
return res.status(400).json({
message: "Patient ID and Test type are Required",
});
}
newMedicalTest.id = uuidv4();
medicalTests.push(newMedicalTest);
res.status(201).json({
message: "Medical test added successfully",
newMedicalTest,
});
});
//********************************************************************************************** */
app.put("/medical-tests/:id", (req, res) => {
const testId = parseInt(req.params.id);
const updatedMedicalTest = req.body;
// Input Validation:
if (!updatedMedicalTest.testName) {
return res.status(400).json({
message: "Test Name is required",
});
}
const index = medicalTests.findIndex((test) => test.id === testId);
if (index !== -1) {
medicalTests[index] = {
...medicalTests[index],
...updatedMedicalTest,
};
res.json({
message: "Medical test updated successfully",
medicalTest: medicalTests[index],
});
} else {
res.status(404).json({ message: "Medical test not found" });
}
});
//********************************************************************************************** */
app.delete("/medical-tests/:id", (req, res) => {
const testId = parseInt(req.params.id);
const index = medicalTests.findIndex((test) => test.id === testId);
if (index !== -1) {
medicalTests.splice(index, 1);
res.json({ message: "Medical test deleted successfully" });
} else {
res.status(404).json({ message: "Medical test not found" });
}
});
app.listen(3004, () => {
console.log("Server is running on port 3004");
});