-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// [id].ts: [] in file names for dynamic routes in Next.js | ||
// This endpoint is part of the user profile management feature | ||
// it fetches a user's profile by their ID. | ||
// pages/api/profile/[id].ts | ||
// pages/api/profile/[id].ts | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
const getProfile = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const { id } = req.query; | ||
|
||
try { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: Number(id) }, | ||
}); | ||
|
||
if (user) { | ||
res.status(200).json(user); | ||
} else { | ||
res.status(404).json({ message: 'User not found' }); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ message: 'Internal server error', error }); | ||
} | ||
}; | ||
|
||
export default getProfile; |