Clean up, add API
This commit is contained in:
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 };
|
||||
|
||||
Reference in New Issue
Block a user