diff --git a/index.js b/index.js index c6e598c..040f217 100644 --- a/index.js +++ b/index.js @@ -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 @@ -14,5 +15,6 @@ app.get('/', (req, res)=>{ // call routers app.use('/api/v1/patients',patientsRouter); +app.use('/api/v1/history',historyRouter); app.listen(PORT) \ No newline at end of file diff --git a/src/controllers/v1/history/HistoryController.js b/src/controllers/v1/history/HistoryController.js index 781640c..354abf8 100644 --- a/src/controllers/v1/history/HistoryController.js +++ b/src/controllers/v1/history/HistoryController.js @@ -1,80 +1,91 @@ -const {success, error} = require('../../../utils/responser') +const { success, error } = require('../../../utils/responser') +// const { patients } = require('../patients/PatientController') + const history = [ { - id:1, - patient_id:1, - date:"10/10/2022", - report:"Bla Bla Bla Bla", - prescription:[ + 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: 1, + name: "Panadol", + dose: "Twice each 12 hours", + note: "" }, { - id:2, - name:"Aspirin", - dose:"Once at morning", - 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: 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: 1, + name: "Panadol", + dose: "Twice each 12 hours", + note: "" }, { - id:2, - name:"Aspirin", - dose:"Once at morning", - 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: 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: 1, + name: "Panadol", + dose: "Twice each 12 hours", + note: "" }, { - id:2, - name:"Aspirin", - dose:"Once at morning", - 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 createHistoryForP = (req, res) => { + const id = req.params.id + let newHistory = req.body + newHistory.patient_id = id + // console.log(patients); + // let index = patients.findIndex(el => el.id == id); + + // if (index > -1) { + history.push(newHistory) + return res.status(200).json(success(200, newHistory, "Success")) + // } else { + return res.status(404).json(error(404, "Not Found")) + // } } -const getAllHistory = (req, res)=>{ - return res.status(200).json(success(200,history,"Success")) +const getAllHistory = (req, res) => { + return res.status(200).json(success(200, history, "Success")) } module.exports = { getAllHistory, - createHistoryForP + createHistoryForP, + history, } \ No newline at end of file diff --git a/src/controllers/v1/patients/PatientController.js b/src/controllers/v1/patients/PatientController.js index f0d9ea8..9b1dcc3 100644 --- a/src/controllers/v1/patients/PatientController.js +++ b/src/controllers/v1/patients/PatientController.js @@ -1,59 +1,94 @@ -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: "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: 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" + 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 getAllPatients = (req, res) => { + return res.status(200).json(success(200, patients, "Success")) } -const getPatientById = (req, res)=>{ +const getPatientById = (req, res) => { const id = req.params.id; - // TO-DO + let patient = patients.find(el => el.id == id); + if(patient){ + return res.status(200).json(success(200, patient, "Success")) + }else{ + return res.status(404).json(error(404, "Not Found")) + } } -const deletePatient = (req, res)=>{ +const deletePatient = (req, res) => { const id = req.params.id; - // TO-DO + let index = patients.findIndex(el => el.id == id); + if (index > -1) { + let patient = patients[index] + patients.splice(index, 1) + return res.status(200).json(success(200, patient, "Success")) + } else { + return res.status(404).json(error(404, "Not Found")) + } } -const updatePatient = (req, res)=>{ +const updatePatient = (req, res) => { const id = req.params.id; - // TO-DO + let index = patients.findIndex(el => el.id == id); + if (index > -1) { + let newPatient = { + 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[index] = newPatient; + + return res.status(200).json(success(200, patients[index], "Success")) + } else { + return res.status(404).json(error(404, "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; + var patientHistory = history.filter(el => el.patient_id == id) + if(patientHistory){ + + return res.status(200).json(success(200,patientHistory, "Ok")) + }else{ + return res.status(404).json(error(404, "Not Found")) + } } + module.exports = { getAllPatients, getPatientById, deletePatient, updatePatient, - getHistoryOfPatient + getHistoryOfPatient, + patients, } \ No newline at end of file diff --git a/src/routes/v1/patients.js b/src/routes/v1/patients.js index 9c7a76f..e9cbf3d 100644 --- a/src/routes/v1/patients.js +++ b/src/routes/v1/patients.js @@ -1,6 +1,7 @@ const express = require('express') const router = express.Router() const {getAllPatients, getPatientById, deletePatient, updatePatient, getHistoryOfPatient} = require('../../controllers/v1/patients/PatientController') +const {createHistoryForP} = require('../../controllers/v1/history/HistoryController') @@ -9,5 +10,6 @@ router.get('/:id', getPatientById); router.delete('/:id', deletePatient); router.put('/:id', updatePatient); router.get('/:id/history', getHistoryOfPatient); +router.post('/:id/history', createHistoryForP); // TO-DO // add endpoint for adding new Patient module.exports = router; \ No newline at end of file