Update auth, question, answer
This commit is contained in:
@@ -27,7 +27,6 @@ const createUser = async ({ request, response, render }) => {
|
|||||||
response.status = 422;
|
response.status = 422;
|
||||||
userData.errors = errors;
|
userData.errors = errors;
|
||||||
render("register.eta", userData);
|
render("register.eta", userData);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
const hashedPassword = await bcrypt.hash(userData.password);
|
const hashedPassword = await bcrypt.hash(userData.password);
|
||||||
const user = await authServices.createUser(userData.email, hashedPassword);
|
const user = await authServices.createUser(userData.email, hashedPassword);
|
||||||
|
|||||||
@@ -8,5 +8,8 @@ router.get("/", mainController.showMain);
|
|||||||
router.get("/auth/login", authController.showLogin);
|
router.get("/auth/login", authController.showLogin);
|
||||||
router.get("/auth/register", authController.showRegister);
|
router.get("/auth/register", authController.showRegister);
|
||||||
router.post("/auth/login", authController.login);
|
router.post("/auth/login", authController.login);
|
||||||
|
router.get("/topics", topicController.listTopics);
|
||||||
|
router.post("/topics", topicController.addTopic);
|
||||||
|
router.get("/topics/:id/delete", topicController.deleteTopic);
|
||||||
|
|
||||||
export { router };
|
export { router };
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import { sql } from "../database/database.js";
|
||||||
|
|
||||||
|
const countAnswers = async () => {
|
||||||
|
const result = await sql`SELECT COUNT(id) FROM question_answer_options`;
|
||||||
|
return result.rows[0].count;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getAnswersByQuestionId = async (questionId) => {
|
||||||
|
const result =
|
||||||
|
await sql`SELECT * FROM question_answer_options WHERE question_id = ${questionId}`;
|
||||||
|
return result.rows;
|
||||||
|
};
|
||||||
|
|
||||||
|
const addAnswer = async (questionId, optionText, isCorrect) => {
|
||||||
|
await sql`INSERT INTO question_answer_options (question_id, option_text, is_correct) VALUES (${questionId}, ${optionText}, ${isCorrect})`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteAnswer = async (questionId, optionId) => {
|
||||||
|
await sql`DELETE FROM question_answer_options WHERE question_id = ${questionId} AND id = ${optionId}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteAnswerByOptionId = async (optionId) => {
|
||||||
|
await sql`DELETE FROM question_answer_options WHERE id = ${optionId}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCorrectOption = async (questionId) => {
|
||||||
|
const result =
|
||||||
|
await sql`SELECT * FROM question_answer_options WHERE question_id = ${questionId} AND is_correct = true`;
|
||||||
|
return result.rows;
|
||||||
|
};
|
||||||
|
|
||||||
|
const storeAnswer = async (userId, questionId, optionId) => {
|
||||||
|
await sql`INSERT INTO question_answers (user_id, question_id, question_answer_option_id) VALUES (${userId}, ${questionId}, ${optionId})`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
countAnswers,
|
||||||
|
getAnswersByQuestionId,
|
||||||
|
addAnswer,
|
||||||
|
deleteAnswer,
|
||||||
|
deleteAnswerByOptionId,
|
||||||
|
getCorrectOption,
|
||||||
|
storeAnswer,
|
||||||
|
};
|
||||||
|
|||||||
@@ -6,3 +6,37 @@ const countQuestions = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getQuestionsByTopicId = async (topicId) => {
|
const getQuestionsByTopicId = async (topicId) => {
|
||||||
|
const result = await sql`SELECT * FROM questions WHERE topic_id = ${topicId}`;
|
||||||
|
return result.rows;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getQuestionByQuestionId = async (questionId) => {
|
||||||
|
const result = await sql`SELECT * FROM questions WHERE id = ${questionId}`;
|
||||||
|
return result.rows[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
const addQuestion = async (userId, topicId, question) => {
|
||||||
|
await sql`INSERT INTO questions (user_id, topic_id, question_text) VALUES (${userId}, ${topicId}, ${question})`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteQuestion = async (questionId) => {
|
||||||
|
await sql`DELETE FROM questions WHERE id = ${questionId}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getRandQuestion = async (topicId) => {
|
||||||
|
const result =
|
||||||
|
await sql`SELECT * FROM questions WHERE topic_id = ${topicId} ORDER BY RANDOM() LIMIT 1`;
|
||||||
|
if (result.rows.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return result.rows[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
countQuestions,
|
||||||
|
getQuestionsByTopicId,
|
||||||
|
getQuestionByQuestionId,
|
||||||
|
addQuestion,
|
||||||
|
deleteQuestion,
|
||||||
|
getRandQuestion,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user