Code cleanup
This commit is contained in:
parent
9b86ea8b2e
commit
d344fcb1d2
@ -1,31 +0,0 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
|
|
||||||
type User = {
|
|
||||||
id: number;
|
|
||||||
first_name: string;
|
|
||||||
last_name: string;
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getUserById = async (id: number): Promise<User | null> => {
|
|
||||||
return await prisma.user.findUnique({
|
|
||||||
where: {
|
|
||||||
id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<User | null>
|
|
||||||
) {
|
|
||||||
const { method } = req;
|
|
||||||
|
|
||||||
if (method === 'GET') {
|
|
||||||
const { id } = req.query;
|
|
||||||
|
|
||||||
const user = await getUserById(Number(id));
|
|
||||||
|
|
||||||
return res.status(200).json(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,37 +0,0 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
|
|
||||||
type User = {
|
|
||||||
id: number,
|
|
||||||
first_name: string,
|
|
||||||
last_name: string,
|
|
||||||
email: string,
|
|
||||||
};
|
|
||||||
|
|
||||||
const createUser = async (body: Omit<User, 'id'>): Promise<User> => {
|
|
||||||
return await prisma.user.create({ data: body });
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchAllUsers = async (): Promise<User[]> => {
|
|
||||||
return await prisma.user.findMany();
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse<User | User[]>
|
|
||||||
) {
|
|
||||||
const { method } = req;
|
|
||||||
|
|
||||||
if (method === 'GET') {
|
|
||||||
const users = await fetchAllUsers();
|
|
||||||
|
|
||||||
return res.status(200).json(users);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method === 'POST') {
|
|
||||||
const { first_name, last_name, email } = req.body;
|
|
||||||
|
|
||||||
const user = await createUser({ first_name, last_name, email });
|
|
||||||
|
|
||||||
return res.status(200).json(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user