Skip to content

Commit 3358a0f

Browse files
Merge pull request #16 from ShubhaMahobia/14-fetch-all-doctors
implemented the fetch all doctors API
2 parents b4d2303 + 1dc7cf1 commit 3358a0f

File tree

3 files changed

+104
-49
lines changed

3 files changed

+104
-49
lines changed

controller/authenticationController.js

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const express = require("express");
22
const userPatient = require("../model/UserPatientModel");
33
const bcrypt = require("bcryptjs");
44
const userDoctor = require("../model/UserDoctorModel");
5-
const mongoose = require('mongoose');
6-
5+
const mongoose = require("mongoose");
76

87
//This is a test function to check is the server is running or not.
98
exports.test = (req, res) => {
@@ -39,15 +38,14 @@ exports.isNewUser = async (req, res) => {
3938
//This is the create Profile Function for the user(Patient)
4039
exports.signUpPatient = async (req, res) => {
4140
try {
42-
4341
//Here we are saving the identification number in encrypted form so that it will be secured in our database
4442
const identificationNumber = req.body.identificationNumber;
4543
const hashedIdentificationNumber = await bcrypt.hash(
4644
identificationNumber,
4745
12
4846
);
4947

50-
//Creating patient Object
48+
//Creating patient Object
5149
const patient = new userPatient({
5250
firebaseUserId: req.body.firebaseUserId,
5351
firstName: req.body.firstName,
@@ -82,19 +80,18 @@ exports.signUpPatient = async (req, res) => {
8280
message: "Internal Server Error",
8381
});
8482
}
85-
}
83+
};
8684
//This is the create Profile Function for the user(Doctor)
8785
exports.signUpDoctor = async (req, res) => {
8886
try {
89-
9087
//Here we are saving the identification number in encrypted form so that it will be secured in our database
9188
const identificationNumber = req.body.identificationNumber;
9289
const hashedIdentificationNumber = await bcrypt.hash(
9390
identificationNumber,
9491
12
9592
);
9693

97-
//Creating Doctor Object
94+
//Creating Doctor Object
9895
const doctor = new userDoctor({
9996
firebaseUserId: req.body.firebaseUserId,
10097
firstName: req.body.firstName,
@@ -130,54 +127,62 @@ exports.signUpDoctor = async (req, res) => {
130127
message: "Internal Server Error",
131128
});
132129
}
133-
}
130+
};
134131

135-
//Login Fuction for fetching user Patient details
136-
exports.fetchUserDetails= async (req, res) => {
137-
try {
138-
const email=req.body.email;
139-
if( email==null){
140-
return res.status(400).json({ success:false ,message:"Please provide an email"});
132+
//Login Fuction for fetching user Patient details
133+
exports.fetchUserDetails = async (req, res) => {
134+
try {
135+
const email = req.body.email;
136+
if (email == null) {
137+
return res
138+
.status(400)
139+
.json({ success: false, message: "Please provide an email" });
140+
} else {
141+
const user = await userPatient.findOne({ Email: email });
142+
if (!user) {
143+
return res.status(401).json({
144+
success: false,
145+
message: "Authentication failed! Email not found.",
146+
});
147+
} else {
148+
return res
149+
.status(401)
150+
.json({ success: true, data: user, message: "User success" });
151+
}
141152
}
142-
else{
143-
const user=await userPatient.findOne({Email:email});
144-
if(!user){
145-
return res.status(401).json({success:false,message:"Authentication failed! Email not found."});
146-
}
147-
else{
148-
return res.status(401).json({success:true,data: user, message:"User success"});
149-
}
150-
}
151-
152-
153153
} catch (err) {
154-
return res.status(500).json({success:false,message:"Internal Server Error"});
155-
154+
return res
155+
.status(500)
156+
.json({ success: false, message: "Internal Server Error" });
156157
}
157-
}
158-
158+
};
159159

160160
//Login Fuction for fetching user Doctor details
161-
exports.fetchDoctorDetails= async (req, res) => {
162-
try {
163-
const email=req.body.email;
164-
if( email==null){
165-
return res.status(400).json({ success:false ,message:"Please provide an email"});
161+
exports.fetchDoctorDetails = async (req, res) => {
162+
try {
163+
const email = req.body.email;
164+
if (email == null) {
165+
return res
166+
.status(400)
167+
.json({ success: false, message: "Please provide an email" });
168+
} else {
169+
const user = await userDoctor.findOne({ Email: email });
170+
if (!user) {
171+
return res.status(401).json({
172+
success: false,
173+
message: "Authentication failed! Email not found.",
174+
});
175+
} else {
176+
return res
177+
.status(401)
178+
.json({ success: true, data: user, message: "User success" });
179+
}
166180
}
167-
else{
168-
const user=await userDoctor.findOne({Email:email});
169-
if(!user){
170-
return res.status(401).json({success:false,message:"Authentication failed! Email not found."});
171-
}
172-
else{
173-
return res.status(401).json({success:true,data: user, message:"User success"});
174-
}
175-
}
176-
177-
178181
} catch (err) {
179-
return res.status(500).json({success:false,message:"Internal Server Error"});
180-
182+
return res
183+
.status(500)
184+
.json({ success: false, message: "Internal Server Error" });
181185
}
182-
}
186+
};
187+
183188

controller/doctorController.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const express = require("express");
2+
const bcrypt = require("bcryptjs");
3+
const userDoctor = require("../model/UserDoctorModel");
4+
const mongoose = require("mongoose");
5+
6+
//Function for fetching all the doctors
7+
exports.fetchAllDoctors = async (req, res) => {
8+
try {
9+
const allDoctors = await userDoctor.find({});
10+
return res.status(200).json({
11+
success: true,
12+
message: "Details fetched successfully",
13+
data: allDoctors,
14+
});
15+
} catch (err) {
16+
return res
17+
.status(500)
18+
.json({ success: false, message: "Internal Server Error" });
19+
}
20+
};
21+
22+
// Function to fetch a doctor by ID (firebaseId)
23+
exports.fetchDoctorById = async (req, res) => {
24+
try {
25+
const doctorId = req.params.id; // Assuming the ID is passed as a route parameter
26+
const doctor = await userDoctor.findOne({ firebaseUserId: doctorId }); // Changed to findOne with firebaseUserId
27+
if (!doctor) {
28+
return res
29+
.status(404)
30+
.json({ success: false, message: "Doctor not found" });
31+
}
32+
33+
return res.status(200).json({
34+
success: true,
35+
message: "Doctor details fetched successfully",
36+
data: doctor,
37+
});
38+
} catch (err) {
39+
return res
40+
.status(500)
41+
.json({ success: false, message: "Internal Server Error" });
42+
}
43+
};

routes/authenticationRoutes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const {
88
fetchUserDetails,
99
fetchDoctorDetails,
1010
} = require("../controller/authenticationController");
11+
12+
const {
13+
fetchAllDoctors,
14+
fetchDoctorById,
15+
} = require("../controller/doctorController");
1116
const router = express.Router();
1217

1318
router.get("/", test);
@@ -16,5 +21,7 @@ router.post("/checkNewUser", isNewUser);
1621
router.post("/registerDoctor", signUpDoctor);
1722
router.post("/getUser", fetchUserDetails);
1823
router.post("/getDoctor", fetchDoctorDetails);
24+
router.get("/getAllDoctors", fetchAllDoctors);
25+
router.get("/getDoctorById/:id", fetchDoctorById);
1926

20-
module.exports = router;
27+
module.exports = router;

0 commit comments

Comments
 (0)