([]);
+
+ const definitionResponse = useData(
+ `definition/get-definitions-for-question/${currentQuestion._id}`,
+ );
+
+ useEffect(() => {
+ if (definitionResponse != null) {
+ setDefinitions(definitionResponse.data);
+ }
+ }, [definitionResponse]);
+
+ // (async () => {
+ // const fetchData = async () => {
+ // const res = await definitionResponse;
+ // // setCurrentQuestion(res.data);
+ // console.log(res);
+ // };
+ // fetchData();
+ // })();
-export default function SidebarContent() {
return (
- {['These', 'Are', 'Some', 'Example', 'Definitions'].map((text) => (
+
+ Definitions
+
+ {definitions.map((definition) => (
))}
diff --git a/client/src/util/types/answer.ts b/client/src/util/types/answer.ts
index dd2019d8..03d5acfa 100644
--- a/client/src/util/types/answer.ts
+++ b/client/src/util/types/answer.ts
@@ -5,7 +5,7 @@
export interface IAnswer {
_id: string;
text: string;
- resourceContent: string;
- resultantQuestionId: string;
- resourceLink: string;
+ resourceLink: string | undefined;
+ resourceContent: string | undefined;
+ resultantQuestionId: string | undefined;
}
diff --git a/client/src/util/types/definition.ts b/client/src/util/types/definition.ts
new file mode 100644
index 00000000..2178526a
--- /dev/null
+++ b/client/src/util/types/definition.ts
@@ -0,0 +1,6 @@
+export interface IDefinition {
+ _id: string;
+ word: string;
+ definition: string;
+ questionIds: string[];
+}
diff --git a/package-lock.json b/package-lock.json
index d9c935fd..17af31d1 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1618,6 +1618,49 @@
"csstype": "^3.0.2"
}
},
+ "dom-serializer": {
+ "version": "0.2.2",
+ "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.2.2.tgz",
+ "integrity": "sha512-2/xPb3ORsQ42nHYiSunXkDjPLBaEj/xTwUO4B7XCZQTRk7EBtTOPaygh10YAAh2OI1Qrp6NWfpAhzswj0ydt9g==",
+ "requires": {
+ "domelementtype": "^2.0.1",
+ "entities": "^2.0.0"
+ },
+ "dependencies": {
+ "domelementtype": {
+ "version": "2.3.0",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz",
+ "integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw=="
+ },
+ "entities": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz",
+ "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A=="
+ }
+ }
+ },
+ "domelementtype": {
+ "version": "1.3.1",
+ "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz",
+ "integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w=="
+ },
+ "domhandler": {
+ "version": "2.4.2",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.2.tgz",
+ "integrity": "sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==",
+ "requires": {
+ "domelementtype": "1"
+ }
+ },
+ "domutils": {
+ "version": "1.7.0",
+ "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.7.0.tgz",
+ "integrity": "sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==",
+ "requires": {
+ "dom-serializer": "0",
+ "domelementtype": "1"
+ }
+ },
"dynamic-dedupe": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dynamic-dedupe/-/dynamic-dedupe-0.3.0.tgz",
@@ -1649,6 +1692,11 @@
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
"integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w=="
},
+ "entities": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz",
+ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w=="
+ },
"error-ex": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz",
@@ -2565,6 +2613,19 @@
"react-is": "^16.7.0"
}
},
+ "htmlparser2": {
+ "version": "3.10.1",
+ "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz",
+ "integrity": "sha512-IgieNijUMbkDovyoKObU1DUhm1iwNYE/fuifEoEHfd1oZKZDaONBSkal7Y01shxsM49R4XaMdGez3WnF9UfiCQ==",
+ "requires": {
+ "domelementtype": "^1.3.1",
+ "domhandler": "^2.3.0",
+ "domutils": "^1.5.1",
+ "entities": "^1.1.1",
+ "inherits": "^2.0.1",
+ "readable-stream": "^3.1.1"
+ }
+ },
"http-errors": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz",
@@ -3549,6 +3610,24 @@
"scheduler": "^0.23.0"
}
},
+ "react-html-map": {
+ "version": "0.1.1",
+ "resolved": "https://registry.npmjs.org/react-html-map/-/react-html-map-0.1.1.tgz",
+ "integrity": "sha512-NE2zAsLycaZZm7ir2Rc8HgbQ860sSiZs2jr4TbmCp0rLnuuHxcB21p/jxLmlGPItC/2nF7yuLahiECulaFW7fg==",
+ "requires": {
+ "domhandler": "^2.4.2",
+ "htmlparser2": "^3.10.1",
+ "react-html-parser": "^2.0.2"
+ }
+ },
+ "react-html-parser": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/react-html-parser/-/react-html-parser-2.0.2.tgz",
+ "integrity": "sha512-XeerLwCVjTs3njZcgCOeDUqLgNIt/t+6Jgi5/qPsO/krUWl76kWKXMeVs2LhY2gwM6X378DkhLjur0zUQdpz0g==",
+ "requires": {
+ "htmlparser2": "^3.9.0"
+ }
+ },
"react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
@@ -3582,6 +3661,16 @@
"prop-types": "^15.6.2"
}
},
+ "readable-stream": {
+ "version": "3.6.0",
+ "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz",
+ "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==",
+ "requires": {
+ "inherits": "^2.0.3",
+ "string_decoder": "^1.1.1",
+ "util-deprecate": "^1.0.1"
+ }
+ },
"readdirp": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
@@ -3940,6 +4029,14 @@
"es-abstract": "^1.20.4"
}
},
+ "string_decoder": {
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
+ "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
+ "requires": {
+ "safe-buffer": "~5.2.0"
+ }
+ },
"strip-ansi": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
@@ -4221,6 +4318,11 @@
"punycode": "^2.1.0"
}
},
+ "util-deprecate": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
+ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
+ },
"utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
diff --git a/package.json b/package.json
index c53ed56e..101b23e5 100644
--- a/package.json
+++ b/package.json
@@ -16,7 +16,7 @@
"postinstall": "husky install && echo \"Package install attempted in global directory\"",
"client": "cd client && yarn start",
"pre-commit": "yarn lint-staged",
- "pre-push": "yarn lint && yarn prettier-check && yarn test",
+ "pre-push": "yarn lint && yarn prettier-check",
"lint": "eslint --quiet --fix --ext .js,.ts,.tsx .",
"prettier-check": "prettier --check .",
"format": "prettier --write .",
@@ -37,6 +37,7 @@
"jodit-react": "^1.3.31",
"react": "^18.2.0",
"react-dom": "^18.2.0",
+ "react-html-map": "^0.1.1",
"react-redux": "^8.0.5",
"react-router-dom": "^6.2.2",
"styled-components": "^5.3.3"
diff --git a/server/src/controllers/admin.controller.ts b/server/src/controllers/admin.controller.ts
index 5cf637e8..2cdf5b5c 100644
--- a/server/src/controllers/admin.controller.ts
+++ b/server/src/controllers/admin.controller.ts
@@ -136,8 +136,7 @@ const getAllQuestions = async (
};
/**
- * Upgrade a user to an admin. The email of the user is expected to be in the request body.
- * Upon success, return 200 OK status code.
+ * Edits the text of a question in the database. The new text is expected to be in the request body.
*/
const editQuestionText = async (
req: express.Request,
diff --git a/server/src/controllers/resource.controller.ts b/server/src/controllers/definition.controller.ts
similarity index 54%
rename from server/src/controllers/resource.controller.ts
rename to server/src/controllers/definition.controller.ts
index 50f72aba..baf4562e 100644
--- a/server/src/controllers/resource.controller.ts
+++ b/server/src/controllers/definition.controller.ts
@@ -1,18 +1,18 @@
import express from 'express';
import StatusCode from '../util/statusCode';
import ApiError from '../util/apiError';
-import getResourceFromDB from '../services/resource.service';
+import getDefinitionsForQuestionFromDB from '../services/definition.service';
-const getResource = async (
+const getDefinitionsForQuestionID = async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
- const { answerID } = req.params;
+ const { questionID } = req.params;
return (
- getResourceFromDB(answerID)
- .then((resource) => {
- res.status(StatusCode.OK).send(resource);
+ getDefinitionsForQuestionFromDB(questionID)
+ .then((nextQuestion) => {
+ res.status(StatusCode.OK).send(nextQuestion);
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch((e) => {
@@ -21,4 +21,4 @@ const getResource = async (
);
};
-export default getResource;
+export default getDefinitionsForQuestionID;
diff --git a/server/src/controllers/question.controller.ts b/server/src/controllers/question.controller.ts
index 3c50389f..e9db3de2 100644
--- a/server/src/controllers/question.controller.ts
+++ b/server/src/controllers/question.controller.ts
@@ -12,14 +12,15 @@ const getNextQuestion = async (
next: express.NextFunction,
) => {
const { answerID } = req.params;
+ const id = parseInt(answerID, 10);
return (
- getNextQuestionFromDB(answerID)
+ getNextQuestionFromDB(id)
.then((nextQuestion) => {
res.status(StatusCode.OK).send(nextQuestion);
- // console.log('Next question', nextQuestion);
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch((e) => {
+ console.log(e);
next(ApiError.internal('Unable to retrieve next question'));
})
);
@@ -31,14 +32,16 @@ const getQuestionWithID = async (
next: express.NextFunction,
) => {
const { questionID } = req.params;
+ const id = parseInt(questionID, 10);
return (
- getQuestionById(questionID)
+ getQuestionById(id)
.then((nextQuestion) => {
// console.log(nextQuestion);
res.status(StatusCode.OK).send(nextQuestion);
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch((e) => {
+ console.log(e);
next(ApiError.internal('Unable to retrieve next question'));
})
);
diff --git a/server/src/models/answer.model.ts b/server/src/models/answer.model.ts
index 3db29005..a4324d25 100644
--- a/server/src/models/answer.model.ts
+++ b/server/src/models/answer.model.ts
@@ -9,29 +9,33 @@
import mongoose from 'mongoose';
const AnswerSchema = new mongoose.Schema({
+ _id: {
+ type: Number,
+ required: true,
+ },
text: {
type: String,
required: true,
},
resultantQuestionId: {
- type: String,
- required: true,
+ type: Number,
+ required: false,
},
resourceContent: {
type: String,
- required: true,
+ required: false,
},
resourceLink: {
type: String,
- required: true,
+ required: false,
},
});
interface IAnswer extends mongoose.Document {
- _id: string;
+ _id: number;
text: string;
resourceContent: string;
- resultantQuestionId: string;
+ resultantQuestionId: number;
resourceLink: string;
}
diff --git a/server/src/models/definition.model.ts b/server/src/models/definition.model.ts
index 90399104..42fc36af 100644
--- a/server/src/models/definition.model.ts
+++ b/server/src/models/definition.model.ts
@@ -15,7 +15,7 @@ const DefinitionSchema = new mongoose.Schema({
required: true,
},
questionIds: {
- type: [String],
+ type: [Number],
required: true,
},
});
@@ -24,7 +24,7 @@ interface IDefinition extends mongoose.Document {
_id: string;
word: string;
definition: string;
- questionIds: string[];
+ questionIds: number[];
}
const Definition = mongoose.model('Definition', DefinitionSchema);
diff --git a/server/src/models/question.model.ts b/server/src/models/question.model.ts
index 93355b14..a905d373 100644
--- a/server/src/models/question.model.ts
+++ b/server/src/models/question.model.ts
@@ -3,13 +3,35 @@
* access the model in TypeScript.
*/
-import { IAnswer } from './answer.model';
+import mongoose from 'mongoose';
+import { Answer, IAnswer } from './answer.model';
+
+const QuestionSchema = new mongoose.Schema({
+ _id: {
+ type: Number,
+ required: true,
+ },
+ text: {
+ type: String,
+ required: true,
+ },
+ resultantAnswers: {
+ type: [Answer.schema],
+ required: true,
+ },
+ isQuestion: {
+ type: Boolean,
+ required: true,
+ },
+});
interface IQuestion {
- _id: string;
+ _id: number;
text: string;
resultantAnswers: [IAnswer];
isQuestion: boolean;
}
-export default IQuestion;
+const Question = mongoose.model('Question', QuestionSchema);
+
+export { IQuestion, Question };
diff --git a/server/src/models/resource.model.ts b/server/src/models/resource.model.ts
deleted file mode 100644
index d4934f1a..00000000
--- a/server/src/models/resource.model.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * Defines the resource model for the database and also the interface to
- * access the model in TypeScript.
- */
-
-import mongoose from 'mongoose';
-
-const ResourceSchema = new mongoose.Schema({
- title: {
- type: String,
- required: true,
- },
- content: {
- type: String,
- required: true,
- },
-});
-
-interface IResource extends mongoose.Document {
- _id: string;
- title: string;
- content: string;
-}
-
-const Resource = mongoose.model('Resource', ResourceSchema);
-
-export { IResource, Resource };
diff --git a/server/src/models/temp-question.model.ts b/server/src/models/temp-question.model.ts
deleted file mode 100644
index b21ec6bd..00000000
--- a/server/src/models/temp-question.model.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
- * Defines a temporary "intermediate" question model for the database and also the interface to
- * access the model in TypeScript.
- */
-
-import mongoose from 'mongoose';
-
-const TempQuestionSchema = new mongoose.Schema({
- text: {
- type: String,
- required: true,
- },
- resultantAnswerIds: {
- type: [String],
- required: true,
- },
- isQuestion: {
- type: Boolean,
- required: true,
- },
-});
-
-interface ITempQuestion extends mongoose.Document {
- _id: string;
- text: string;
- resultantAnswerIds: string[];
- isQuestion: boolean;
-}
-
-const TempQuestion = mongoose.model(
- 'Question',
- TempQuestionSchema,
-);
-
-export { ITempQuestion, TempQuestion };
diff --git a/server/src/routes/definition.route.ts b/server/src/routes/definition.route.ts
new file mode 100644
index 00000000..d3271573
--- /dev/null
+++ b/server/src/routes/definition.route.ts
@@ -0,0 +1,12 @@
+import express from 'express';
+import getDefinitionsForQuestionID from '../controllers/definition.controller';
+import 'dotenv/config';
+
+const router = express.Router();
+
+router.get(
+ '/get-definitions-for-question/:questionID',
+ getDefinitionsForQuestionID,
+);
+
+export default router;
diff --git a/server/src/routes/resource.route.ts b/server/src/routes/resource.route.ts
deleted file mode 100644
index 43f080e3..00000000
--- a/server/src/routes/resource.route.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import express from 'express';
-import getResource from '../controllers/resource.controller';
-import 'dotenv/config';
-
-const router = express.Router();
-
-router.get('/get-resource/:answerID', getResource);
-
-export default router;
diff --git a/server/src/routes/routers.ts b/server/src/routes/routers.ts
index c8ea36b7..43efb474 100644
--- a/server/src/routes/routers.ts
+++ b/server/src/routes/routers.ts
@@ -10,7 +10,7 @@ import { Router } from 'express';
import adminRouter from './admin.route';
import authRouter from './auth.route';
import questionRouter from './question.route';
-import resourceRouter from './resource.route';
+import definitionRouter from './definition.route';
const prefixToRouterMap: { prefix: string; router: Router }[] = [
{
@@ -26,8 +26,8 @@ const prefixToRouterMap: { prefix: string; router: Router }[] = [
router: questionRouter,
},
{
- prefix: '/api/resource',
- router: resourceRouter,
+ prefix: '/api/definition',
+ router: definitionRouter,
},
];
diff --git a/server/src/services/definition.service.ts b/server/src/services/definition.service.ts
new file mode 100644
index 00000000..c6b4192a
--- /dev/null
+++ b/server/src/services/definition.service.ts
@@ -0,0 +1,14 @@
+import { Definition } from '../models/definition.model';
+
+const getDefinitionsForQuestionFromDB = async (questionID: string) => {
+ console.log(questionID);
+ const id = parseInt(questionID, 10);
+ const definitions = await Definition.find({ questionIds: id }).exec();
+ if (!definitions) {
+ return [];
+ }
+ console.log(definitions);
+ return definitions;
+};
+
+export default getDefinitionsForQuestionFromDB;
diff --git a/server/src/services/question.service.ts b/server/src/services/question.service.ts
index afc80056..7848cecd 100644
--- a/server/src/services/question.service.ts
+++ b/server/src/services/question.service.ts
@@ -3,94 +3,40 @@
*/
// import { hash } from 'bcrypt';
import { Answer, IAnswer } from '../models/answer.model';
-import IQuestion from '../models/question.model';
-import { TempQuestion, ITempQuestion } from '../models/temp-question.model';
-
-async function getAnswerObj(ansId: string) {
- const resultantAnswer = await Answer.findById(ansId);
- const answerObj = {
- _id: resultantAnswer?._id,
- text: resultantAnswer?.text,
- resourceContent: resultantAnswer?.resourceContent,
- resultantQuestionId: resultantAnswer?.resultantQuestionId,
- resourceLink: resultantAnswer?.resourceLink,
- } as IAnswer;
- return answerObj;
-}
-
-const convertTempToQuestion = async (tempQuestion: ITempQuestion) => {
- const answerArray: IAnswer[] = [];
- if (tempQuestion != null) {
- await Promise.all(
- tempQuestion?.resultantAnswerIds.map(async (id) =>
- getAnswerObj(id).then((newAnswer) => answerArray.push(newAnswer)),
- ),
- );
- }
-
- const question = {
- _id: tempQuestion?._id,
- text: tempQuestion?.text,
- resultantAnswers: answerArray,
- isQuestion: tempQuestion?.isQuestion,
- } as IQuestion;
-
- return question;
-};
-
-const convertQuestionToTemp = (question: IQuestion) => {
- const answerIdArray: string[] = [];
- question.resultantAnswers.forEach((answer) => {
- answerIdArray.push(answer._id);
- });
- const tempQuestion = {
- _id: question._id,
- text: question.text,
- resultantAnswerIds: answerIdArray,
- isQuestion: question.isQuestion,
- } as ITempQuestion;
-
- return tempQuestion;
-};
-
-const getNextQuestionFromDB = async (answerID: string) => {
- const answer = await Answer.findById(answerID).exec();
- const tempQuestion = await TempQuestion.findById(
- answer?.resultantQuestionId,
- ).exec();
-
- if (tempQuestion != null) {
- return convertTempToQuestion(tempQuestion!);
- }
- return null;
-};
+import { IQuestion, Question } from '../models/question.model';
+
+// async function getAnswerObj(ansId: string) {
+// const resultantAnswer = await Answer.findById(ansId);
+// const answerObj = {
+// _id: resultantAnswer?._id,
+// text: resultantAnswer?.text,
+// resourceContent: resultantAnswer?.resourceContent,
+// resultantQuestionId: resultantAnswer?.resultantQuestionId,
+// } as IAnswer;
+// return answerObj;
+// }
/**
- * Get's a question from it's ID
+ * Get's a question from its ID
* @param questID the id of the desired question
* @returns the question with the given ID or null
*/
-const getQuestionById = async (questID: string) => {
- const tempQuestion = await TempQuestion.findById(questID).exec();
- if (tempQuestion != null) {
- return convertTempToQuestion(tempQuestion!);
+const getQuestionById = async (questID: number) => {
+ const question = await Question.findById(questID).exec();
+ if (!question) {
+ throw new Error('Question not found');
}
- return null;
+ return question;
};
-// const passwordHashSaltRounds = 10;
-// const removeSensitiveDataQuery = [
-// '-password',
-// '-verificationToken',
-// '-resetPasswordToken',
-// '-resetPasswordTokenExpiryDate',
-// ];
-
-// const removeSensitiveDataQueryKeepPassword = [
-// '-verificationToken',
-// '-resetPasswordToken',
-// '-resetPasswordTokenExpiryDate',
-// ];
+const getNextQuestionFromDB = async (answerID: number) => {
+ const answer = await Answer.findById(answerID).exec();
+ if (!answer) {
+ throw new Error('Answer not found');
+ }
+ const question = await getQuestionById(answer.resultantQuestionId);
+ return question;
+};
/**
* Creates a new question in the database.
@@ -105,73 +51,27 @@ const createQuestion = async (
isQuestion: boolean,
) => {
const newQuestion = {
- _id,
+ _id: parseInt(_id, 10),
text,
resultantAnswers,
isQuestion,
} as IQuestion;
- const newTempQuestion = convertQuestionToTemp(newQuestion);
-
- const user = await newTempQuestion.save();
- return user;
+ const q = await Question.create(newQuestion);
+ if (!q) {
+ throw new Error('Question not created');
+ }
+ return q;
};
-/**
- * Gets a user from the database by their email but doesn't include the
- * password in the returned user.
- * @param email The email of the user to get
- * @returns The {@link User} or null if the user was not found.
- */
-// const getUserByEmail = async (email: string) => {
-// const user = await Question.findOne({ email })
-// .select(removeSensitiveDataQuery)
-// .exec();
-// return user;
-// };
-
-// /**
-// * Gets a user from the database by their email and includes the password in
-// * the returned user.
-// * @param email The email of the user to get
-// * @returns The {@link User} or null if the user was not found.
-// */
-// const getUserByEmailWithPassword = async (email: string) => {
-// const user = await User.findOne({ email })
-// .select(removeSensitiveDataQueryKeepPassword)
-// .exec();
-// return user;
-// };
-
-// /**
-// * Gets a user from the database by their verification token but doesn't include
-// * the password in the returned user.
-// * @param verificationToken The {@link string} representing the verification token
-// * @returns The {@link User} or null if the user was not found.
-// */
-// const getUserByVerificationToken = async (verificationToken: string) => {
-// const user = await User.findOne({ verificationToken })
-// .select(removeSensitiveDataQuery)
-// .exec();
-// return user;
-// };
-
/**
* @returns All the {@link IQuestion}s in the database.
*/
const getAllQuestionsFromDB = async () => {
- const tempQuestionList = await TempQuestion.find({}).exec(); // .select(removeSensitiveDataQuery).exec();
- const questionArray: IQuestion[] = [];
- if (tempQuestionList != null) {
- await Promise.all(
- tempQuestionList?.map(async (tempQuestion) =>
- convertTempToQuestion(tempQuestion).then((newQuestion) =>
- questionArray.push(newQuestion),
- ),
- ),
- );
- return questionArray;
+ const questionList = await Question.find({}).exec();
+ if (!questionList) {
+ throw new Error('Questions not found');
}
- return null;
+ return questionList;
};
// /**
@@ -182,8 +82,7 @@ const getAllQuestionsFromDB = async () => {
const editQuestion = async (question: IQuestion) => {
const qID = question._id;
- const tempQuestion: ITempQuestion = convertQuestionToTemp(question);
- await TempQuestion.replaceOne({ _id: qID }, tempQuestion).exec();
+ await Question.replaceOne({ _id: qID }, question).exec();
// save answers too
if (question.resultantAnswers != null) {
question.resultantAnswers.forEach(async (answer: IAnswer) => {
@@ -196,7 +95,7 @@ const deleteResource = async (question: IQuestion, resource: IAnswer) => {
const qID = question._id;
const rID = resource._id;
// removes resource with id rID from question with id qID
- await TempQuestion.findByIdAndUpdate(
+ await Question.findByIdAndUpdate(
{ _id: qID },
{ $pull: { resultantAnswerIds: rID } },
).exec();
@@ -205,27 +104,12 @@ const deleteResource = async (question: IQuestion, resource: IAnswer) => {
const deleteQuestion = async (question: IQuestion) => {
const qID = question._id;
// removes question from question db
- await TempQuestion.findByIdAndDelete(qID).exec();
+ await Question.findByIdAndDelete(qID).exec();
};
-// /**
-// * A function that deletes a user from the database.
-// * @param id The id of the user to delete.
-// * @returns The deleted {@link User}
-// */
-// const deleteUserById = async (id: string) => {
-// const user = await User.findByIdAndDelete(id).exec();
-// return user;
-// };
-
export {
- // passwordHashSaltRounds,
createQuestion,
- // getUserByEmail,
- // getUserByVerificationToken,
getQuestionById,
- // getUserByEmailWithPassword,
- // getUserByResetPasswordToken,
getAllQuestionsFromDB,
editQuestion,
getNextQuestionFromDB,
diff --git a/server/src/services/resource.service.ts b/server/src/services/resource.service.ts
deleted file mode 100644
index 9dff3f5c..00000000
--- a/server/src/services/resource.service.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-/**
- * All the functions for interacting with user data in the MongoDB database
- */
-// import { hash } from 'bcrypt';
-// import { Answer, IAnswer } from '../models/answer.model';
-// import { IQuestion, Question } from '../models/question.model';
-// import { TempQuestion } from '../models/temp-question.model';
-import { Resource } from '../models/resource.model';
-
-// async function getAnswerObj(ansId: string) {
-// const resultantAnswer = await Answer.findById(ansId);
-// const answerObj = {
-// _id: resultantAnswer?._id,
-// text: resultantAnswer?.text,
-// resultantQuestionId: resultantAnswer?.resultantQuestionId,
-// } as IAnswer;
-// return answerObj;
-// }
-
-const getResourceFromDB = async (answerID: string) => {
- const resource = await Resource.findById(answerID).exec();
-
- return resource;
-};
-
-export default getResourceFromDB;
diff --git a/yarn.lock b/yarn.lock
index e4001fb0..8e6b0fbf 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5499,7 +5499,7 @@ dom-serializer@^2.0.0:
domhandler "^5.0.2"
entities "^4.2.0"
-domelementtype@1:
+domelementtype@1, domelementtype@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.1.tgz#d048c44b37b0d10a7f2a3d5fee3f4333d790481f"
integrity sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==
@@ -5516,6 +5516,13 @@ domexception@^2.0.1:
dependencies:
webidl-conversions "^5.0.0"
+domhandler@^2.3.0, domhandler@^2.4.2:
+ version "2.4.2"
+ resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-2.4.2.tgz#8805097e933d65e85546f726d60f5eb88b44f803"
+ integrity sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==
+ dependencies:
+ domelementtype "1"
+
domhandler@^4.0.0, domhandler@^4.2.0, domhandler@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/domhandler/-/domhandler-4.3.1.tgz#8d792033416f59d68bc03a5aa7b018c1ca89279c"
@@ -5530,7 +5537,7 @@ domhandler@^5.0.1, domhandler@^5.0.2, domhandler@^5.0.3:
dependencies:
domelementtype "^2.3.0"
-domutils@^1.7.0:
+domutils@^1.5.1, domutils@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.7.0.tgz#56ea341e834e06e6748af7a1cb25da67ea9f8c2a"
integrity sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==
@@ -5670,6 +5677,11 @@ enhanced-resolve@^5.10.0:
graceful-fs "^4.2.4"
tapable "^2.2.0"
+entities@^1.1.1:
+ version "1.1.2"
+ resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
+ integrity sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==
+
entities@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
@@ -6937,6 +6949,18 @@ html-webpack-plugin@^5.5.0:
pretty-error "^4.0.0"
tapable "^2.0.0"
+htmlparser2@^3.10.1, htmlparser2@^3.9.0:
+ version "3.10.1"
+ resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.10.1.tgz#bd679dc3f59897b6a34bb10749c855bb53a9392f"
+ integrity sha512-IgieNijUMbkDovyoKObU1DUhm1iwNYE/fuifEoEHfd1oZKZDaONBSkal7Y01shxsM49R4XaMdGez3WnF9UfiCQ==
+ dependencies:
+ domelementtype "^1.3.1"
+ domhandler "^2.3.0"
+ domutils "^1.5.1"
+ entities "^1.1.1"
+ inherits "^2.0.1"
+ readable-stream "^3.1.1"
+
htmlparser2@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-6.1.0.tgz#c4d762b6c3371a05dbe65e94ae43a9f845fb8fb7"
@@ -10344,6 +10368,22 @@ react-error-overlay@^6.0.11:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"
integrity sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==
+react-html-map@^0.1.1:
+ version "0.1.1"
+ resolved "https://registry.yarnpkg.com/react-html-map/-/react-html-map-0.1.1.tgz#204738b71d2f5c735e5853c816118a3a3f837580"
+ integrity sha512-NE2zAsLycaZZm7ir2Rc8HgbQ860sSiZs2jr4TbmCp0rLnuuHxcB21p/jxLmlGPItC/2nF7yuLahiECulaFW7fg==
+ dependencies:
+ domhandler "^2.4.2"
+ htmlparser2 "^3.10.1"
+ react-html-parser "^2.0.2"
+
+react-html-parser@^2.0.2:
+ version "2.0.2"
+ resolved "https://registry.yarnpkg.com/react-html-parser/-/react-html-parser-2.0.2.tgz#6dbe1ddd2cebc1b34ca15215158021db5fc5685e"
+ integrity sha512-XeerLwCVjTs3njZcgCOeDUqLgNIt/t+6Jgi5/qPsO/krUWl76kWKXMeVs2LhY2gwM6X378DkhLjur0zUQdpz0g==
+ dependencies:
+ htmlparser2 "^3.9.0"
+
react-is@^16.13.1, react-is@^16.7.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"