Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • AF7626/hospital-management-system
1 result
Show changes
Commits on Source (4)
File added
###
curl -X POST http://localhost:3000/api/v1/admin/add-doctor \
-H "Content-Type: application/json" \
-d '{
"name": "Dr. John Doe",
"email": "johndoe@example.com",
"password": "securepassword",
"image": "doctor-image-url",
"speciality": "Cardiologist",
"degree": "MD",
"experience": "10 years",
"about": "Experienced heart specialist",
"available": true,
"fees": 200,
"slots_booked": {},
"address": { "city": "New York", "street": "123 Main St" },
"date": 20250210
}'
\ No newline at end of file
...@@ -69,3 +69,10 @@ ...@@ -69,3 +69,10 @@
## 13. Emergency Assistance ## 13. Emergency Assistance
- Users can contact emergency services or request emergency medical assistance directly from the app in case of critical situations. - Users can contact emergency services or request emergency medical assistance directly from the app in case of critical situations.
User Management
POST /users/register → Register new users (patients, doctors, admins)
POST /users/login → Authenticate users
GET /users/:id → Get user details
PUT /users/:id → Update user profile
import app from './app.js';
Deno.serve(app.fetch);
\ No newline at end of file
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
import * as userController from "./userController.js";
const app = new Hono();
app.get("/v1/users", userController.showForm);
app.get("/v1/users/:id", userController.showUser);
app.post("/v1/users", userController.createUser);
app.post("/v1/users/:id", userController.updateUser);
app.post("/v1/users/:id/delete", userController.deleteUser);
export default app;
import mongoose from "mongoose";
//function to connect to mangoDB database
const connectDB = async () => {
mongoose.connection.on('connected', () => console.log("Database connected to application"))
await mongoose.connect(`${process.env.MONGODB_URI}/unix-doctors`)
}
export default connectDB;
\ No newline at end of file
// import { addDoctorService } from '../services/adminService.js'; // Import the service
import validator from "validator";
import bcrypt from "bcrypt";
import doctorModel from '../models/doctorModel.js';
// function to add doctor in the database
const addDoctor = async (req, res) => {
try {
const { name, email, password, speciality, degree, experience, about, fees, address } = req.body;
console.log(req.body)
if (!name || !email || !password || !speciality || !degree || !experience || !about || !fees || !address) {
return res.status(201).json({ success: false, message: "Missing Details!" });
}
// validating email format
if (!validator.isEmail(email)) {
return res.json({ success: false, message: "Please enter a valid email" })
}
// validating strong password
if (password.length < 8) {
return res.json({ success: false, message: "Please enter a strong password" })
}
// hashing user password
const salt = await bcrypt.genSalt(5); // the more no. round the more time it will take
const hashedPassword = await bcrypt.hash(password, salt)
//save the data to database this has to have its own file called adminService
const doctorData = {
name,
email,
password: hashedPassword,
speciality,
degree,
experience,
about,
fees,
address,
date: Date.now()
}
const newDoctor = new doctorModel(doctorData)
await newDoctor.save()
res.json({ success: true, message: 'Doctor Added' })
// // Call the service function to handle doctor creation
// const response = await addDoctorService({ name, email, password, speciality, degree, experience, about, fees, address });
// res.json(response); // Send the response from the service
} catch (error) {
console.log(error);
res.status(500).json({ success: false, message: error.message });
}
};
export { addDoctor };
\ No newline at end of file
import mongoose from 'mongoose';
const doctorSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
speciality : { type: String, required: true },
degree: { type: String, required: true },
experience: { type: String, required: true },
about: { type: String, required: true },
available: { type: Boolean, default: true },
fees: { type: Number, required: true },
slots_booked: { type: Object, default: {} },
address: { type: Object, required: true },
date: { type: Number, required: true },
}, { minimize: false })
const doctorModel = mongoose.models.doctor || mongoose.model("doctor", doctorSchema);
export default doctorModel;
\ No newline at end of file
import mongoose from "mongoose";
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
phone: { type: String, default: '+358423077981' },
address: { type: Object, default: { line1: '', line2: '' } },
gender: { type: String, default: 'Not Selected' },
dob: { type: String, default: 'Not Selected' },
password: { type: String, required: true },
})
const userModel = mongoose.models.user || mongoose.model("user", userSchema);
export default userModel;
\ No newline at end of file
{
"name": "backend",
"version": "1.0.0",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"bcrypt": "^5.1.1",
"cloudinary": "^2.5.1",
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.9",
"validator": "^13.12.0"
}
}
import express from 'express';
import { addDoctor } from '../controllers/adminController.js';
// Create a new router instance for handling admin-related routes
const adminRouter = express.Router();
// This handles POST requests to the endpoint: http://localhost:3000/api/v1/admin/add-doctor
adminRouter.post('/add-doctor', addDoctor);
export default adminRouter;
import connectDB from './config/mongodb.js';
import express from 'express';
import cors from 'cors';
import 'dotenv/config';
import adminRouter from './routes/adminRoute.js';
const app = express();
const port = process.env.port || 3000;
connectDB();
// middlewares
// Middleware to parse JSON request bodies
app.use(express.json());
app.use(cors());
// Define API endpoints using base URL: http://localhost:3000/api/v1/admin
// This means all routes inside `adminRouter` will be prefixed with `/api/v1/admin`
app.use("/api/v1/admin", adminRouter);
app.get("/", (req,res) => {
res.send("Hello its time to ride hello");
})
app.listen(port, () => {
console.log("server is running",port);
})
//npm start dev
\ No newline at end of file
import doctorModel from '../models/doctorModel.js';
import bcrypt from "bcrypt";
//function to for service layer
const addDoctorService = async ({ name, email, password, speciality, degree, experience, about, fees, address }) => {
try {
// hashing user password
const salt = await bcrypt.genSalt(5); // the more rounds, the more time it will take
const hashedPassword = await bcrypt.hash(password, salt);
// Save the data to database
const doctorData = {
name,
email,
password: hashedPassword,
speciality,
degree,
experience,
about,
fees,
address,
date: Date.now(),
};
const newDoctor = new doctorModel(doctorData);
await newDoctor.save();
return { success: true, message: 'Doctor Added' };
} catch (error) {
console.log(error);
return { success: false, message: error.message };
}
};
export { addDoctorService };
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<form method="POST" action="/v1/users/<%= it.user.id %>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<%= it.user.name %>" /><br/>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<%= it.user.email %>" /><br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="<%= it.user.isbn %>" /><br/>
<input type="submit" value="Update" />
</form>
<p><a href="/v1/users">Back to users</a></p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register User</h2>
<form method="POST" action="/v1/users">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required /><br/>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required /><br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required /><br/>
<input type="submit" value="Register" />
</form>
<ul>
<h2>Existing Users</h2>
<ul>
<% it.users.forEach((user) => { %>
<li>
<a href="/v1/users/<%= user.id %>"><%= user.name %> (<%= user.email %>)</a>
<form method="POST" action="/v1/users/<%= user.id %>/delete" style="display:inline;">
<input type="submit" value="Delete" />
</form>
</li>
<% }); %>
</ul>
</body>
</html>
\ No newline at end of file
import { Eta } from "https://deno.land/x/eta@v3.4.0/src/index.ts";
import * as userService from "./userService.js";
const eta = new Eta({ views: `${Deno.cwd()}/templates/` });
const showForm = async (c) => {
return c.html(
eta.render("users.eta", { users: await userService.listUsers() }),
);
};
const createUser = async (c) => {
const body = await c.req.parseBody();
await userService.createUser(body);
return c.redirect("/v1/users");
};
const showUser = async (c) => {
const id = c.req.param("id");
return c.html(
eta.render("user.eta", { user: await userService.getUser(id) }),
);
};
const updateUser = async (c) => {
const id = c.req.param("id");
const body = await c.req.parseBody();
await userService.updateUser(id, body);
return c.redirect(`/v1/users/${id}`);
};
const deleteUser = async (c) => {
const id = c.req.param("id");
await userService.deleteUser(id);
return c.redirect("/v1/users");
}
export { createUser, showForm, showUser, updateUser,deleteUser };
\ No newline at end of file
const createUser = async (user) => {
user.id = crypto.randomUUID();
const kv = await Deno.openKv();
await kv.set(["users", user.id], user);
};
const listUsers = async () => {
const kv = await Deno.openKv();
const userEntries = await kv.list({ prefix: ["users"] });
const users = [];
for await (const entry of userEntries) {
users.push(entry.value);
}
return users;
};
const getUser = async (id) => {
const kv = await Deno.openKv();
const user = await kv.get(["users", id]);
return user?.value ?? {};
};
const updateUser = async (id, user) => {
user.id = id;
const kv = await Deno.openKv();
await kv.set(["users", id], user);
};
const deleteUser = async (id) => {
const kv = await Deno.openKv();
await kv.delete(["users",id]);
}
export {createUser,listUsers,getUser,updateUser,deleteUser}
\ No newline at end of file