Update auth, question, answer

This commit is contained in:
AndrewTrieu
2023-03-09 14:16:17 +02:00
parent 67b0a911c2
commit e6d68e98d6
4 changed files with 81 additions and 1 deletions

View File

@@ -6,3 +6,37 @@ const countQuestions = async () => {
};
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,
};