Skip to content

Commit

Permalink
Merge branch 'main' into nguyen/docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
nknguyenhc authored Oct 2, 2024
2 parents 3acdd85 + b8e58fd commit a961dd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
20 changes: 15 additions & 5 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ router.post("/", async (req: Request, res: Response) => {
const savedQuestion = await newQuestion.save();
res.status(201).json(savedQuestion);
} catch (err) {
res.status(400).json({ error: (err as Error).message });
res.status(400).json({
message: `Question with questionId ${questionId} already exists`,
});
}
});

Expand All @@ -30,7 +32,9 @@ router.get("/", async (req: Request, res: Response) => {
const questions = await Question.find();
res.json(questions);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
res
.status(500)
.json({ message: "Server Error : Unable to fetch questions Question" });
}
});

Expand All @@ -44,7 +48,9 @@ router.get("/:id", async (req: Request, res: Response) => {
if (!question) return res.status(404).json({ error: "Question not found" });
res.json(question);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
res
.status(500)
.json({ message: "Server Error : Unable to fetch Question" });
}
});

Expand All @@ -63,7 +69,9 @@ router.put("/:id", async (req: Request, res: Response) => {
return res.status(404).json({ error: "Question not found" });
res.json(updatedQuestion);
} catch (err) {
res.status(500).json({ error: (err as Error).message });
res
.status(500)
.json({ message: "Server Error : Unable to update Question" });
}
});

Expand All @@ -78,7 +86,9 @@ router.delete("/:id", async (req: Request, res: Response) => {
return res.status(404).json({ error: "Question not found" });
res.json({ message: "Question deleted successfully" });
} catch (err) {
res.status(500).json({ error: (err as Error).message });
res
.status(500)
.json({ message: "Server Error : Unable to delete Question" });
}
});

Expand Down
11 changes: 7 additions & 4 deletions frontend/src/components/QuestionDialog/QuestionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ const QuestionDialog = (props: {

const handleSubmit = async () => {
setIsErrorDisplayed(false);

try {
const response = isAddNew
? await QuestionService.addQuestion(id, title, description, categoriesString, complexity, link)
: await QuestionService.editQuestion(id, title, description, categoriesString, complexity, link);
closeMainDialog();
questionCallback(response, isAddNew ? "added" : "updated");
closeMainDialog();
} catch (error: any) {
setError(error?.message ?? "An unknown error occurred");
setIsErrorDisplayed(true);
let errorMessage = "An unknown error occurred";
if (error instanceof QuestionValidationError) {
errorMessage = error.message;
} else {
errorMessage = error.response.data.message;
}
setError(errorMessage);
setIsErrorDisplayed(true);
}
};

Expand Down

0 comments on commit a961dd9

Please sign in to comment.