1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Is there a way I can access all the patients in the database?

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 7, 2024 às 04:52.

  1. Stack

    Stack Membro Participativo

    I have a hospital_db in my workbench and I am trying to access all the patients from my backend using nodejs but I still can't access the data

    I created some routes for the patients in the database but I cannot access the data from my backend. I am getting an error (canceled) from the api.

    App.js

    const express = require('express');
    const bodyParser = require('body-parser');
    const dotenv = require('dotenv');

    dotenv.config()

    const app = express();

    app.use(bodyParser.json());

    const patientRoutes = require('./routes/patients')
    const doctorRoutes = require('./routes/doctors')
    const appointmentRoutes = require('./routes/appointments')
    const adminRoutes = require('./routes/admin')

    app.use('/patients', patientRoutes);
    app.use('/doctors', doctorRoutes);
    app.use('/appointments', appointmentRoutes);
    app.use('/admin', adminRoutes);

    const PORT = process.env.PORT || 5000
    app.listen(PORT, () => {
    console.log(`listening on port ${PORT}`);
    })


    routes/patients

    const express = require('express');
    const router = express.Router();
    const patientController = require('../controllers/patientController');
    const adminAuth = require('../middleware/adminAuth')
    const auth = require('../middleware/auth')

    router.get('/', adminAuth, patientController.getPatients)

    // register
    router.post('/register', patientController.registerPatient)

    // update
    router.put('/profile', auth, patientController.updatePatient)

    // delete
    router.delete('/profile', auth, patientController.deletePatient)

    module.exports = router;


    controllers/patientController

    const db = require('../config/db');
    const bcrypt = require('bcrypt');

    // Get all patients (Admin only)
    exports.getPatients = async (req, res) => {
    const { search, gender, date_of_birth } = req.query; // Optional filters

    let query = 'SELECT id, first_name, last_name, gender, date_of_birth FROM Patients';
    let queryParams = [];

    // Filters for searching
    if (search) {
    query += ' WHERE (first_name LIKE ? OR last_name LIKE ? OR email LIKE ?)';
    queryParams.push(`%${search}%`, `%${search}%`, `%${search}%`);
    }

    if (gender) {
    query += query.includes('WHERE') ? ' AND' : ' WHERE';
    query += ' gender = ?';
    queryParams.push(gender);
    }

    if (date_of_birth) {
    query += query.includes('WHERE') ? ' AND' : ' WHERE';
    query += ' date_of_birth = ?';
    queryParams.push(date_of_birth);
    }

    try {
    const [patients] = await db.query(query, queryParams);
    res.json(patients);
    } catch (err) {
    console.error(err);
    res.status(500).json({ message: 'Server error' });
    }
    };

    // Register a new patient
    exports.registerPatient = async (req, res) => {
    const { first_name, last_name, email, password, phone, date_of_birth, gender, address } = req.body;

    try {
    // Check if patient already exists
    const [existingPatient] = await db.query('SELECT * FROM Patients WHERE email = ?', );
    if (existingPatient.length > 0) {
    return res.status(400).json({ message: 'Email already exists' });
    }

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

    // Insert the new patient
    await db.query(
    'INSERT INTO Patients (first_name, last_name, email, password_hash, phone, date_of_birth, gender, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
    [first_name, last_name, email, hashedPassword, phone, date_of_birth, gender, address]
    );

    res.status(201).json({ message: 'Registration successful' });
    } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Server error' });
    }
    };

    // Update patient profile
    exports.updatePatient = async (req, res) => {
    const { first_name, last_name, phone, date_of_birth, gender, address } = req.body;

    try {
    await db.query(
    'UPDATE Patients SET first_name = ?, last_name = ?, phone = ?, date_of_birth = ?, gender = ?, address = ? WHERE id = ?',
    [first_name, last_name, phone, date_of_birth, gender, address, req.user.id]
    );
    res.json({ message: 'Profile updated successfully' });
    } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Server error' });
    }
    };

    // Delete a patient account
    exports.deletePatient = async (req, res) => {
    try {
    await db.query('DELETE FROM Patients WHERE id = ?', [req.user.id]);
    res.json({ message: 'Account deleted successfully' });
    } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Server error' });
    }
    };

    [url="https://stackoverflow.com/questions/79061052/is-there-a-way-i-can-access-all-the-patients-in-the-database"]Continue reading...[/url]

Compartilhe esta Página