Skip to content

Commit 8411117

Browse files
committed
Add Check For Empty Fields in Questions
- Now instead of a Bool, an Array is returned for Add and Update Question Methods. If the length of this array is 0, that means addition or update of the Question was successful without any errors. - If length > 0, All the errors/causes can be iterated through, and the first one can be shown to the user - Updated Formatting
1 parent 1cf696d commit 8411117

File tree

4 files changed

+132
-83
lines changed

4 files changed

+132
-83
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export const QuestionForm = ({qId, addQuestion, setAddQ, setQId, questionNumber}
1111
const handleSubmit = (toggleAddQ, addQ) => e => {
1212
e.preventDefault();
1313
toggleAddQ(false);
14-
addQ(title, difficulty, topic, description)
14+
15+
const response = addQ(title, difficulty, topic, description);
16+
return response; // If array length is 0, then successful, else can index through it for errors
1517
}
1618
return (
1719
<div className="form-container">

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const Questions = () => {
2525
updateLocalDatabase();
2626
updateStates();
2727

28-
console.log("Is Additon Successful: " + isAddedSuccessfully);
28+
console.log("Is Additon Successful: " + (isAddedSuccessfully.length === 0));
29+
console.log("Errors While Adding: " + (isAddedSuccessfully.length === 0 ? "None" : isAddedSuccessfully))
2930

3031
return isAddedSuccessfully;
3132
}
@@ -50,7 +51,8 @@ export const Questions = () => {
5051
updateLocalDatabase();
5152
updateStates();
5253

53-
console.log("Is Update Successful: " + isUpdateSuccessful);
54+
console.log("Is Update Successful: " + (isUpdateSuccessful.length === 0));
55+
console.log("Errors While Updating: " + (isUpdateSuccessful.length === 0 ? "None" : isUpdateSuccessful))
5456

5557
return isUpdateSuccessful;
5658
}
Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,100 @@
11
// A Class that stores and encapsulates Database Features
22

33
class QuestionDatabase {
4-
constructor() {
5-
this.database = new Map();
6-
}
4+
static DUPLICATE_QUESTION_MESSAGE = "The Entered Question Already Exists";
5+
6+
constructor() {
7+
this.database = new Map();
8+
}
9+
10+
setDatabase(database) {
11+
this.database = database;
12+
}
13+
14+
clearDatabase() {
15+
this.database = new Map();
16+
}
17+
18+
addQuestion(questionToAdd) {
19+
const emptyFields = questionToAdd.isQuestionValid();
720

8-
setDatabase(database) {
9-
this.database = database;
21+
if (emptyFields.length !== 0) {
22+
return emptyFields; // Question Has Empty Fields
1023
}
1124

12-
clearDatabase() {
13-
this.database = new Map();
25+
if (!this.isDuplicateQuestion(questionToAdd, true)) {
26+
this.database.set(questionToAdd.id, questionToAdd);
27+
28+
return []; // Implies Question Added Successfully
1429
}
1530

16-
addQuestion(questionToAdd) {
17-
if (!this.isDuplicateQuestion(questionToAdd, true)) {
18-
this.database.set(questionToAdd.id, questionToAdd);
31+
return [QuestionDatabase.DUPLICATE_QUESTION_MESSAGE]; // Question is Duplicate
32+
}
1933

20-
return true;
21-
}
34+
deleteQuestion(questionId) {
35+
if (this.database.has(questionId)) {
36+
this.database.delete(questionId);
2237

23-
return false
38+
return true;
2439
}
2540

26-
deleteQuestion(questionId) {
27-
if (this.database.has(questionId)) {
28-
this.database.delete(questionId);
41+
return false;
42+
}
2943

30-
return true;
31-
}
44+
updateQuestion(questionToUpdate) {
45+
const emptyFields = questionToUpdate.isQuestionValid();
3246

33-
return false;
47+
if (emptyFields.length !== 0) {
48+
return emptyFields; // Question Has Empty Fields
3449
}
3550

36-
updateQuestion(questionToUpdate) {
37-
if (!this.isDuplicateQuestion(questionToUpdate, false)) {
38-
this.database.set(questionToUpdate.id, questionToUpdate);
39-
return true;
40-
}
51+
if (!this.isDuplicateQuestion(questionToUpdate, false)) {
52+
this.database.set(questionToUpdate.id, questionToUpdate);
4153

42-
return false;
54+
return []; // Implies Question Updated Successfully
4355
}
4456

45-
getAllQuestions() {
46-
var questionList = [];
57+
return [QuestionDatabase.DUPLICATE_QUESTION_MESSAGE]; // Question is Duplicate
58+
}
4759

48-
for (const [key, value] of this.database) {
49-
questionList.push(value);
50-
}
60+
getAllQuestions() {
61+
var questionList = [];
5162

52-
return questionList;
63+
for (const [key, value] of this.database) {
64+
questionList.push(value);
5365
}
5466

55-
isDuplicateQuestion(questionToCheck, toCheckId) {
56-
// Ensure Unique ID when adding questions
57-
if (this.database.has(questionToCheck.id) && toCheckId) {
58-
return true;
59-
}
67+
return questionList;
68+
}
6069

61-
var questionList = this.getAllQuestions();
70+
isDuplicateQuestion(questionToCheck, toCheckId) {
71+
// Ensure Unique ID when adding questions
72+
if (this.database.has(questionToCheck.id) && toCheckId) {
73+
return true;
74+
}
6275

63-
for (var i = 0; i < questionList.length; i++) {
64-
if (questionToCheck.id === questionList[i].id) {
65-
continue;
66-
}
76+
var questionList = this.getAllQuestions();
6777

68-
if (questionToCheck.title === questionList[i].title) {
69-
return true;
70-
}
78+
for (var i = 0; i < questionList.length; i++) {
79+
if (questionToCheck.id === questionList[i].id) {
80+
continue;
81+
}
7182

72-
if (questionToCheck.description === questionList[i].description) {
73-
return true;
74-
}
75-
}
83+
if (questionToCheck.title === questionList[i].title) {
84+
return true;
85+
}
7686

77-
return false;
87+
if (questionToCheck.description === questionList[i].description) {
88+
return true;
89+
}
7890
}
7991

80-
toJSON() {
81-
return this.database;
82-
}
92+
return false;
93+
}
94+
95+
toJSON() {
96+
return this.database;
97+
}
8398
}
8499

85100
export default QuestionDatabase;
Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,69 @@
11
// Class that stores and encapsulates the Question and its related fields
22

33
class QuestionModel {
4-
constructor(id, title, description, complexity, category) {
5-
this.id = id;
6-
this.title = title;
7-
this.description = description;
8-
this.complexity = complexity;
9-
this.category= category;
10-
}
4+
static QUESTION_FIELDS = {
5+
ID: "Id",
6+
TITLE: "Title",
7+
DESCRIPTION: "Description",
8+
COMPLEXITY: "Complexity",
9+
CATEGORY: "Category",
10+
};
1111

12-
updateQuestionTitle(newQuestionTitle) {
13-
this.title = newQuestionTitle;
14-
}
12+
static ERROR_MESSAGE = " Is Empty";
1513

16-
updateQuestionDescription(newQuestionDescription) {
17-
this.description = newQuestionDescription;
18-
}
14+
constructor(id, title, description, complexity, category) {
15+
this.id = id;
16+
this.title = title;
17+
this.description = description;
18+
this.complexity = complexity;
19+
this.category = category;
20+
}
1921

20-
updateQuestionComplexity(newQuestionComplexity) {
21-
this.complexity = newQuestionComplexity;
22-
}
22+
isQuestionValid() {
23+
let errors = [];
2324

24-
updateQuestionCategory(newQuestionCategory) {
25-
this.category = newQuestionCategory;
26-
}
25+
for (const qField in QuestionModel.QUESTION_FIELDS) {
26+
const fieldName = QuestionModel.QUESTION_FIELDS[qField].toLowerCase();
2727

28-
toJSON() {
29-
return {
30-
id: this.id,
31-
title: this.title,
32-
description: this.description,
33-
complexity: this.complexity,
34-
category: this.category,
35-
};
28+
if (
29+
(typeof this[fieldName] === "string" &&
30+
this[fieldName].trim() === "") ||
31+
!this[fieldName]
32+
) {
33+
errors.push(
34+
QuestionModel.QUESTION_FIELDS[qField] + QuestionModel.ERROR_MESSAGE
35+
);
36+
}
3637
}
38+
39+
return errors;
40+
}
41+
42+
updateQuestionTitle(newQuestionTitle) {
43+
this.title = newQuestionTitle;
44+
}
45+
46+
updateQuestionDescription(newQuestionDescription) {
47+
this.description = newQuestionDescription;
48+
}
49+
50+
updateQuestionComplexity(newQuestionComplexity) {
51+
this.complexity = newQuestionComplexity;
52+
}
53+
54+
updateQuestionCategory(newQuestionCategory) {
55+
this.category = newQuestionCategory;
56+
}
57+
58+
toJSON() {
59+
return {
60+
id: this.id,
61+
title: this.title,
62+
description: this.description,
63+
complexity: this.complexity,
64+
category: this.category,
65+
};
66+
}
3767
}
38-
68+
3969
export default QuestionModel;

0 commit comments

Comments
 (0)