mocksaml/pages/api/users/[id].ts

35 lines
663 B
TypeScript
Raw Normal View History

2022-01-09 07:28:39 +00:00
import { PrismaClient } from '@prisma/client';
import type { NextApiRequest, NextApiResponse } from 'next';
const prisma = new PrismaClient();
type User = {
id: number;
first_name: string;
last_name: string;
email: string;
};
2022-01-13 17:07:44 +00:00
const getUserById = async (id: number): Promise<User | null> => {
2022-01-09 07:28:39 +00:00
return await prisma.user.findUnique({
where: {
id,
},
});
};
export default async function handler(
req: NextApiRequest,
2022-01-13 17:07:44 +00:00
res: NextApiResponse<User | null>
2022-01-09 07:28:39 +00:00
) {
const { method } = req;
if (method === 'GET') {
const { id } = req.query;
const user = await getUserById(Number(id));
return res.status(200).json(user);
}
}