Skip to content

Commit 5b4f5b3

Browse files
committed
Provide Feedback For Operation Performed
On Performing Operations such as Add Question, Update Question or Delete Question, a boolean value is also returned. True implies the operation was successfully executed, while False implies otherwise.
1 parent 57c6b96 commit 5b4f5b3

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

peer-prep/src/Components/QuestionList/QuestionList.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,39 @@ export const Questions = () => {
2020
const addQuestion = (qTitle, qDifficulty, qTopic, qDescription) => {
2121
// Add in Database and Upload into Storage
2222
let newQuestion = new QuestionModel(qId, qTitle, qDescription, qDifficulty, qTopic);
23-
database.addQuestion(newQuestion);
23+
let isAddedSuccessfully = database.addQuestion(newQuestion);
2424

2525
updateLocalDatabase();
2626
updateStates();
2727

28-
// setQs([...qs, {id: qId, title: qTitle, difficulty: qDifficulty,
29-
// topic: qTopic, description: qDescription}]);
28+
console.log("Is Additon Successful: " + isAddedSuccessfully);
29+
30+
return isAddedSuccessfully;
3031
}
3132

3233
// Delete a Question
3334
const deleteQuestion = (questionId) => {
34-
database.deleteQuestion(questionId);
35+
let isDeletedSuccessfully = database.deleteQuestion(questionId);
3536

3637
updateLocalDatabase();
3738
updateStates();
39+
40+
console.log("Is Deletion Successful: " + isDeletedSuccessfully);
41+
42+
return isDeletedSuccessfully;
3843
}
3944

4045
// Update an existing Question
4146
const updateQuestion = (qId, qTitle, qDescription, qDifficulty, qTopic) => {
4247
let updatedQuestion = new QuestionModel(qId, qTitle, qDescription, qDifficulty, qTopic);
43-
database.updateQuestion(updatedQuestion);
48+
let isUpdateSuccessful = database.updateQuestion(updatedQuestion);
4449

4550
updateLocalDatabase();
4651
updateStates();
52+
53+
console.log("Is Update Successful: " + isUpdateSuccessful);
54+
55+
return isUpdateSuccessful;
4756
}
4857

4958
const updateLocalDatabase = () => {
@@ -54,7 +63,7 @@ export const Questions = () => {
5463
const updateStates = () => {
5564
// Update States
5665
setQs(Array.from(database.database));
57-
setQId(Array.from(database.database).length == 0 ? 0 : Array.from(database.database).sort((a, b) => a[0] < b[0]).slice(-1)[0][0]);
66+
setQId(Array.from(database.database).length === 0 ? 0 : Array.from(database.database).sort((a, b) => a[0] < b[0]).slice(-1)[0][0]);
5867
}
5968

6069
// To load data on mount

peer-prep/src/DataModel/QuestionDatabase.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,32 @@ class QuestionDatabase {
1414
}
1515

1616
addQuestion(questionToAdd) {
17-
if (!this.isDuplicateQuestion(questionToAdd)) {
17+
if (!this.isDuplicateQuestion(questionToAdd, true)) {
1818
this.database.set(questionToAdd.id, questionToAdd);
19+
20+
return true;
1921
}
22+
23+
return false
2024
}
2125

2226
deleteQuestion(questionId) {
2327
if (this.database.has(questionId)) {
2428
this.database.delete(questionId);
29+
30+
return true;
2531
}
32+
33+
return false;
2634
}
2735

2836
updateQuestion(questionToUpdate) {
29-
this.database.set(questionToUpdate.id, questionToUpdate);
37+
if (!this.isDuplicateQuestion(questionToUpdate, false)) {
38+
this.database.set(questionToUpdate.id, questionToUpdate);
39+
return true;
40+
}
41+
42+
return false;
3043
}
3144

3245
getAllQuestions() {
@@ -39,14 +52,19 @@ class QuestionDatabase {
3952
return questionList;
4053
}
4154

42-
isDuplicateQuestion(questionToCheck) {
43-
if (this.database.has(questionToCheck.id)) {
55+
isDuplicateQuestion(questionToCheck, toCheckId) {
56+
// Ensure Unique ID when adding questions
57+
if (this.database.has(questionToCheck.id) && toCheckId) {
4458
return true;
4559
}
4660

4761
var questionList = this.getAllQuestions();
4862

4963
for (var i = 0; i < questionList.length; i++) {
64+
if (questionToCheck.id === questionList[i].id) {
65+
continue;
66+
}
67+
5068
if (questionToCheck.title === questionList[i].title) {
5169
return true;
5270
}

peer-prep/src/Utility/localStorage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function updateLocalQuestionDatabase(updatedDatabase) {
77

88
function retrieveQuestionDatabase() {
99
let questionDatabase = new QuestionDatabase();
10-
console.log(localStorage.getItem("questions"));
10+
1111
questionDatabase.setDatabase(new Map(JSON.parse(localStorage.getItem("questions"))))
1212
return questionDatabase;
1313
}

0 commit comments

Comments
 (0)