2023-02-06 11:59:23 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { authOptions } from "pages/api/auth/[...nextauth]";
|
2023-02-18 21:32:02 -06:00
|
|
|
import getCollections from "@/lib/api/controllers/collections/getCollections";
|
2023-02-24 11:32:28 -06:00
|
|
|
import postCollection from "@/lib/api/controllers/collections/postCollection";
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
export default async function (req: NextApiRequest, res: NextApiResponse) {
|
2023-02-06 11:59:23 -06:00
|
|
|
const session = await getServerSession(req, res, authOptions);
|
|
|
|
|
2023-02-08 15:11:33 -06:00
|
|
|
if (!session?.user?.email) {
|
|
|
|
return res.status(401).json({ response: "You must be logged in." });
|
2023-02-06 11:59:23 -06:00
|
|
|
}
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (req.method === "GET") {
|
|
|
|
const collections = await getCollections(session.user.id);
|
|
|
|
return res
|
|
|
|
.status(collections.status)
|
|
|
|
.json({ response: collections.response });
|
|
|
|
} else if (req.method === "POST") {
|
|
|
|
const newCollection = await postCollection(
|
|
|
|
req.body.collectionName.trim(),
|
|
|
|
session.user.id
|
|
|
|
);
|
|
|
|
return res
|
|
|
|
.status(newCollection.status)
|
|
|
|
.json({ response: newCollection.response });
|
|
|
|
}
|
2023-02-06 11:59:23 -06:00
|
|
|
}
|