Refactored pages/api/assignments to mongodb

This commit is contained in:
Carlos Mesquita
2024-09-07 15:13:41 +01:00
parent e8b7c5ff80
commit 6f7ef1abef
14 changed files with 820 additions and 462 deletions

View File

@@ -1,10 +1,10 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { app } from "@/firebase";
import { getFirestore, doc, getDoc, setDoc } from "firebase/firestore";
import client from "@/lib/mongodb";
import { ObjectId } from 'mongodb';
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
const db = getFirestore(app);
const db = client.db(process.env.MONGODB_DB);
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -12,14 +12,18 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
// verify if it's a logged user that is trying to archive
if (req.session.user) {
const { id } = req.query as { id: string };
const docSnap = await getDoc(doc(db, "assignments", id));
const doc = await db.collection("assignments").findOne({ _id: new ObjectId(id) });
if (!docSnap.exists()) {
if (!doc) {
res.status(404).json({ ok: false });
return;
}
await setDoc(docSnap.ref, { released: true }, { merge: true });
await db.collection("assignments").updateOne(
{ _id: new ObjectId(id) },
{ $set: { released: true } }
);
res.status(200).json({ ok: true });
return;
}