Skip to content

Commit 0e0b52b

Browse files
committed
appointment backend integrated
1 parent 38e8d71 commit 0e0b52b

File tree

6 files changed

+375
-105
lines changed

6 files changed

+375
-105
lines changed

back_end/.env

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PORT=5000
2-
DB_USER=ta
3-
DB_PASSWORD=12345678
4-
DB_SERVER=localhost
5-
DB_DATABASE=HMS
2+
DB_USER=sa
3+
DB_PASSWORD=hammad123
4+
DB_SERVER=DESKTOP-C0O50OH
5+
DB_DATABASE=Healvista
6+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const sql = require('mssql');
2+
3+
// Get all patients
4+
const getAllPatients = async (req, res) => {
5+
try {
6+
const request = new sql.Request();
7+
const result = await request.query(`
8+
SELECT
9+
patient_id,
10+
full_name,
11+
date_of_birth,
12+
gender,
13+
contact_info,
14+
registration_date
15+
FROM Patients
16+
ORDER BY full_name
17+
`);
18+
res.json(result.recordset);
19+
} catch (err) {
20+
console.error('Database error:', err);
21+
res.status(500).json({ message: 'Error fetching patients', error: err.message });
22+
}
23+
};
24+
25+
// Search patients by name
26+
const searchPatients = async (req, res) => {
27+
const { name } = req.query;
28+
29+
try {
30+
const request = new sql.Request();
31+
request.input('name', sql.VarChar(100), `%${name}%`);
32+
33+
const result = await request.query(`
34+
SELECT
35+
patient_id,
36+
full_name,
37+
date_of_birth,
38+
gender,
39+
contact_info
40+
FROM Patients
41+
WHERE full_name LIKE @name
42+
ORDER BY full_name
43+
`);
44+
45+
res.json(result.recordset);
46+
} catch (err) {
47+
console.error('Database error:', err);
48+
res.status(500).json({ message: 'Error searching patients', error: err.message });
49+
}
50+
};
51+
52+
// Get patient by ID
53+
const getPatientById = async (req, res) => {
54+
const { id } = req.params;
55+
56+
try {
57+
const request = new sql.Request();
58+
request.input('id', sql.Int, id);
59+
60+
const result = await request.query(`
61+
SELECT
62+
patient_id,
63+
full_name,
64+
date_of_birth,
65+
gender,
66+
contact_info
67+
FROM Patients
68+
WHERE patient_id = @id
69+
`);
70+
71+
if (result.recordset.length === 0) {
72+
return res.status(404).json({ message: 'Patient not found' });
73+
}
74+
75+
res.json(result.recordset[0]);
76+
} catch (err) {
77+
console.error('Database error:', err);
78+
res.status(500).json({ message: 'Error fetching patient', error: err.message });
79+
}
80+
};
81+
82+
// Create new patient
83+
const createPatient = async (req, res) => {
84+
const { full_name, date_of_birth, gender, contact_info } = req.body;
85+
86+
try {
87+
const request = new sql.Request();
88+
request.input('full_name', sql.VarChar(100), full_name);
89+
request.input('date_of_birth', sql.Date, date_of_birth);
90+
request.input('gender', sql.VarChar(10), gender);
91+
request.input('contact_info', sql.VarChar(255), contact_info);
92+
93+
const result = await request.query(`
94+
INSERT INTO Patients (full_name, date_of_birth, gender, contact_info)
95+
OUTPUT INSERTED.patient_id
96+
VALUES (@full_name, @date_of_birth, @gender, @contact_info)
97+
`);
98+
99+
res.status(201).json({
100+
patient_id: result.recordset[0].patient_id,
101+
message: 'Patient created successfully'
102+
});
103+
} catch (err) {
104+
console.error('Database error:', err);
105+
res.status(500).json({ message: 'Error creating patient', error: err.message });
106+
}
107+
};
108+
109+
module.exports = {
110+
getAllPatients,
111+
searchPatients,
112+
getPatientById,
113+
createPatient
114+
};

back_end/controller/appointmentController.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const createAppointment = async (req, res) => {
1616
request.input('appointment_date', sql.DateTime, appointment_date);
1717
request.input('status', sql.VarChar(20), 'Scheduled');
1818

19+
// Create appointment
1920
const result = await request.query(`
2021
INSERT INTO Appointments (
2122
patient_id, doctor_id, appointment_date, status
@@ -28,11 +29,21 @@ const createAppointment = async (req, res) => {
2829

2930
const appointmentId = result.recordset[0].appointment_id;
3031

31-
// Create feedback placeholder
32-
await request.query(`
33-
INSERT INTO Feedback (appointment_id, patient_id)
34-
VALUES (@appointmentId, @patient_id)
35-
`, { appointmentId });
32+
// Create feedback record (without rating)
33+
const feedbackRequest = new sql.Request();
34+
feedbackRequest.input('patient_id', sql.Int, patient_id);
35+
feedbackRequest.input('comments', sql.Text, reason || '');
36+
37+
await feedbackRequest.query(`
38+
INSERT INTO Feedback (
39+
patient_id,
40+
comments
41+
)
42+
VALUES (
43+
@patient_id,
44+
@comments
45+
)
46+
`);
3647

3748
res.status(201).json({
3849
message: 'Appointment created successfully',
@@ -59,12 +70,10 @@ const getAppointmentsByDoctor = async (req, res) => {
5970
p.full_name as patient_name,
6071
a.appointment_date,
6172
a.status,
62-
f.feedback_id,
63-
f.comments,
64-
f.rating
73+
f.comments
6574
FROM Appointments a
6675
JOIN Patients p ON a.patient_id = p.patient_id
67-
LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
76+
LEFT JOIN Feedback f ON a.patient_id = f.patient_id
6877
WHERE a.doctor_id = @doctorId
6978
ORDER BY a.appointment_date DESC
7079
`);
@@ -92,13 +101,11 @@ const getAppointmentsByPatient = async (req, res) => {
92101
d.specialization,
93102
a.appointment_date,
94103
a.status,
95-
f.feedback_id,
96-
f.comments,
97-
f.rating
104+
f.comments
98105
FROM Appointments a
99106
JOIN Doctors d ON a.doctor_id = d.doctor_id
100107
JOIN Users u ON d.user_id = u.user_id
101-
LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
108+
LEFT JOIN Feedback f ON a.patient_id = f.patient_id
102109
WHERE a.patient_id = @patientId
103110
ORDER BY a.appointment_date DESC
104111
`);
@@ -135,36 +142,19 @@ const updateAppointmentStatus = async (req, res) => {
135142

136143
// Submit feedback for appointment
137144
const submitFeedback = async (req, res) => {
138-
const { id } = req.params;
139-
const { comments, rating } = req.body;
145+
const { patientId } = req.params;
146+
const { comments } = req.body;
140147

141148
try {
142149
const request = new sql.Request();
143-
request.input('id', sql.Int, id);
150+
request.input('patient_id', sql.Int, patientId);
144151
request.input('comments', sql.Text, comments);
145-
request.input('rating', sql.Float, rating);
146152

147153
await request.query(`
148154
UPDATE Feedback
149155
SET comments = @comments,
150-
rating = @rating,
151156
submitted_date = GETDATE()
152-
WHERE appointment_id = @id
153-
`);
154-
155-
// Update doctor's feedback score
156-
await request.query(`
157-
UPDATE d
158-
SET d.feedback_score = (
159-
SELECT AVG(CAST(f.rating AS FLOAT))
160-
FROM Feedback f
161-
JOIN Appointments a ON f.appointment_id = a.appointment_id
162-
WHERE a.doctor_id = d.doctor_id
163-
AND f.rating IS NOT NULL
164-
)
165-
FROM Doctors d
166-
JOIN Appointments a ON d.doctor_id = a.doctor_id
167-
WHERE a.appointment_id = @id
157+
WHERE patient_id = @patient_id
168158
`);
169159

170160
res.json({ message: 'Feedback submitted successfully' });
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const express = require('express');
2+
const {
3+
getAllPatients,
4+
searchPatients,
5+
getPatientById,
6+
createPatient
7+
} = require('../controller/app_patient_controller');
8+
9+
const router = express.Router();
10+
11+
router.get('/', getAllPatients);
12+
router.get('/search', searchPatients);
13+
router.get('/:id', getPatientById);
14+
router.post('/', createPatient);
15+
16+
module.exports = router;

back_end/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const pharmacyRoutes = require('./routes/pharmacy_routes');
1313
const labRoutes = require('./routes/lab_routes');
1414
const doctorRoutes = require('./routes/doctorRoutes');
1515
const appointmentRoutes = require('./routes/appointmentRoutes');
16+
const appoint_patient_routes=require('./routes/app_pateint_router');
1617

18+
app.use('/api/appoint_patients',appoint_patient_routes);
1719
app.use('/api/pharmacy', pharmacyRoutes);
1820
app.use('/api/lab', labRoutes);
1921
app.use('/api/doctors', doctorRoutes);

0 commit comments

Comments
 (0)