-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
144 lines (132 loc) · 3.64 KB
/
Copy pathdb.js
File metadata and controls
144 lines (132 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**************************************************
* db.js
**************************************************/
const fs = require("fs");
const path = require("path");
const sqlite3 = require("sqlite3").verbose();
// Ensure .data folder for the DB
const dataDir = path.join(__dirname, ".data");
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir);
const dbPath = path.join(dataDir, "quiz.db");
const db = new sqlite3.Database(dbPath, (err) => {
if (err) console.error("Error opening database:", err);
else console.log("Connected to SQLite at", dbPath);
});
db.serialize(() => {
db.run(`
CREATE TABLE IF NOT EXISTS quizzes (
id TEXT PRIMARY KEY,
question TEXT,
misconceptions TEXT, -- JSON array
correctAnswers TEXT, -- JSON array
started INTEGER DEFAULT 0,
ended INTEGER DEFAULT 0
)
`);
db.run(`
CREATE TABLE IF NOT EXISTS responses (
quizId TEXT,
username TEXT,
response TEXT,
PRIMARY KEY(quizId, username)
)
`);
});
/**
* Creates a new quiz record.
*/
function createQuiz(question, misconceptions = [], correctAnswers = []) {
return new Promise((resolve, reject) => {
const quizId = "quiz-" + Math.floor(Math.random() * 100000);
const misStr = JSON.stringify(misconceptions);
const corrStr = JSON.stringify(correctAnswers);
const stmt = db.prepare(`
INSERT INTO quizzes (id, question, misconceptions, correctAnswers, started, ended)
VALUES (?, ?, ?, ?, 0, 0)
`);
stmt.run(quizId, question, misStr, corrStr, function (err) {
if (err) return reject(err);
resolve(quizId);
});
});
}
/**
* Marks quiz as started.
*/
function startQuiz(quizId) {
return new Promise((resolve, reject) => {
const stmt = db.prepare("UPDATE quizzes SET started = 1 WHERE id = ?");
stmt.run(quizId, function (err) {
if (err) return reject(err);
resolve();
});
});
}
/**
* Marks quiz as ended/closed.
*/
function closeQuiz(quizId) {
return new Promise((resolve, reject) => {
const stmt = db.prepare("UPDATE quizzes SET ended = 1 WHERE id = ?");
stmt.run(quizId, function (err) {
if (err) return reject(err);
resolve();
});
});
}
/**
* Retrieves a quiz record by ID.
*/
function getQuiz(quizId) {
return new Promise((resolve, reject) => {
db.get("SELECT * FROM quizzes WHERE id = ?", [quizId], (err, row) => {
if (err) return reject(err);
if (!row) return resolve(null);
const misconceptions = row.misconceptions ? JSON.parse(row.misconceptions) : [];
const correctAnswers = row.correctAnswers ? JSON.parse(row.correctAnswers) : [];
resolve({
id: row.id,
question: row.question,
misconceptions,
correctAnswers,
started: row.started,
ended: row.ended
});
});
});
}
/**
* Inserts/updates a student response.
*/
function storeResponse(quizId, username, response) {
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
INSERT INTO responses (quizId, username, response)
VALUES (?, ?, ?)
ON CONFLICT(quizId, username) DO UPDATE SET response=excluded.response
`);
stmt.run(quizId, username, response, function (err) {
if (err) return reject(err);
resolve();
});
});
}
/**
* Returns all responses for a quiz.
*/
function getAllResponses(quizId) {
return new Promise((resolve, reject) => {
db.all("SELECT username, response FROM responses WHERE quizId = ?", [quizId], (err, rows) => {
if (err) return reject(err);
resolve(rows || []);
});
});
}
module.exports = {
createQuiz,
startQuiz,
closeQuiz,
getQuiz,
storeResponse,
getAllResponses
};