Skip to content

Commit

Permalink
Fix edit patient information
Browse files Browse the repository at this point in the history
Now we can update patient info through PATCH
  • Loading branch information
ffracture committed Dec 18, 2023
1 parent 7c4f3d8 commit 8b2b4f2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
86 changes: 43 additions & 43 deletions server/app/Controller/DoctorController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mysql = require("mysql");
const sqlObject= require("../../untils/Sql")
const {multipleSQLToObject, SQLToObject} = require("../../untils/Sql");
const sqlObject = require("../../untils/Sql")
const { multipleSQLToObject, SQLToObject } = require("../../untils/Sql");
const db = (connect = mysql.createConnection({
host: "127.0.0.1",
user: "root",
Expand All @@ -11,116 +11,116 @@ const db = (connect = mysql.createConnection({
class DoctorController {
//[GET] /doctor/patient_list
async getAllPatients(req, res) {
const d_Id= req.cookies.d_ID;
const d_Id = req.cookies.d_ID;
console.log(d_Id);
// const doctorDeptid= req.cookies.doctor_deptid; // đăng nhập lưu cái deptid của doctor vào cookies rồi lấy ra để check
try {
await db.query(`SELECT pName,pId FROM inchargeof WHERE dId=${d_Id}`,(err, result) => {
await db.query(`SELECT pName,pId FROM inchargeof WHERE dId=${d_Id}`, (err, result) => {
if (err) { throw err; }
console.log(typeof result)
res.render('doctor/patientlist',{patients: result})
res.render('doctor/patientlist', { patients: result })
});
} catch (error) {
console.log(error);
}
}
//[GET] /doctor/patient_list/:id
async patientInformation(req, res){
async patientInformation(req, res) {
const patientId = req.params.id;
const d_Id= req.cookies.d_ID;
try{
await db.query(`SELECT * FROM inchargeof WHERE pId=${patientId} AND dId=${d_Id}`,(err, result)=>{
if (err){
const d_Id = req.cookies.d_ID;
try {
await db.query(`SELECT * FROM inchargeof WHERE pId=${patientId} AND dId=${d_Id}`, (err, result) => {
if (err) {
console.log(err)
}
console.log(result)
res.render('doctor/inchargeInformation', {patient: result})
res.render('doctor/inchargeInformation', { patient: result })
})
}catch (error){
} catch (error) {
console.log(error);
}
}

//[GET] /doctor/ getAllAppointments
async getAllAppointment(req, res){
async getAllAppointment(req, res) {
try {
const dId=req.cookies.d_ID;
await db.query(`SELECT name, date, time FROM appointments JOIN patients ON patients.pId=appointments.pId WHERE dId=${dId}`,(err,result)=>{
res.render("doctor/appointmentList",{appointment:result});
} );
}catch (error) {
const dId = req.cookies.d_ID;
await db.query(`SELECT name, date, time FROM appointments JOIN patients ON patients.pId=appointments.pId WHERE dId=${dId}`, (err, result) => {
res.render("doctor/appointmentList", { appointment: result });
});
} catch (error) {
console.log(error);
}
}


//[GET] /doctor/inChargeForm/:id
async inChargeForm(req, result) {
async inChargeForm(req, result) {
try {
const d_ID= req.cookies.d_ID
const d_ID = req.cookies.d_ID
console.log(typeof req.params.id)
await db.query(`SELECT name,pId, phoneNumber FROM patients WHERE patients.pId=${req.params.id}`, (err, res) => {
await db.query(`SELECT name,pId, phoneNumber FROM patients WHERE patients.pId=${req.params.id}`, (err, res) => {
if (err) {
console.log(err)
}
console.log(res);
result.status(200).render('doctor/inChargeForm', {patient: res, d_ID: d_ID});
result.status(200).render('doctor/inChargeForm', { patient: res, d_ID: d_ID });
})
}catch (e) {
} catch (e) {
console.log(e);
}
}

//[POST] /doctor/inChargeForm/:id
async inChargeFormPost(req, res) {
const newCharge={
pId:req.body.pId,
const newCharge = {
pId: req.body.pId,
dId: req.cookies.dId,
pName: req.body.pName,
details: req.body.details,
startDate: req.body.startDate
}
try {
await db.query(`INSERT INTO inchargeof SET ? `,newCharge, (err, res)=>{
await db.query(`INSERT INTO inchargeof SET ? `, newCharge, (err, res) => {

if(err){
console.log(err)
throw err;
}
if (err) {
console.log(err)
throw err;
}
})
res.redirect("/doctor/patientList");
}catch (e) {
} catch (e) {
console.log(e);
}
}

//GET] /doctor/updateInchargeDetails/:id
async updateForm(req,res){
try{
await db.query(`SELECT details FROM inchargeof WHERE inchargeof.pId= ${req.params.id}`, (err, result)=>{
if(err){
async updateForm(req, res) {
try {
await db.query(`SELECT details FROM inchargeof WHERE inchargeof.pId= ${req.params.id}`, (err, result) => {
if (err) {
console.log(err)
throw err;
}
res.render("doctor/updateInchargeDetails",{details:result})
res.render("doctor/updateInchargeDetails", { details: result })
})
}catch (e) {
} catch (e) {
console.log(e);
}
}
//[PATCH] /doctor/updateForm/:id
async updateDetail(req,res){
try{
async updateDetail(req, res) {
try {
const details = req.body.details
const dId=req.cookies.d_ID;
await db.query(`Update inchargeof SET inchargeof.details="${details}" WHERE inchargeof.pId=${req.params.id} AND inchargeof.dId=${dId}`,(err, result)=>{
if (err){
const dId = req.cookies.d_ID;
await db.query(`Update inchargeof SET inchargeof.details="${details}" WHERE inchargeof.pId=${req.params.id} AND inchargeof.dId=${dId}`, (err, result) => {
if (err) {
console.log(err)
throw err
}
res.redirect('/doctor/patientlist')
})
}catch (e) {
} catch (e) {
console.log(e)
}
}
Expand Down
36 changes: 30 additions & 6 deletions server/app/Controller/PatientController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class PatientController {
async showNewAppointmentForm(req, res) {
try {
const patientID = req.cookies.pID;
// const doctors = await this.getAvailableDoctors();
res.render("patient/newAppointment", { patientID: patientID });
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -67,7 +66,7 @@ class PatientController {
await db.query(
`INSERT INTO appointments SET ?`,
newAppointment,
(err, result) => {
(err) => {
if (err) throw err;
res.redirect("/patient/appointments");
}
Expand Down Expand Up @@ -117,13 +116,14 @@ class PatientController {
async editPatientInfo(req, res) {
const { name, age, address, phoneNumber, insuranceNumber, password } =
req.body;
const patientId = req.params.id;
const patientId = req.cookies.id;

try {
await db.query(
`UPDATE patients SET name = ?, age = ?, address = ?, phoneNumber = ?, insuranceNumber = ?, password = ? WHERE id = ?`,
[name, age, address, phoneNumber, insuranceNumber, password, patientId],
(err, result) => {
// `UPDATE patients SET name = ?, age = ?, address = ?, phoneNumber = ?, insuranceNumber = ?, password = ? WHERE id = ?`,
// [name, age, address, phoneNumber, insuranceNumber, password, patientId],
`UPDATE patients SET patients.name = '${name}', patients.age = '${age}', patients.address = '${address}', patients.phoneNumber = '${phoneNumber}', patients.insuranceNumber = '${insuranceNumber}', patients.password = '${password}' WHERE patients.pId = ${patientId}`,
(err) => {
if (err) throw err;
res.redirect("/patient/details/" + patientId);
}
Expand All @@ -132,5 +132,29 @@ class PatientController {
console.log(error);
}
}

// [PATCH] /patient/updateInfo/:id
async updatePatientInfo(req, res) {
const patientId = req.params.id;
const updates = req.body;

const updateFields = Object.keys(updates);
const updateValues = Object.values(updates);

try {
const updateSetClause = updateFields.map(field => `${field} = ?`).join(', ');

await db.query(
`UPDATE patients SET ${updateSetClause} WHERE pId = ?`,
[...updateValues, patientId],
(err, result) => {
if (err) throw err;
res.redirect(`/patientDetails`);
}
);
} catch (error) {
console.log(error);
}
}
}
module.exports = new PatientController();
2 changes: 1 addition & 1 deletion server/routes/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PatientController = require("../app/Controller/PatientController");

route.get("/patientDetails", PatientController.getPatientDetails);
route.get("/patientEdit", PatientController.editPatientForm);
route.patch("/patientEdit/:id", PatientController.editPatientInfo);
route.patch("/updateInfo/:id", PatientController.updatePatientInfo);
route.get("/appointments", PatientController.getPatientAppointments);
route.get("/newAppointment/:id", PatientController.showNewAppointmentForm);
route.post("/newAppointment/:id", PatientController.createNewAppointmentPost);
Expand Down

0 comments on commit 8b2b4f2

Please sign in to comment.