Clean up, add API
This commit is contained in:
@@ -1 +0,0 @@
|
||||
You could add project configuration files here.
|
||||
42
drill-and-practice/routes/apis/questionApi.js
Normal file
42
drill-and-practice/routes/apis/questionApi.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import * as questionService from "../../services/questionService.js";
|
||||
import * as answerService from "../../services/answerService.js";
|
||||
|
||||
const getRandQuestion = async ({ response }) => {
|
||||
const randQuestion = await questionService.getRandQuestionAPI();
|
||||
if (randQuestion === null) {
|
||||
response.body = {};
|
||||
} else {
|
||||
const questionId = randQuestion.id;
|
||||
const optionsData = await answerService.getAnswersByQuestionId(questionId);
|
||||
optionsData.forEach((option) => {
|
||||
delete option.question_id;
|
||||
delete option.is_correct;
|
||||
const optionId = option.id;
|
||||
delete option.id;
|
||||
option.optionId = optionId;
|
||||
const optionText = option.option_text;
|
||||
delete option.option_text;
|
||||
option.optionText = optionText;
|
||||
});
|
||||
const responseQuestion = {
|
||||
questionId: questionId,
|
||||
questionText: randQuestion.question_text,
|
||||
answeroptions: optionsData,
|
||||
};
|
||||
response.body = responseQuestion;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
response.body = { correct: correct };
|
||||
};
|
||||
|
||||
export { getRandQuestion, checkRandQuestion };
|
||||
@@ -1 +0,0 @@
|
||||
You could add api-related endpoints here.
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from "../deps.js";
|
||||
import * as mainController from "./controllers/mainController.js";
|
||||
import * as authController from "./controllers/authController.js";
|
||||
import * as topicController from "./controllers/topicController.js";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
@@ -10,6 +11,6 @@ router.get("/auth/register", authController.showRegister);
|
||||
router.post("/auth/login", authController.login);
|
||||
router.get("/topics", topicController.listTopics);
|
||||
router.post("/topics", topicController.addTopic);
|
||||
router.get("/topics/:id/delete", topicController.deleteTopic);
|
||||
|
||||
router.get("/topics/:tId/delete", topicController.deleteTopic);
|
||||
router.get("/topics/:tId", qu);
|
||||
export { router };
|
||||
|
||||
@@ -32,6 +32,14 @@ const getRandQuestion = async (topicId) => {
|
||||
return result.rows[0];
|
||||
};
|
||||
|
||||
const getRandQuestionAPI = async () => {
|
||||
const result = await sql`SELECT * FROM questions ORDER BY RANDOM() LIMIT 1`;
|
||||
if (result.rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return result.rows[0];
|
||||
};
|
||||
|
||||
export {
|
||||
countQuestions,
|
||||
getQuestionsByTopicId,
|
||||
@@ -39,4 +47,5 @@ export {
|
||||
addQuestion,
|
||||
deleteQuestion,
|
||||
getRandQuestion,
|
||||
getRandQuestionAPI,
|
||||
};
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
You could add services here
|
||||
@@ -1 +0,0 @@
|
||||
You could add static content here. Note that you cannot currently submit binary data.
|
||||
4
drill-and-practice/views/correct.eta
Normal file
4
drill-and-practice/views/correct.eta
Normal file
@@ -0,0 +1,4 @@
|
||||
<% layout("./layouts/layout.eta") %>
|
||||
<h1>Your answer is correct!</h1>
|
||||
|
||||
<a href="/quiz/<%= it.tid %>">Next question</a>
|
||||
15
drill-and-practice/views/incorrect.eta
Normal file
15
drill-and-practice/views/incorrect.eta
Normal file
@@ -0,0 +1,15 @@
|
||||
<% layout("./layouts/layout.eta") %>
|
||||
<h1>Your answer is not correct.</h1>
|
||||
|
||||
<h2>Correct answer:</h2>
|
||||
|
||||
<% if (it.data && it.data.length > 0) { %>
|
||||
|
||||
<% it.data.forEach(item => { %>
|
||||
<%= item %>
|
||||
<% }); %>
|
||||
<% } else { %>
|
||||
<p>Correct options are not found.</p>
|
||||
<% } %>
|
||||
|
||||
<a href="/quiz/<%= it.tid %>">Next question</a>
|
||||
@@ -1 +0,0 @@
|
||||
Add any necessary partials here.
|
||||
Reference in New Issue
Block a user