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 authMiddleware = async (context, next) => {
const user = await context.state.session.get("user"); const user = await context.state.session.get("user");
console.log(user);
if ( if (
!user && !user &&
restrictedPaths.some((path) => restrictedPaths.some((path) =>

View File

@@ -6,10 +6,10 @@ const validationRules = {
option: [validasaur.required, validasaur.minLength(1)], 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 topicId = params.tId;
const questionId = params.qId; 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 body = request.body({ type: "form" });
const formData = await body.value; const formData = await body.value;
const answerData = { 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 body = request.body({ type: "form" });
const params = await body.value; const params = await body.value;
const userDatabase = await authService.findUser(params.get("email")); const userDatabase = await authService.findUser(params.get("email"));
@@ -58,7 +58,7 @@ const login = async ({ request, response, state, render }) => {
return; return;
} }
await state.session.set("user", user); await context.state.session.set("user", user);
response.redirect("/topics"); response.redirect("/topics");
}; };

View File

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

View File

@@ -7,16 +7,16 @@ const validationRules = {
question: [validasaur.required, validasaur.minLength(1)], 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 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 body = request.body({ type: "form" });
const formData = await body.value; const formData = await body.value;
const topicName = (await topicService.getTopicByTopicId(topicId)).name; const topicName = (await topicService.getTopicByTopicId(topicId)).name;
const questionData = { const questionData = {
topicId: topicId, topicId: topicId,
topicName: topicName, topicName: topicName,
question: formData.get("question_text"), question: formData.get("question"),
}; };
const [passes, errors] = await validasaur.validate( const [passes, errors] = await validasaur.validate(
questionData, 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 topicId = params.tId;
const questionId = params.qId; const questionId = params.qId;
const optionId = params.oId; 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 correctOptionIds = await answerService.getCorrectOptionIds(questionId);
const correct = correctOptionIds.includes(Number(optionId)); const correct = correctOptionIds.includes(Number(optionId));
await answerService.storeAnswer(userId, questionId, optionId); await answerService.storeAnswer(userId, questionId, optionId);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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