Skip to content

Commit

Permalink
admin and Hod page backend works
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanPrajapati7015 committed Nov 3, 2024
1 parent b6b8ade commit 3b2e561
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 80 deletions.
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MONGO_URI =mongodb://0.0.0.0/hodDatabase
MONGO_URI =mongodb+srv://opdadmin:[email protected]/OPD-staff
2 changes: 2 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ app.get('/', (req, res) => {
})

app.use('/admin', adminRoutes);
app.use('/doctor', hodRoutes);



app.listen(port, () => {
Expand Down
1 change: 1 addition & 0 deletions backend/config/hodDatabase.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
const mongoose = require('mongoose');

function connectTodB(){
Expand Down
2 changes: 1 addition & 1 deletion backend/db.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const mongoose = require('mongoose');

mongoose.connect("mongodb://localhost:27017/")
mongoose.connect("mongodb+srv://opdadmin:[email protected]/OPD-staff")
/**
mongodb+srv://opdadmin:[email protected]/
*/
Expand Down
182 changes: 167 additions & 15 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"license": "ISC",
"dependencies": {
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"jsonwebtoken": "^9.0.2",
"cors": "^2.8.5",
"express": "^4.21.1"
"mongoose": "^8.8.0",
"zod": "^3.23.8"
}
}
88 changes: 38 additions & 50 deletions backend/routes/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,66 @@ const router = express.Router();
const zod = require("zod");
const { HOD } = require('../db');

const HODBody = zod.object({
username : zod.string(),
email : zod.string().email(),
password : zod.string(),
department : zod.string(),
room_no : zod.number(),
availability : zod.boolean()
})


router.post('/add', async (req, res) => {
const { success } = HODBody.safeParse(req.body)
if (!success) {
return res.status(411).json({
message: "Enter correct inputs"

try {
const findHOD = await HOD.findOne({
email: req.body.email
})
}
try{
const findHOD = await HOD.findOne({
email : req.body.email
})

if(findHOD==null){
const createdHOD = await HOD.create({
username: req.body.username,
email : req.body.email,
password: req.body.password,
department : req.body.department,
room_no : req.body.room_no,
availability : req.body.availability
})
res.status(200).json({
message: "HOD added successfully",
HODid : createdHOD._id
})
}
else{
res.json({
msg : "Have a HOD with same mail"
})
}
} catch (error) {
res.status(500).json({ message: 'Error while Adding HOD', error });
}
if (findHOD == null) {
const createdHOD = await HOD.create({
username: req.body.username,
email: req.body.email,
password: req.body.password,
department: req.body.department,
room_no: req.body.room_no,
availability: req.body.availability
})
res.status(200).json({
message: "HOD added successfully",
HODid: createdHOD._id
})
}
else {
res.json({
msg: "Have a HOD with same mail"
})
}
} catch (error) {
res.status(500).json({ message: 'Error while Adding HOD', error });
}

});


router.delete('/delete/:id', async (req, res) => {
try {
const { id } = req.params;

// Find and delete the item by ID
const deletedItem = await HOD.findByIdAndDelete(id);

if (!deletedItem) {
return res.status(404).json({ message: 'HOD not found' });
}

res.status(200).json({ message: 'HOD removed successfully', deletedItem });
} catch (error) {
res.status(500).json({ message: 'Error while removing HOD', error });
}
});

router.get('/',async (req, res) => {
try{
const HODs = await HOD.find({},'username department')
res.status(200).json(HODs);
} catch (error) {
res.status(500).json({ message: 'Error fetching user details', error });
}
router.get('/', async (req, res) => {
try {
const HODs = await HOD.find({}, 'username department')
res.status(200).json(HODs);

} catch (error) {
res.status(500).json({ message: 'Error fetching user details', error });
}
});

module.exports = router;
Loading

0 comments on commit 3b2e561

Please sign in to comment.