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

By yousif ahmed #6

Open
wants to merge 2 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
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const express = require('express')
const app = express()
const patientsRouter = require('./src/routes/v1/patients')
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
app.use(express.json())
// middleware
app.use(express.json());

// for testing
app.get('/', (req, res)=>{
res.send('<h1>This is Clinic Restful API</h1>')
app.get("/", (req, res) => {
res.send("<h1>This is Clinic Restful API</h1>");
});

// call routers

app.use('/api/v1/patients',patientsRouter);
app.use("/api/v1/patients", patientsRouter);
app.use("/api/v1/patients-history", historyRouter);

app.listen(PORT)
app.listen(PORT);
162 changes: 88 additions & 74 deletions src/controllers/v1/history/HistoryController.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,94 @@
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"))
}

// صار تدلل XD

const createHistoryForP = (req, res) => {
const id = req.params.id; //patient
let { patient_id, date, report, prescription } = req.body;
let exist = history.findIndex((item) => item.id == id);
if (exist == -1) {
let update = {
id: id,
patient_id: patient_id,
date: date,
report: report,
prescription: prescription,
};
history.push(update);
res.status(200).json(success(200, update, "history added"));
} else {
res.status(404).json(error(404, "something want wrong"));
}
};
const getAllHistory = (req, res) => {
return res.status(200).json(success(200, history, "Success"));
};

module.exports = {
getAllHistory,
createHistoryForP
}
history,
getAllHistory,
createHistoryForP,
};
162 changes: 109 additions & 53 deletions src/controllers/v1/patients/PatientController.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,115 @@
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:"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 patientIndex = patients.findIndex((item) => item.id == id);
if (patientIndex > -1) {
res
.status(200)
.json(success(200, patients[patientIndex], "Patient founded"));
} else {
res.status(404).json(error(404, "Patient not found at this 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.id == id);
if (patientIndex > -1) {
let slicedPatient = patients.splice(patientIndex, 1);
res
.status(200)
.json(
success(200, slicedPatient, "The patient you want to delete is gone")
);
} else {
res.status(404).json(error(404, "Patient not found"));
}
};

const updatePatient = (req, res)=>{
const id = req.params.id;
// TO-DO
}
const updatePatient = (req, res) => {
const id = req.params.id;
let { full_name, birth_date, gender, code, phone } = req.body;
let patientIndex = patients.findIndex((item) => item.id == id);
if (patientIndex > -1) {
let update = {
id: id,
full_name: full_name,
birth_date: birth_date,
gender: gender,
code: code,
phone: phone,
};
patients[patientIndex] = update;

const getHistoryOfPatient = (req, res)=>{
const id = req.params.id; // patient id
// TO-DO
return res.status(200).json(success(200,{},"Ok"))
}
res.status(200).json(success(200, update, "User updated"));
} else {
res.status(404).json(error(404, "Patient not found"));
}
};

const getHistoryOfPatient = (req, res) => {
const id = req.params.id; // patient id
let historyOfPatient = history.filter((item) => item.id == id);
if (history.length > 0) {
return res.status(200).json(success(200, historyOfPatient, "Ok"));
} else {
res.status(404).json(error(404, "History not found"));
}
};
const addNewPatient = (req, res) => {
let { id, full_name, birth_date, gender, code, phone } = req.body;
let addPatient = {
id: id,
full_name: full_name,
birth_date: birth_date,
gender: gender,
code: code,
phone: phone,
};
let exist = patients.findIndex((item) => item.id == id);
if (exist == -1) {
patients.push(addPatient);
res.status(200).json(success(200, addNewPatient, "Patient added"));
} else {
res.status(409).json(error(409, "Patient id already exist"));
}
};
module.exports = {
getAllPatients,
getPatientById,
deletePatient,
updatePatient,
getHistoryOfPatient
}
getAllPatients,
getPatientById,
deletePatient,
updatePatient,
getHistoryOfPatient,
addNewPatient,
patients,
};
16 changes: 10 additions & 6 deletions src/routes/v1/history.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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);
router.get("/", getAllHistory);
// TO-DO // add endpoint for posting new history for a patient
module.exports = router;
//you got it
router.post("/:id", createHistoryForP);
module.exports = router;
Loading