aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/examples/with-prisma/apps/web/pages/api/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'examples/with-prisma/apps/web/pages/api/users.ts')
-rw-r--r--examples/with-prisma/apps/web/pages/api/users.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/with-prisma/apps/web/pages/api/users.ts b/examples/with-prisma/apps/web/pages/api/users.ts
new file mode 100644
index 0000000..3f7b3f0
--- /dev/null
+++ b/examples/with-prisma/apps/web/pages/api/users.ts
@@ -0,0 +1,34 @@
+import { prisma } from "database";
+
+import type { NextApiRequest, NextApiResponse } from "next";
+
+/**
+ * Users
+ *
+ * @description A basic API endpoint to retrieve all the users in the database
+ */
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method !== "GET") {
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+
+ try {
+ const users = await prisma.user.findMany();
+ if (!users)
+ throw {
+ message: "Failed to retrieve users",
+ status: 500,
+ };
+
+ return res.status(200).json({
+ users,
+ });
+ } catch ({ message = "An unknown error occured", status = 500 }) {
+ console.error({ message, status });
+ return res.status(status).end(message);
+ }
+}