Skip to content

Commit 3b9f5e8

Browse files
committed
Delete category if no other questions have it (triggered when question is deleted)
1 parent 0ecce13 commit 3b9f5e8

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

Backend/QuestionService/controllers/questions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ questionsRouter.get("/by-category-and-complexity", async (req, res) => {
1717
}
1818
});
1919

20+
questionsRouter.get("/by-category", async (req, res) => {
21+
const { category } = req.query;
22+
try {
23+
const query = category ? { category: { $in: [new RegExp(`^${category}$`, "i")] } } : {};
24+
const questions = await QuestionModel.find(query);
25+
res.status(200).json(questions);
26+
} catch (error) {
27+
console.error("Error fetching questions by category:", error);
28+
res.status(500).json({ error: error.message });
29+
}
30+
});
31+
2032
// Read all questions
2133
questionsRouter.get("/", async (req, res) => {
2234
try {

Frontend/src/components/question/Question.jsx

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ButtonGroup from "react-bootstrap/ButtonGroup";
55
import CreateQn from "./CreateQn";
66
import EditQn from "./EditQn";
77
import questionService from "../../services/questions"
8+
import categoryService from "../../services/categories"
89

910
function Question() {
1011
const [questions, setQuestions] = useState([]);
@@ -63,16 +64,44 @@ function Question() {
6364
};
6465

6566
const handleDeleteConfirm = () => {
66-
if (questionToDelete) {
67-
questionService.deleteQuestion(questionToDelete)
68-
.then(res => {
69-
console.log(res);
70-
setQuestions(questions.filter(question => question._id !== questionToDelete));
71-
handleCloseDelete();
67+
if (!questionToDelete) return;
68+
69+
// retrieve the question to get its categories before deleting
70+
questionService.get(questionToDelete)
71+
.then((question) => {
72+
const categories = question.data.category;
73+
console.log("question retrieved: ", question)
74+
console.log(`catgories of qn: ${categories}`)
75+
76+
// delete the question
77+
questionService.deleteQuestion(questionToDelete)
78+
.then(res => {
79+
console.log(res);
80+
81+
82+
setQuestions(questions.filter(q => q._id !== questionToDelete));
83+
handleCloseDelete();
84+
85+
// check each category to see if it should be deleted
86+
categories.forEach(async (category) => {
87+
try {
88+
const remainingQuestions = await questionService.getQuestionsByCategory(category);
89+
90+
// if no other questions have this category, delete the category
91+
if (remainingQuestions.length === 0) {
92+
await categoryService.deleteCategory(category);
93+
console.log(`Category ${category} deleted.`);
94+
}
95+
} catch (err) {
96+
console.error(`Error processing category ${category}:`, err);
97+
}
98+
});
99+
})
100+
.catch(err => console.error("Error deleting question:", err));
72101
})
73-
.catch(err => console.log(err));
74-
}
102+
.catch(err => console.error(`Error fetching question data for ID ${questionToDelete}:`, err));
75103
};
104+
76105

77106
const renderQuestionsTable = (questions) => {
78107
const sortedQuestions = [...questions].sort((a, b) => a.id - b.id)

Frontend/src/services/categories.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const createCategories = async (newCategories) => {
1616
};
1717

1818

19-
const deleteCategory = async (CategoryId) => {
20-
const response = await axios.delete(`${baseCategoryUrl}/${CategoryId}`);
19+
const deleteCategory = async (categoryName) => {
20+
const response = await axios.delete(`${baseCategoryUrl}/${categoryName}`);
2121
return response.data;
2222
};
2323

24+
2425
export default { getAllCategories, createCategories, deleteCategory };

Frontend/src/services/questions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ const deleteQuestion = (id) => {
3333
return request.then(response => response.data);
3434
}
3535

36-
export default { getAll, get, createQuestion, updateQuestion, deleteQuestion }
36+
export default { getAll, get, createQuestion, updateQuestion, deleteQuestion, getQuestionsByCategory }

0 commit comments

Comments
 (0)