Added title to the exam generate
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, setDoc, doc} from "firebase/firestore";
|
||||
import {getFirestore, setDoc, doc, runTransaction, collection, query, where, getDocs} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Exam, InstructorGender, Variant} from "@/interfaces/exam";
|
||||
@@ -47,8 +47,25 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
const {module} = req.query as {module: string};
|
||||
|
||||
const exam = {...req.body, module: module};
|
||||
await setDoc(doc(db, module, req.body.id), exam);
|
||||
|
||||
try {
|
||||
const exam = {...req.body, module: module};
|
||||
await runTransaction(db, async (transaction) => {
|
||||
|
||||
res.status(200).json(exam);
|
||||
const docRef = doc(db, module, req.body.id);
|
||||
const docSnap = await transaction.get(docRef);
|
||||
|
||||
if (docSnap.exists()) {
|
||||
throw new Error('Name already exists');
|
||||
}
|
||||
|
||||
|
||||
const newDocRef = doc(db, module, req.body.id);
|
||||
transaction.set(newDocRef, exam);
|
||||
});
|
||||
res.status(200).json(exam);
|
||||
} catch (error) {
|
||||
console.error("Transaction failed: ", error);
|
||||
res.status(500).json({ok: false, error: (error as any).message});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user