Clean up, add API

This commit is contained in:
AndrewTrieu
2023-03-10 12:43:41 +02:00
parent 4f94f70a9b
commit 7a76d625e2
10 changed files with 73 additions and 7 deletions

View File

@@ -1 +0,0 @@
You could add project configuration files here.

View 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 };

View File

@@ -1 +0,0 @@
You could add api-related endpoints here.

View File

@@ -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 };

View File

@@ -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,
};

View File

@@ -1 +0,0 @@
You could add services here

View File

@@ -1 +0,0 @@
You could add static content here. Note that you cannot currently submit binary data.

View File

@@ -0,0 +1,4 @@
<% layout("./layouts/layout.eta") %>
<h1>Your answer is correct!</h1>
<a href="/quiz/<%= it.tid %>">Next question</a>

View 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>

View File

@@ -1 +0,0 @@
Add any necessary partials here.