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