mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-24 20:27:13 -05:00
add suggestion backend
This commit is contained in:
42
pages/api/plus/suggestions.ts
Normal file
42
pages/api/plus/suggestions.ts
Normal 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;
|
||||
Reference in New Issue
Block a user