This commit is contained in:
Timur Ercan
2023-01-07 15:11:15 +01:00
parent 256c518cbc
commit cfaffaa84c
4 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1 @@
POST for signing (using special body)

View File

@ -0,0 +1,49 @@
import { PrismaClient } from "@documenso/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
export default async function userHandler(
req: NextApiRequest,
res: NextApiResponse
) {
const { method, body } = req;
const prisma = new PrismaClient();
// Check Session
switch (method) {
case "POST":
if (!body.userId) {
res.status(400).end("Owner ID cannot be empty.");
}
try {
const newDocument: any = await prisma.document
.create({
data: { userId: body.userId, document: body.document },
})
.then(async () => {
await prisma.$disconnect();
res.status(200).send(newDocument);
});
} catch (error) {
await prisma.$disconnect();
res.status(500).end("An error has occured.");
}
break;
case "GET":
// GET all docs for user in session
let documents = await prisma.document.findMany({
where: {
userId: body.userId,
},
});
res.status(200).send(documents);
break;
default:
res.setHeader("Allow", ["GET", "POST"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
}

View File

@ -0,0 +1,42 @@
// POST to create
import PrismaClient from "@documenso/prisma";
import User from "@documenso/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { json } from "stream/consumers";
export default async function userHandler(
req: NextApiRequest,
res: NextApiResponse<User>
) {
const { method, body } = req;
const prisma = new PrismaClient();
switch (method) {
case "POST":
if (!body.email) {
res.status(400).end("Email cannot be empty.");
}
try {
let newUser: any;
newUser = await prisma.user
.create({
data: { email: body.email },
})
.then(async () => {
await prisma.$disconnect();
res.status(200).send(newUser);
});
} catch (error) {
await prisma.$disconnect();
res.status(500).end("An error has occured. Error: " + error);
}
break;
default:
res.setHeader("Allow", ["POST"]);
res.status(405).end(`Method ${method} Not Allowed`);
}
}