Skip to content

Commit

Permalink
Check for whitespaces and make changes to duplicate title checking
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelinglow committed Sep 29, 2024
1 parent aeb80e0 commit 856891c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ app.post("/questions/add", async (req, res) => {
try {
console.log(req.body);
const questionJson = {
title: req.body.title,
title: req.body.title.trim(),
category: req.body.category,
complexity: req.body.complexity,
description: req.body.description,
};
const querySnap = await db.collection("questions").where('title', '==', req.body.title).get();
const querySnap = await db.collection("questions").where('title', '==', req.body.title.trim()).get();
if (!querySnap.empty) {
return res.status(409).json({ message: 'Duplicate entry found' });
}
Expand All @@ -136,19 +136,24 @@ app.put("/questions/update/:id", async (req, res) => {
console.log("Updating question ID:", questionId);

const updatedQuestion = {
title: req.body.title,
title: req.body.title.trim(),
category: req.body.category,
complexity: req.body.complexity,
description: req.body.description,
};
const querySnap = await db.collection("questions").where('title', '==', req.body.title).get();
const querySnap = await db.collection("questions").where('title', '==', req.body.title.trim()).get();
if (!querySnap.empty) {
return res.status(409).json({ message: 'Duplicate entry found' });
for (const doc of querySnap.docs) {
if (doc.id != questionId) {
return res.status(409).json({ message: 'Duplicate entry found' });
}
}
}
const response = await db.collection("questions").doc(questionId).set(updatedQuestion, { merge: true });

res.send({ message: "Question updated successfully", response });
} catch (error) {
console.log(error.message)
res.status(500).send({ error: error.message });
}
});
Expand Down

0 comments on commit 856891c

Please sign in to comment.