From 83be62bde2b01aff7a3c263691f3914554324f80 Mon Sep 17 00:00:00 2001 From: Joseph J Helfenbein Date: Sat, 25 Jan 2025 08:23:53 -0500 Subject: [PATCH] change endpoint structure --- src/app/api/patients.js | 22 +++++-- src/app/api/patients/[email].js | 66 +++++++++++++------- src/app/api/user.js | 107 +++++++++++++++++++++----------- 3 files changed, 133 insertions(+), 62 deletions(-) diff --git a/src/app/api/patients.js b/src/app/api/patients.js index d8fd75b..d7dd24d 100644 --- a/src/app/api/patients.js +++ b/src/app/api/patients.js @@ -1,9 +1,21 @@ -import User from '../../models/User'; +import { User } from '../../models/User'; import { connectDB } from '../../lib/utils'; +import { NextResponse } from 'next/server'; + +export async function GET() { + console.log('Fetching patients...'); -export default async (req, res) => { await connectDB(); - const patients = await User.find({ role: 'patient' }); - res.json(patients); -}; \ No newline at end of file + try { + const patients = await User.find({ role: 'patient' }); + console.log('Fetched patients:', patients); + return NextResponse.json(patients, { status: 200 }); + } catch (error) { + console.error('Error fetching patients:', error); + return NextResponse.json( + { message: 'Internal server error' }, + { status: 500 } + ); + } +} diff --git a/src/app/api/patients/[email].js b/src/app/api/patients/[email].js index c4003a2..98dc78a 100644 --- a/src/app/api/patients/[email].js +++ b/src/app/api/patients/[email].js @@ -1,28 +1,52 @@ -import User from '../../../models/User'; -import connectDB from '../../../lib/utils'; +import { User } from '../../../models/User'; +import { connectDB } from '../../../lib/utils'; +import { NextResponse } from 'next/server'; -export default async (req, res) => { +export async function PUT(req) { + console.log('Processing patient update request...'); + await connectDB(); - const { email } = req.query; - const { medications, medicalConditions } = req.body; + try { + const url = new URL(req.url); + const email = url.searchParams.get('email'); - console.log('Received request to update patient with email:', email); - console.log('Medications:', medications); - console.log('Medical Conditions:', medicalConditions); + if (!email) { + console.log('No email provided'); + return NextResponse.json( + { message: 'Email is required' }, + { status: 400 } + ); + } - const patient = await User.findOne({ email }); + const body = await req.json(); + const { medications, medicalConditions } = body; - if (!patient) { - console.log('Patient not found for email:', email); - return res.status(404).json({ message: 'Patient not found' }); + console.log('Updating patient with email:', email); + console.log('Medications:', medications); + console.log('Medical Conditions:', medicalConditions); + + const patient = await User.findOne({ email }); + + if (!patient) { + console.log('Patient not found for email:', email); + return NextResponse.json( + { message: 'Patient not found' }, + { status: 404 } + ); + } + + patient.medications = medications; + patient.medicalConditions = medicalConditions; + await patient.save(); + + console.log('Patient data updated successfully:', patient); + return NextResponse.json(patient, { status: 200 }); + } catch (error) { + console.error('Error updating patient data:', error); + return NextResponse.json( + { message: 'Internal server error' }, + { status: 500 } + ); } - - patient.medications = medications; - patient.medicalConditions = medicalConditions; - await patient.save(); - - console.log('Patient data updated successfully:', patient); - - res.json(patient); -}; \ No newline at end of file +} diff --git a/src/app/api/user.js b/src/app/api/user.js index 1c356c5..bda0ffa 100644 --- a/src/app/api/user.js +++ b/src/app/api/user.js @@ -1,38 +1,73 @@ -import User from '../../models/User'; +import { User } from '../../models/User'; import { connectDB } from '../../lib/utils'; +import { NextResponse } from 'next/server'; -export default async (req, res) => { - const { userId } = req.query; - - console.log('Received request with userId:', userId); - - if (!userId) { - console.log('No userId provided'); - return res.status(401).json({ message: 'Unauthorized' }); - } - - await connectDB(); - - const user = await User.findOne({ id: userId }); - - if (!user) { - console.log('User not found for userId:', userId); - return res.status(404).json({ message: 'User not found' }); - } - - if (req.method === 'GET') { - console.log('Returning user data:', user); - res.json(user); - } else if (req.method === 'PUT') { - const { role } = req.body; - console.log('Updating user role to:', role); - user.role = role; - await user.save(); - console.log('User role updated successfully'); - res.json(user); - } else { - console.log('Method not allowed:', req.method); - res.status(405).json({ message: 'Method not allowed' }); - } - }; - \ No newline at end of file +export async function GET(req) { + const userId = req.nextUrl.searchParams.get('userId'); + + console.log('Received GET request with userId:', userId); + + if (!userId) { + console.error('No userId provided'); + return NextResponse.json( + { message: 'Unauthorized' }, + { status: 401 } + ); + } + + await connectDB(); + + const user = await User.findOne({ id: userId }); + + if (!user) { + console.error('User not found for userId:', userId); + return NextResponse.json( + { message: 'User not found' }, + { status: 404 } + ); + } + + console.log('Returning user data:', user); + return NextResponse.json(user, { status: 200 }); +} + +export async function PUT(req) { + const userId = req.nextUrl.searchParams.get('userId'); + const { role } = await req.json(); + + console.log('Received PUT request with userId:', userId, 'and role:', role); + + if (!userId) { + console.error('No userId provided'); + return NextResponse.json( + { message: 'Unauthorized' }, + { status: 401 } + ); + } + + await connectDB(); + + const user = await User.findOne({ id: userId }); + + if (!user) { + console.error('User not found for userId:', userId); + return NextResponse.json( + { message: 'User not found' }, + { status: 404 } + ); + } + + if (!role) { + console.error('No role provided in request body'); + return NextResponse.json( + { message: 'Role is required' }, + { status: 400 } + ); + } + + user.role = role; + await user.save(); + + console.log('User role updated successfully'); + return NextResponse.json(user, { status: 200 }); +}