2023-02-24 11:32:28 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { authOptions } from "pages/api/auth/[...nextauth]";
|
2023-03-05 15:03:20 -06:00
|
|
|
import getLinks from "@/lib/api/controllers/links/getLinks";
|
2023-02-24 11:32:28 -06:00
|
|
|
import postLink from "@/lib/api/controllers/links/postLink";
|
2023-03-23 10:25:17 -05:00
|
|
|
import deleteLink from "@/lib/api/controllers/links/deleteLink";
|
2023-03-28 10:11:34 -05:00
|
|
|
import updateLink from "@/lib/api/controllers/links/updateLink";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
2023-02-24 11:32:28 -06:00
|
|
|
const session = await getServerSession(req, res, authOptions);
|
|
|
|
|
|
|
|
if (!session?.user?.email) {
|
|
|
|
return res.status(401).json({ response: "You must be logged in." });
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (req.method === "GET") {
|
2023-06-15 07:39:30 -05:00
|
|
|
const links = await getLinks(session.user.id, req?.query?.body as string);
|
2023-03-28 02:31:50 -05:00
|
|
|
return res.status(links.status).json({ response: links.response });
|
|
|
|
} else if (req.method === "POST") {
|
|
|
|
const newlink = await postLink(req.body, session.user.id);
|
|
|
|
return res.status(newlink.status).json({
|
|
|
|
response: newlink.response,
|
|
|
|
});
|
2023-03-28 10:11:34 -05:00
|
|
|
} else if (req.method === "PUT") {
|
|
|
|
const updated = await updateLink(req.body, session.user.id);
|
|
|
|
return res.status(updated.status).json({
|
|
|
|
response: updated.response,
|
|
|
|
});
|
2023-04-28 16:10:29 -05:00
|
|
|
} else if (req.method === "DELETE") {
|
|
|
|
const deleted = await deleteLink(req.body, session.user.id);
|
|
|
|
return res.status(deleted.status).json({
|
|
|
|
response: deleted.response,
|
|
|
|
});
|
2023-03-28 02:31:50 -05:00
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|