Finish code base
This commit is contained in:
@@ -29,13 +29,18 @@ const getRandQuestion = async ({ response }) => {
|
|||||||
|
|
||||||
const checkRandQuestion = async ({ request, response }) => {
|
const checkRandQuestion = async ({ request, response }) => {
|
||||||
const body = await request.body();
|
const body = await request.body();
|
||||||
const questionId = body.value.questionId;
|
const data = await body.value;
|
||||||
const optionId = body.value.optionId;
|
console.log(data);
|
||||||
const correctOptionIds = await answerService.getCorrectOptionIds(questionId);
|
const questionId = data.questionId;
|
||||||
const correctOptionIdsArray = correctOptionIds.map((option) => {
|
const optionId = data.optionId;
|
||||||
return option.id;
|
console.log(questionId);
|
||||||
});
|
console.log(optionId);
|
||||||
const correct = correctOptionIdsArray.includes(optionId);
|
const correctOptionIds = (
|
||||||
|
await answerService.getCorrectOptionIds(questionId)
|
||||||
|
).map((obj) => obj.id);
|
||||||
|
console.log(correctOptionIds);
|
||||||
|
const correct = correctOptionIds.includes(Number(optionId));
|
||||||
|
console.log(correct);
|
||||||
response.body = { correct: correct };
|
response.body = { correct: correct };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ const deleteAnswer = async ({ params, response }) => {
|
|||||||
const topicId = params.tId;
|
const topicId = params.tId;
|
||||||
const questionId = params.qId;
|
const questionId = params.qId;
|
||||||
const optionId = params.oId;
|
const optionId = params.oId;
|
||||||
await answerService.deleteAnswer(questionId, optionId);
|
await answerService.deleteAnswer(optionId);
|
||||||
response.redirect(`/topics/${topicId}/questions/${questionId}`);
|
response.redirect(`/topics/${topicId}/questions/${questionId}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,6 @@ const storeAnswer = async ({ response, params, state }) => {
|
|||||||
const correctOptionIds = (
|
const correctOptionIds = (
|
||||||
await answerService.getCorrectOptionIds(questionId)
|
await answerService.getCorrectOptionIds(questionId)
|
||||||
).map((obj) => obj.id);
|
).map((obj) => obj.id);
|
||||||
console.log(correctOptionIds);
|
|
||||||
const correct = correctOptionIds.includes(Number(optionId));
|
const correct = correctOptionIds.includes(Number(optionId));
|
||||||
await answerService.storeAnswer(userId, questionId, optionId);
|
await answerService.storeAnswer(userId, questionId, optionId);
|
||||||
if (correct) {
|
if (correct) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ router.post("/auth/register", authController.register);
|
|||||||
// topicController routes (topics)
|
// topicController routes (topics)
|
||||||
router.get("/topics", topicController.listTopics);
|
router.get("/topics", topicController.listTopics);
|
||||||
router.post("/topics", topicController.addTopic);
|
router.post("/topics", topicController.addTopic);
|
||||||
router.get("/topics/:tId/delete", topicController.deleteTopic);
|
router.post("/topics/:tId/delete", topicController.deleteTopic);
|
||||||
|
|
||||||
// questionController routes (questions)
|
// questionController routes (questions)
|
||||||
router.get("/topics/:tId", questionController.listQuestions);
|
router.get("/topics/:tId", questionController.listQuestions);
|
||||||
@@ -54,6 +54,6 @@ router.post(
|
|||||||
|
|
||||||
// questionApi routes (API)
|
// questionApi routes (API)
|
||||||
router.get("/api/questions/random", questionApi.getRandQuestion);
|
router.get("/api/questions/random", questionApi.getRandQuestion);
|
||||||
router.post("api/questions/answer", questionApi.checkRandQuestion);
|
router.post("/api/questions/answer", questionApi.checkRandQuestion);
|
||||||
|
|
||||||
export { router };
|
export { router };
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ const addAnswer = async (questionId, optionText, isCorrect) => {
|
|||||||
await sql`INSERT INTO question_answer_options (question_id, option_text, is_correct) VALUES (${questionId}, ${optionText}, ${isCorrect})`;
|
await sql`INSERT INTO question_answer_options (question_id, option_text, is_correct) VALUES (${questionId}, ${optionText}, ${isCorrect})`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteAnswer = async (questionId, optionId) => {
|
const deleteAnswer = async (optionId) => {
|
||||||
await sql`DELETE FROM question_answer_options WHERE question_id = ${questionId} AND id = ${optionId}`;
|
|
||||||
await sql`DELETE FROM question_answer_options WHERE id = ${optionId}`;
|
await sql`DELETE FROM question_answer_options WHERE id = ${optionId}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ const getTopicByTopicId = async (topicId) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const deleteTopic = async (topicId) => {
|
const deleteTopic = async (topicId) => {
|
||||||
|
await sql`DELETE FROM question_answers WHERE question_id IN (SELECT id FROM questions WHERE topic_id = ${topicId})`;
|
||||||
|
await sql`DELETE FROM question_answer_options WHERE question_id IN (SELECT id FROM questions WHERE topic_id = ${topicId})`;
|
||||||
|
await sql`DELETE FROM questions WHERE topic_id = ${topicId}`;
|
||||||
await sql`DELETE FROM topics WHERE id = ${topicId}`;
|
await sql`DELETE FROM topics WHERE id = ${topicId}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<% it.data.forEach(item => { %>
|
<% it.data.forEach(item => { %>
|
||||||
<li><%= item.option_text %></li>
|
<li><%= item.option_text %></li>
|
||||||
<% }); %>
|
<% }); %>
|
||||||
<ul>
|
</ul>
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
<p>Correct options are not found.</p>
|
<p>Correct options are not found.</p>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|||||||
@@ -39,5 +39,7 @@ Name:<br/>
|
|||||||
<input type="text" name="name"/><br/>
|
<input type="text" name="name"/><br/>
|
||||||
<input type="submit" value="Add"/>
|
<input type="submit" value="Add"/>
|
||||||
</form>
|
</form>
|
||||||
|
<% } else { %>
|
||||||
|
<p>You must be an admin to add a topic.</p>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user