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 Rasheed Wathiq #2

Open
wants to merge 1 commit 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
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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)
15 changes: 14 additions & 1 deletion src/controllers/v1/history/HistoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -76,5 +88,6 @@ const getAllHistory = (req, res)=>{

module.exports = {
getAllHistory,
createHistoryForP
createHistoryForP,
history
}
68 changes: 66 additions & 2 deletions src/controllers/v1/patients/PatientController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {success, error} = require('../../../utils/responser')
const {history} = require('../history/HistoryController')
const patients = [
{
id:1,
Expand All @@ -9,7 +10,7 @@ const patients = [
phone:"+9647711332225"
},
{
id:1,
id:2,
full_name:"Sara Ali",
birth_date:"01/10/2000",
gender:"female",
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion 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 {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;
7 changes: 6 additions & 1 deletion src/routes/v1/patients.js
Original file line number Diff line number Diff line change
@@ -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')




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