This commit is contained in:
AndrewTrieu
2023-03-12 16:21:18 +02:00
parent d6d09df845
commit b3065cfce1
10 changed files with 30 additions and 27 deletions

View File

@@ -6,10 +6,10 @@ const validationRules = {
option: [validasaur.required, validasaur.minLength(1)],
};
const addAnswer = async ({ request, response, params, context, render }) => {
const addAnswer = async ({ request, response, params, state, render }) => {
const topicId = params.tId;
const questionId = params.qId;
const userId = (await context.state.session.get("user")).id;
const userId = (await state.session.get("user")).id;
const body = request.body({ type: "form" });
const formData = await body.value;
const answerData = {

View File

@@ -36,7 +36,7 @@ const register = async ({ request, response, render }) => {
}
};
const login = async ({ request, response, context, render }) => {
const login = async ({ request, response, state, render }) => {
const body = request.body({ type: "form" });
const params = await body.value;
const userDatabase = await authService.findUser(params.get("email"));
@@ -58,7 +58,7 @@ const login = async ({ request, response, context, render }) => {
return;
}
await context.state.session.set("user", user);
await state.session.set("user", user);
response.redirect("/topics");
};

View File

@@ -7,9 +7,9 @@ const validationRules = {
question: [validasaur.required, validasaur.minLength(1)],
};
const addQuestion = async ({ request, response, params, context, render }) => {
const addQuestion = async ({ request, response, params, state, render }) => {
const topicId = params.tId;
const userId = (await context.state.session.get("user")).id;
const userId = (await state.session.get("user")).id;
const body = request.body({ type: "form" });
const formData = await body.value;
const topicName = (await topicService.getTopicByTopicId(topicId)).name;
@@ -83,7 +83,7 @@ const getRandQuestion = async ({ params, response }) => {
if (randQuestion === null) {
response.body = "No questions in this topic!";
} else {
response.redirect(`/topics/${topicId}/questions/${randQuestion.id}/quiz`);
response.redirect(`/quiz/${topicId}/questions/${randQuestion.id}`);
}
};
@@ -93,12 +93,15 @@ const listQuizTopics = async ({ render }) => {
});
};
const storeAnswer = async ({ response, params, context }) => {
const storeAnswer = async ({ response, params, state }) => {
const topicId = params.tId;
const questionId = params.qId;
const optionId = params.oId;
const userId = (await context.state.session.get("user")).id;
const correctOptionIds = await answerService.getCorrectOptionIds(questionId);
const userId = (await state.session.get("user")).id;
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) {
@@ -115,9 +118,10 @@ const showCorrect = async ({ params, render }) => {
const showIncorrect = async ({ params, render }) => {
const questionId = params.qId;
const correctOptions = {
data: [await answerService.getCorrectOptions(questionId)],
data: await answerService.getCorrectOptions(questionId),
tId: params.tId,
};
console.log(correctOptions);
render("incorrect.eta", correctOptions);
};

View File

@@ -5,9 +5,9 @@ const validationRules = {
name: [validasaur.required, validasaur.minLength(1)],
};
const addTopic = async ({ request, response, render, context }) => {
const userId = (await context.state.session.get("user")).id;
const admin = (await context.state.session.get("user")).admin;
const addTopic = async ({ request, response, render, state }) => {
const userId = (await state.session.get("user")).id;
const admin = (await state.session.get("user")).admin;
const body = request.body({ type: "form" });
const params = await body.value;
const topicData = {
@@ -33,17 +33,17 @@ const addTopic = async ({ request, response, render, context }) => {
}
};
const deleteTopic = async ({ params, response, context }) => {
const deleteTopic = async ({ params, response, state }) => {
const topicId = params.tId;
const admin = (await context.state.session.get("user")).admin;
const admin = (await state.session.get("user")).admin;
if (admin) {
await topicService.deleteTopic(topicId);
}
response.redirect("/topics");
};
const listTopics = async ({ render, context }) => {
const user = await context.state.session.get("user");
const listTopics = async ({ render, state }) => {
const user = await state.session.get("user");
render("topics.eta", {
admin: user.admin,
topics: await topicService.getAllTopics(),

View File

@@ -8,7 +8,6 @@ const countAnswers = async () => {
const getAnswersByQuestionId = async (questionId) => {
const result =
await sql`SELECT * FROM question_answer_options WHERE question_id = ${questionId}`;
console.log(result);
return result;
};

View File

@@ -6,7 +6,6 @@ const addTopic = async (userId, name) => {
const countTopics = async () => {
const result = await sql`SELECT COUNT(id) FROM topics`;
console.log(result[0].count);
return result[0].count;
};

View File

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

View File

@@ -4,12 +4,13 @@
<h2>Correct answer:</h2>
<% if (it.data && it.data.length > 0) { %>
<ul>
<% it.data.forEach(item => { %>
<%= item %>
<li><%= item.option_text %></li>
<% }); %>
<ul>
<% } else { %>
<p>Correct options are not found.</p>
<% } %>
<a href="/quiz/<%= it.tid %>">Next question</a>
<a href="/quiz/<%= it.tId %>">Next question</a>

View File

@@ -6,7 +6,7 @@
<% it.details.forEach((item) => { %>
<p><form action="/topics/<%= it.topicId %>/questions/<%= it.id %>/options/<%= item.id %>/delete" method="POST">
Content: <%= item.option_text %>; Correctness: <%= item.is_correct %>
<input type="submit" value="Delete pption" />
<input type="submit" value="Delete option" />
</form></p>
<% }); %>
<% } else { %>
@@ -16,7 +16,7 @@
</form>
<% } %>
<h2>Add an answer pption</h2>
<h2>Add an answer option</h2>
<% if (it.errors) { %>
<ul>

View File

@@ -8,7 +8,7 @@
<% it.topics.forEach(topic => { %>
<p>
<li>
<a href="/quiz/<%= item.id %>"><%= item.name %></a>
<a href="/quiz/<%= topic.id %>"><%= topic.name %></a>
</li>
</p>
<% }); %>