add suggestion backend

This commit is contained in:
Kalle
2021-02-23 22:20:22 +02:00
parent 32115798e2
commit e787a94de6
5 changed files with 80 additions and 11 deletions

View File

@@ -0,0 +1,42 @@
import { getMySession } from "lib/api";
import { UserError } from "lib/errors";
import { NextApiRequest, NextApiResponse } from "next";
import plusService from "services/plus";
import { ZodError } from "zod";
const suggestionsHandler = async (
req: NextApiRequest,
res: NextApiResponse
) => {
const user = await getMySession(req);
switch (req.method) {
case "POST":
await postHandler(req, res);
break;
default:
res.status(405).end();
}
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
if (!user) return res.status(401).end();
try {
await plusService.addSuggestion({ data: req.body, userId: user.id });
} catch (e) {
if (e instanceof ZodError) {
res.status(400).json({ message: e.message });
} else if (e instanceof UserError) {
res.status(400).json({ message: e.message });
} else {
res.status(500).end();
}
return;
}
res.status(200).end();
}
};
export default suggestionsHandler;