This commit is contained in:
AndrewTrieu
2023-03-11 18:19:16 +02:00
parent ee0936c8b1
commit f14618b7cc
16 changed files with 70 additions and 59 deletions

View File

@@ -2,13 +2,13 @@ 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;
return result[0].count;
};
const getAnswersByQuestionId = async (questionId) => {
const result =
await sql`SELECT * FROM question_answer_options WHERE question_id = ${questionId}`;
return result.rows;
return result;
};
const addAnswer = async (questionId, optionText, isCorrect) => {
@@ -23,13 +23,13 @@ const deleteAnswer = async (questionId, optionId) => {
const getCorrectOptionIds = async (questionId) => {
const result =
await sql`SELECT id FROM question_answer_options WHERE question_id = ${questionId} AND is_correct = true`;
return result.rows;
return result;
};
const getCorrectOptions = async (questionId) => {
const result =
await sql`SELECT option_text FROM question_answer_options WHERE question_id = ${questionId} AND is_correct = true`;
return result.rows;
return result;
};
const storeAnswer = async (userId, questionId, optionId) => {

View File

@@ -2,17 +2,17 @@ import { sql } from "../database/database.js";
const countQuestions = async () => {
const result = await sql`SELECT COUNT(id) FROM questions`;
return result.rows[0].count;
return result[0].count;
};
const getQuestionsByTopicId = async (topicId) => {
const result = await sql`SELECT * FROM questions WHERE topic_id = ${topicId}`;
return result.rows;
return result;
};
const getQuestionByQuestionId = async (questionId) => {
const result = await sql`SELECT * FROM questions WHERE id = ${questionId}`;
return result.rows[0];
return result[0];
};
const addQuestion = async (userId, topicId, question) => {
@@ -26,18 +26,18 @@ const deleteQuestion = async (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) {
if (result.length === 0) {
return null;
}
return result.rows[0];
return result[0];
};
const getRandQuestionAPI = async () => {
const result = await sql`SELECT * FROM questions ORDER BY RANDOM() LIMIT 1`;
if (result.rows.length === 0) {
if (result.length === 0) {
return null;
}
return result.rows[0];
return result[0];
};
export {

View File

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