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

Ali abbas Solution #1

Open
wants to merge 4 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
2 changes: 2 additions & 0 deletions 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 @@ -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)
107 changes: 59 additions & 48 deletions src/controllers/v1/history/HistoryController.js
Original file line number Diff line number Diff line change
@@ -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,
}
101 changes: 68 additions & 33 deletions src/controllers/v1/patients/PatientController.js
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions src/routes/v1/patients.js
Original file line number Diff line number Diff line change
@@ -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')



Expand All @@ -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;