This commit is contained in:
AndrewTrieu
2023-03-12 12:43:33 +02:00
parent f14618b7cc
commit d6d09df845
11 changed files with 28 additions and 31 deletions

View File

@@ -2,7 +2,6 @@ const restrictedPaths = ["/topics", "/quiz"];
const authMiddleware = async (context, next) => {
const user = await context.state.session.get("user");
console.log(user);
if (
!user &&
restrictedPaths.some((path) =>

View File

@@ -6,10 +6,10 @@ const validationRules = {
option: [validasaur.required, validasaur.minLength(1)],
};
const addAnswer = async ({ request, response, params, state, render }) => {
const addAnswer = async ({ request, response, params, context, render }) => {
const topicId = params.tId;
const questionId = params.qId;
const userId = (await state.session.get("user")).id;
const userId = (await context.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, state, render }) => {
const login = async ({ request, response, context, 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, state, render }) => {
return;
}
await state.session.set("user", user);
await context.state.session.set("user", user);
response.redirect("/topics");
};

View File

@@ -2,15 +2,15 @@ import { countTopics } from "../../services/topicService.js";
import { countQuestions } from "../../services/questionService.js";
import { countAnswers } from "../../services/answerService.js";
const showMain = ({ render }) => {
const showMain = async ({ render }) => {
const statistics = {
totalTopics: 0,
totalQuestions: 0,
totalAnswers: 0,
totalTopics: "0",
totalQuestions: "0",
totalAnswers: "0",
};
statistics.totalTopics = countTopics();
statistics.totalQuestions = countQuestions();
statistics.totalAnswers = countAnswers();
statistics.totalTopics = await countTopics();
statistics.totalQuestions = await countQuestions();
statistics.totalAnswers = await countAnswers();
render("main.eta", statistics);
};

View File

@@ -7,16 +7,16 @@ const validationRules = {
question: [validasaur.required, validasaur.minLength(1)],
};
const addQuestion = async ({ request, response, params, state, render }) => {
const addQuestion = async ({ request, response, params, context, render }) => {
const topicId = params.tId;
const userId = (await state.session.get("user")).id;
const userId = (await context.state.session.get("user")).id;
const body = request.body({ type: "form" });
const formData = await body.value;
const topicName = (await topicService.getTopicByTopicId(topicId)).name;
const questionData = {
topicId: topicId,
topicName: topicName,
question: formData.get("question_text"),
question: formData.get("question"),
};
const [passes, errors] = await validasaur.validate(
questionData,
@@ -93,11 +93,11 @@ const listQuizTopics = async ({ render }) => {
});
};
const storeAnswer = async ({ response, params, state }) => {
const storeAnswer = async ({ response, params, context }) => {
const topicId = params.tId;
const questionId = params.qId;
const optionId = params.oId;
const userId = (await state.session.get("user")).id;
const userId = (await context.state.session.get("user")).id;
const correctOptionIds = await answerService.getCorrectOptionIds(questionId);
const correct = correctOptionIds.includes(Number(optionId));
await answerService.storeAnswer(userId, questionId, optionId);

View File

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

View File

@@ -8,6 +8,7 @@ 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,26 +6,23 @@ const addTopic = async (userId, name) => {
const countTopics = async () => {
const result = await sql`SELECT COUNT(id) FROM topics`;
console.log(result);
console.log(result[0].count);
return result[0].count;
};
const getAllTopics = async () => {
const result = await sql`SELECT * FROM topics ORDER BY name ASC`;
console.log(result);
return result;
};
const getTopicsByUserId = async (userId) => {
const result =
await sql`SELECT * FROM topics WHERE user_id = ${userId} ORDER BY name ASC`;
console.log(result);
return result;
};
const getTopicByTopicId = async (topicId) => {
const result = await sql`SELECT * FROM topics WHERE id = ${topicId}`;
console.log(result);
return result[0];
};

View File

@@ -30,7 +30,7 @@
<form method="POST" action="/topics/<%= it.topicId %>/questions/<%= it.id %>/options">
Option:<br/>
<textarea name="option_text"><%= it.optionText ? it.optionText : "" %></textarea><br/>
<input type="text" name="option"/><br/>
Correct:
<input type="checkbox" name="correct"/>
<input type="submit" value="Add"/>

View File

@@ -29,7 +29,7 @@
<form method="POST" action="/topics/<%= it.topicId %>/questions">
Text:<br/>
<textarea name="question_text"><%= it.question ? it.question : "" %></textarea><br/>
<input type="text" name="question"/><br/>
<input type="submit" value="Add"/>
</form>

View File

@@ -36,7 +36,7 @@
<% if (it.admin) { %>
<form method="POST" action="/topics">
Name:<br/>
<input type="text" name="name" value="<%= it.name ? it.name : "" %>"/><br/>
<input type="text" name="name"/><br/>
<input type="submit" value="Add"/>
</form>
<% } %>