el.xwx.moe/pages/api/routes/collections/index.ts

39 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
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";
import postCollection from "@/lib/api/controllers/collections/postCollection";
import updateCollection from "@/lib/api/controllers/collections/updateCollection";
import deleteCollection from "@/lib/api/controllers/collections/deleteCollection";
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") {
2023-04-24 16:30:40 -05:00
const newCollection = await postCollection(req.body, session.user.id);
2023-03-28 02:31:50 -05:00
return res
.status(newCollection.status)
.json({ response: newCollection.response });
} else if (req.method === "PUT") {
const updated = await updateCollection(req.body, session.user.id);
return res.status(updated.status).json({ response: updated.response });
} else if (req.method === "DELETE") {
const deleted = await deleteCollection(req.body, session.user.id);
return res.status(deleted.status).json({ response: deleted.response });
2023-03-28 02:31:50 -05:00
}
2023-02-06 11:59:23 -06:00
}