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(),