From 852e320a843f2087526d809d02de9255a65f3716 Mon Sep 17 00:00:00 2001 From: movy18x Date: Thu, 20 Oct 2022 09:17:18 +0300 Subject: [PATCH 1/2] solved --- index.js | 5 +- .../v1/history/HistoryController.js | 157 +++++++++--------- .../v1/patients/PatientController.js | 151 +++++++++++------ src/routes/v1/history.js | 16 +- src/routes/v1/patients.js | 28 ++-- 5 files changed, 211 insertions(+), 146 deletions(-) diff --git a/index.js b/index.js index c6e598c..fa21cf2 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 @@ -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) \ No newline at end of file diff --git a/src/controllers/v1/history/HistoryController.js b/src/controllers/v1/history/HistoryController.js index 781640c..a146681 100644 --- a/src/controllers/v1/history/HistoryController.js +++ b/src/controllers/v1/history/HistoryController.js @@ -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 -} \ No newline at end of file + getAllHistory, + createHistoryForP, + history, +}; diff --git a/src/controllers/v1/patients/PatientController.js b/src/controllers/v1/patients/PatientController.js index f0d9ea8..a17ebeb 100644 --- a/src/controllers/v1/patients/PatientController.js +++ b/src/controllers/v1/patients/PatientController.js @@ -1,59 +1,104 @@ -const {success, error} = require('../../../utils/responser') +const { failed } = require("../../../../../project-0/src/responserApi"); +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(failed(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(failed(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(failed(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(failed(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 -} \ No newline at end of file + getAllPatients, + getPatientById, + deletePatient, + updatePatient, + createPatient, + getHistoryOfPatient, +}; diff --git a/src/routes/v1/history.js b/src/routes/v1/history.js index b4695bb..18d5058 100644 --- a/src/routes/v1/history.js +++ b/src/routes/v1/history.js @@ -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; \ No newline at end of file +router.get("/", getAllHistory); +router.post("/:id", createHistoryForP); +module.exports = router; diff --git a/src/routes/v1/patients.js b/src/routes/v1/patients.js index 9c7a76f..77ad78a 100644 --- a/src/routes/v1/patients.js +++ b/src/routes/v1/patients.js @@ -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; \ No newline at end of file +module.exports = router; From 7e42624f621697f22e492b0b2352fca9c48f23b0 Mon Sep 17 00:00:00 2001 From: movy18x Date: Thu, 20 Oct 2022 09:23:36 +0300 Subject: [PATCH 2/2] solved --- src/controllers/v1/patients/PatientController.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/controllers/v1/patients/PatientController.js b/src/controllers/v1/patients/PatientController.js index a17ebeb..3d572da 100644 --- a/src/controllers/v1/patients/PatientController.js +++ b/src/controllers/v1/patients/PatientController.js @@ -1,4 +1,3 @@ -const { failed } = require("../../../../../project-0/src/responserApi"); const { success, error } = require("../../../utils/responser"); const { history } = require("../history/HistoryController"); const patients = [ @@ -34,7 +33,7 @@ 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(failed(404, `patient with id ${id} not found`)); + 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}`)); }; @@ -49,7 +48,7 @@ const deletePatient = (req, res) => { success(200, patients, `patient with id ${id} deleted successfully`) ); } else - return res.status(404).json(failed(404, `patient with id ${id} not found`)); + return res.status(404).json(error(404, `patient with id ${id} not found`)); }; const updatePatient = (req, res) => { @@ -71,7 +70,7 @@ const updatePatient = (req, res) => { success(200, patients, `patient with id ${id} deleted successfully`) ); } else - return res.status(404).json(failed(404, `patient with id ${id} not found`)); + return res.status(404).json(error(404, `patient with id ${id} not found`)); }; const getHistoryOfPatient = (req, res) => { @@ -79,7 +78,7 @@ const getHistoryOfPatient = (req, res) => { 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(failed(404, "not found")); + else return res.status(204).json(error(404, "not found")); }; const createPatient = (req, res) => {