diff --git a/server/app/Controller/DoctorController.js b/server/app/Controller/DoctorController.js index 5c39c4a..6280a23 100644 --- a/server/app/Controller/DoctorController.js +++ b/server/app/Controller/DoctorController.js @@ -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", @@ -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) } } diff --git a/server/app/Controller/PatientController.js b/server/app/Controller/PatientController.js index 6f27e9b..84375e8 100644 --- a/server/app/Controller/PatientController.js +++ b/server/app/Controller/PatientController.js @@ -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); @@ -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"); } @@ -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); } @@ -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(); diff --git a/server/routes/patient.js b/server/routes/patient.js index d77eb4d..9f8a800 100644 --- a/server/routes/patient.js +++ b/server/routes/patient.js @@ -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);