This repository has been archived on 2025-12-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
quizzy-application/drill-and-practice/services/questionService.js
2023-03-09 14:16:17 +02:00

43 lines
1.1 KiB
JavaScript

import { sql } from "../database/database.js";
const countQuestions = async () => {
const result = await sql`SELECT COUNT(id) FROM questions`;
return result.rows[0].count;
};
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,
};