Skip to content

Commit

Permalink
Create Customer and delete Customer
Browse files Browse the repository at this point in the history
  • Loading branch information
sujal1256 committed Oct 17, 2024
1 parent 5f0a518 commit e2a203d
Show file tree
Hide file tree
Showing 8 changed files with 791 additions and 226 deletions.
51 changes: 38 additions & 13 deletions Backend/api/controllers/PatientController.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
// controllers/patientController.js
import { Patient } from '../models/Patient.js';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { Patient } from "../models/Patient.js";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";

// Register a new patient
export const registerPatient = async (req, res) => {
try {
const { name, email, gender, mobile, password } = req.body;
const { name, email, gender, mobile, password, age} = req.body;

console.log(req.body);

// Check if the patient already exists
const existingPatient = await Patient.findOne({ email });
if (existingPatient) {
return res.status(400).json({ message: 'Patient already exists' });
return res.status(400).json({ message: "Patient already exists" });
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Create a new patient
const newPatient = new Patient({
const newPatient = await Patient.create({
name,
email,
gender,
mobile,
age,
password: hashedPassword,
});

await newPatient.save();
res.status(201).json({ message: 'Patient registered successfully' });
res.status(201).json({ message: "Patient registered successfully" });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error', error });
res.status(500).json({ message: "Server error", error });
}
};

export const getAllPatients = async (req, res) => {
const patients = await Patient.find();
return res.status(200).json({ data: patients });
};
// Login a patient
export const loginPatient = async (req, res) => {
try {
Expand All @@ -42,21 +48,40 @@ export const loginPatient = async (req, res) => {
// Find the patient by email
const patient = await Patient.findOne({ email });
if (!patient) {
return res.status(400).json({ message: 'Invalid credentials' });
return res.status(400).json({ message: "Invalid credentials" });
}

// Compare the passwords
const isMatch = await bcrypt.compare(password, patient.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials' });
return res.status(400).json({ message: "Invalid credentials" });
}

// Generate a JWT token
const token = jwt.sign({ id: patient._id }, process.env.JWT_SECRET, { expiresIn: '1h' });
const token = jwt.sign({ id: patient._id }, process.env.JWT_SECRET, {
expiresIn: "1h",
});

res.status(200).json({ token });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Server error', error });
res.status(500).json({ message: "Server error", error });
}
};

export const deletePatient = async (req, res) => {
const {patientId} = req.body;

const patient = await Patient.deleteOne({_id: patientId});

console.log(patient);

if(!patient){
return res.status(400).json({
success: false,
message: "Patient not found"
})
}

return res.status()
}
4 changes: 4 additions & 0 deletions Backend/api/models/Patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const patientSchema = mongoose.Schema({
type: String,
required: true,
},
age: {
type: Number,
require: true
}
}, { timestamps: true });

export const Patient = mongoose.model('Patient', patientSchema);
6 changes: 5 additions & 1 deletion Backend/api/routes/PatientRoute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// routes/patientRoutes.js
import express from 'express';
import { registerPatient, loginPatient } from '../controllers/PatientController.js';
import { registerPatient, loginPatient, getAllPatients, deletePatient } from '../controllers/PatientController.js';

const router = express.Router();

Expand All @@ -10,4 +10,8 @@ router.post('/register', registerPatient);
// Route for logging in a patient
router.post('/login', loginPatient);

router.get("/get-patients", getAllPatients);

router.post("/delete-patient", deletePatient);

export default router;
1 change: 1 addition & 0 deletions Frontend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASE_URL =
Loading

0 comments on commit e2a203d

Please sign in to comment.