Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved by mustafa bahedh #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express')
const app = express()
const patientsRouter = require('./src/routes/v1/patients')
const historyRouter = require('./src/routes/v1/history')

const PORT = 5000;
// middlwares
Expand All @@ -13,6 +14,8 @@ app.get('/', (req, res)=>{

// call routers

app.use('/api/v1/patients',patientsRouter);
app.use('/api/v1/patients', patientsRouter);
app.use('/api/v1/history', historyRouter);


app.listen(PORT)
157 changes: 83 additions & 74 deletions src/controllers/v1/history/HistoryController.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,89 @@
const {success, error} = require('../../../utils/responser')
const { success, error } = require("../../../utils/responser");
const history = [
{
id:1,
patient_id:1,
date:"10/10/2022",
report:"Bla Bla Bla Bla",
prescription:[
{
id:1,
name:"Panadol",
dose:"Twice each 12 hours",
note:""
},
{
id:2,
name:"Aspirin",
dose:"Once at morning",
note:""
}
]
},
{
id:2,
patient_id:2,
date:"11/10/2022",
report:"Bla Bla Bla Bla",
prescription:[
{
id:1,
name:"Panadol",
dose:"Twice each 12 hours",
note:""
},
{
id:2,
name:"Aspirin",
dose:"Once at morning",
note:""
}
]
},
{
id:3,
patient_id:3,
date:"11/10/2022",
report:"Bla Bla Bla Bla",
prescription:[
{
id:1,
name:"Panadol",
dose:"Twice each 12 hours",
note:""
},
{
id:2,
name:"Aspirin",
dose:"Once at morning",
note:""
}
]
},
]
{
id: 1,
patient_id: 1,
date: "10/10/2022",
report: "Bla Bla Bla Bla",
prescription: [
{
id: 1,
name: "Panadol",
dose: "Twice each 12 hours",
note: "",
},
{
id: 2,
name: "Aspirin",
dose: "Once at morning",
note: "",
},
],
},
{
id: 2,
patient_id: 2,
date: "11/10/2022",
report: "Bla Bla Bla Bla",
prescription: [
{
id: 1,
name: "Panadol",
dose: "Twice each 12 hours",
note: "",
},
{
id: 2,
name: "Aspirin",
dose: "Once at morning",
note: "",
},
],
},
{
id: 3,
patient_id: 3,
date: "11/10/2022",
report: "Bla Bla Bla Bla",
prescription: [
{
id: 1,
name: "Panadol",
dose: "Twice each 12 hours",
note: "",
},
{
id: 2,
name: "Aspirin",
dose: "Once at morning",
note: "",
},
],
},
];

// TO-DO
// Create history for given patient ID
const createHistoryForP=(req, res)=>{
const id = req.params.id;//patient
// TO-DO
}
const getAllHistory = (req, res)=>{
return res.status(200).json(success(200,history,"Success"))
}


const createHistoryForP = (req, res) => {
const id = req.params.id;
let patientHistory = {
id: req.body.id,
patient_id: id,
date: req.body.date,
report: req.body.report,
prescription: req.body.prescription,
};
history.push(patientHistory);
return res
.status(200)
.json(success(200, history, `patient history created successfully`));
};
const getAllHistory = (req, res) => {
return res.status(200).json(success(200, history, "Success"));
};

module.exports = {
getAllHistory,
createHistoryForP
}
getAllHistory,
createHistoryForP,
history,
};
150 changes: 97 additions & 53 deletions src/controllers/v1/patients/PatientController.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,103 @@
const {success, error} = require('../../../utils/responser')
const { success, error } = require("../../../utils/responser");
const { history } = require("../history/HistoryController");
const patients = [
{
id:1,
full_name:"Adel Ahmed",
birth_date:"19/10/2022",
gender:"male",
code:"100",
phone:"+9647711332225"
},
{
id:1,
full_name:"Sara Ali",
birth_date:"01/10/2000",
gender:"female",
code:"101",
phone:"+9647711332226"
},
{
id:3,
full_name:"Saif Ahmed",
birth_date:"16/05/1977",
gender:"male",
code:"102",
phone:"+9647711332227"
},

]
const getAllPatients = (req, res)=>{
return res.status(200).json(success(200,patients,"Success"))
}
const getPatientById = (req, res)=>{
const id = req.params.id;
// TO-DO
}
{
id: 1,
full_name: "Adel Ahmed",
birth_date: "19/10/2022",
gender: "male",
code: "100",
phone: "+9647711332225",
},
{
id: 2,
full_name: "Sara Ali",
birth_date: "01/10/2000",
gender: "female",
code: "101",
phone: "+9647711332226",
},
{
id: 3,
full_name: "Saif Ahmed",
birth_date: "16/05/1977",
gender: "male",
code: "102",
phone: "+9647711332227",
},
];
const getAllPatients = (req, res) => {
return res.status(200).json(success(200, patients, "Success"));
};
const getPatientById = (req, res) => {
const id = req.params.id;
let patient = patients.find((item) => item && item.id == id);
if (!patient)
return res.status(404).json(error(404, `patient with id ${id} not found`));
else return res.status(200).json(success(200, patient, `of for ${id}`));
};

const deletePatient = (req, res)=>{
const id = req.params.id;
// TO-DO
}
const deletePatient = (req, res) => {
const id = req.params.id;
let patientIndex = patients.findIndex((item) => item && item.id == id);
if (patientIndex > -1) {
patients.splice(patientIndex, 1);
return res
.status(200)
.json(
success(200, patients, `patient with id ${id} deleted successfully`)
);
} else
return res.status(404).json(error(404, `patient with id ${id} not found`));
};

const updatePatient = (req, res)=>{
const id = req.params.id;
// TO-DO
}
const updatePatient = (req, res) => {
const id = req.params.id;
let patientIndex = patients.findIndex((item) => item && item.id == id);
if (patientIndex > -1) {
let updatePatient = {
id: id,
full_name: req.body.full_name,
birth_date: req.body.birth_date,
gender: req.body.gender,
code: req.body.code,
phone: req.body.phone,
};
patients[patientIndex] = updatePatient;
return res
.status(200)
.json(
success(200, patients, `patient with id ${id} deleted successfully`)
);
} else
return res.status(404).json(error(404, `patient with id ${id} not found`));
};

const getHistoryOfPatient = (req, res)=>{
const id = req.params.id; // patient id
// TO-DO
return res.status(200).json(success(200,{},"Ok"))
}
const getHistoryOfPatient = (req, res) => {
const id = req.params.id;
const patientHistory = history.find((item) => item.patient_id == id);
if (patientHistory)
return res.status(200).json(success(200, patientHistory, "Ok"));
else return res.status(204).json(error(404, "not found"));
};

const createPatient = (req, res) => {
let patient = {
id: req.body.id,
full_name: req.body.full_name,
birth_date: req.body.birth_date,
gender: req.body.gender,
code: req.body.code,
phone: req.body.phone,
};
patients.push(patient);
return res.status(200).json(success(200, patients, `created successfully`));
};
module.exports = {
getAllPatients,
getPatientById,
deletePatient,
updatePatient,
getHistoryOfPatient
}
getAllPatients,
getPatientById,
deletePatient,
updatePatient,
createPatient,
getHistoryOfPatient,
};
16 changes: 9 additions & 7 deletions src/routes/v1/history.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require('express')
const router = express.Router()
const {getAllHistory} = require('../../controllers/v1/history/HistoryController')
const express = require("express");
const router = express.Router();
const {
getAllHistory,
createHistoryForP,
} = require("../../controllers/v1/history/HistoryController");


router.get('/', getAllHistory);
// TO-DO // add endpoint for posting new history for a patient
module.exports = router;
router.get("/", getAllHistory);
router.post("/:id", createHistoryForP);
module.exports = router;
28 changes: 17 additions & 11 deletions src/routes/v1/patients.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
const express = require('express')
const router = express.Router()
const {getAllPatients, getPatientById, deletePatient, updatePatient, getHistoryOfPatient} = require('../../controllers/v1/patients/PatientController')
const express = require("express");
const router = express.Router();
const {
getAllPatients,
getPatientById,
deletePatient,
updatePatient,
getHistoryOfPatient,
createPatient,
} = require("../../controllers/v1/patients/PatientController");



router.get('/', getAllPatients);
router.get('/:id', getPatientById);
router.delete('/:id', deletePatient);
router.put('/:id', updatePatient);
router.get('/:id/history', getHistoryOfPatient);
router.get("/", getAllPatients);
router.get("/:id", getPatientById);
router.post("/", createPatient);
router.delete("/:id", deletePatient);
router.put("/:id", updatePatient);
router.get("/:id/history", getHistoryOfPatient);
// TO-DO // add endpoint for adding new Patient
module.exports = router;
module.exports = router;