From b724bc7742e6d3f3188521ad14374400449001e6 Mon Sep 17 00:00:00 2001 From: rasheed98-dev Date: Thu, 20 Oct 2022 02:49:37 +0300 Subject: [PATCH] solved --- index.js | 9 +++ .../v1/history/HistoryController.js | 15 +++- .../v1/patients/PatientController.js | 68 ++++++++++++++++++- src/routes/v1/history.js | 4 +- src/routes/v1/patients.js | 7 +- 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c6e598c..b06aa63 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,15 @@ const express = require('express') const app = express() const patientsRouter = require('./src/routes/v1/patients') +const historyRouter = require('./src/routes/v1/history') +const bodyParser = require('body-parser') +app.use(bodyParser.json()) +app.use( + bodyParser.urlencoded({ + extended: true, + }) +) const PORT = 5000; // middlwares app.use(express.json()) @@ -14,5 +22,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..fe585cd 100644 --- a/src/controllers/v1/history/HistoryController.js +++ b/src/controllers/v1/history/HistoryController.js @@ -66,7 +66,19 @@ const history = [ // Create history for given patient ID const createHistoryForP=(req, res)=>{ const id = req.params.id;//patient + const {date, report, prescription} = req.body + let newHistory = { + id: id, + patient_id: id, + date: date, + report: report, + prescription:prescription + } + history.push(newHistory) + return res.status(200).json(success(200, newHistory, "ok")) + // TO-DO + // console.log(req.body) } const getAllHistory = (req, res)=>{ return res.status(200).json(success(200,history,"Success")) @@ -76,5 +88,6 @@ const getAllHistory = (req, res)=>{ 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..0f631aa 100644 --- a/src/controllers/v1/patients/PatientController.js +++ b/src/controllers/v1/patients/PatientController.js @@ -1,4 +1,5 @@ const {success, error} = require('../../../utils/responser') +const {history} = require('../history/HistoryController') const patients = [ { id:1, @@ -9,7 +10,7 @@ const patients = [ phone:"+9647711332225" }, { - id:1, + id:2, full_name:"Sara Ali", birth_date:"01/10/2000", gender:"female", @@ -32,25 +33,88 @@ const getAllPatients = (req, res)=>{ const getPatientById = (req, res)=>{ const id = req.params.id; // TO-DO + let patient = patients.find((p)=> p.id == Number(id)) + if(!patient){ + return res.status(404).json(error(404,"patient Not Found")) + + } else{ + return res.status(200).json(success(200, patient, "ok")) + + + } } const deletePatient = (req, res)=>{ const id = req.params.id; // TO-DO + let patientIndex = patients.findIndex((p)=> p.id == Number(id)) + if(patientIndex > -1) + { + patients.splice(patientIndex,1) + res.status(200).json(success(200, patientIndex, `patient ${id} deleted`)) + } + else{ + res.status(404).json(error(404,"patient Not Found and not deleted")) + + } } const updatePatient = (req, res)=>{ const id = req.params.id; // TO-DO + const {full_name, birth_date, gender, code, phone}= req.body + let patient = patients.find((p)=> p.id == Number(id)) + + if(!patient){ + return res.status(404).json(error(404,"patient Not Found")) + + }else{ + patients.map((p)=>{ + if(p.id == Number(id)){ + p.id = id + p.full_name = full_name + p.birth_date= birth_date + p.gender = gender + p.code = code + p.phone = phone + } + + // return patient + }) + return res.status(200).json(success(200,patient,"Ok")) + + } + + + } const getHistoryOfPatient = (req, res)=>{ const id = req.params.id; // patient id + let history1 = history.find((h)=>(h.patient_id== Number(id))) // TO-DO - return res.status(200).json(success(200,{},"Ok")) + return res.status(200).json(success(200,history1,"Ok")) } +const createPatient=(req, res)=>{ + const {id, full_name, birth_date, gender, code, phone}= req.body + console.log(id) + let patient = { + id: id, + full_name: full_name, + birth_date: birth_date, + gender: gender, + code: code, + phone: phone + } + + patients.push(patient) + + return res.status(200).json(success(200, patient, "ok")) + +} module.exports = { + createPatient, getAllPatients, getPatientById, deletePatient, diff --git a/src/routes/v1/history.js b/src/routes/v1/history.js index b4695bb..c48373d 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 {getAllHistory, createHistoryForP} = require('../../controllers/v1/history/HistoryController') router.get('/', getAllHistory); + // TO-DO // add endpoint for posting new history for a patient +// router.post('/', createHistoryForP); module.exports = router; \ No newline at end of file diff --git a/src/routes/v1/patients.js b/src/routes/v1/patients.js index 9c7a76f..4a46056 100644 --- a/src/routes/v1/patients.js +++ b/src/routes/v1/patients.js @@ -1,6 +1,9 @@ const express = require('express') const router = express.Router() -const {getAllPatients, getPatientById, deletePatient, updatePatient, getHistoryOfPatient} = require('../../controllers/v1/patients/PatientController') +const {getAllPatients, getPatientById, deletePatient, updatePatient, getHistoryOfPatient, createPatient} = require('../../controllers/v1/patients/PatientController') +// const {createHistoryForP} = require('../.../controllers/v1/history/HistoryController') +const {createHistoryForP} = require('../../controllers/v1/history/HistoryController') + @@ -9,5 +12,7 @@ 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 +router.post('/',createPatient ); module.exports = router; \ No newline at end of file