@defikitdotnet/education-module-ai
Version:
AI Education Module using Agent Framework
107 lines (106 loc) • 5.28 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuizSubmissionRepository = void 0;
var uuid_1 = require("uuid");
var QuizSubmissionRepository = /** @class */ (function () {
function QuizSubmissionRepository(db) {
this.db = db;
this.init();
}
QuizSubmissionRepository.prototype.init = function () {
// Create the quiz_submissions table if it doesn't exist
var createTableSql = "\n CREATE TABLE IF NOT EXISTS quiz_submissions (\n id TEXT PRIMARY KEY,\n quizId TEXT NOT NULL,\n studentId TEXT NOT NULL, \n score INTEGER NOT NULL, -- Store score as integer percentage\n answers TEXT NOT NULL, -- Store answers as JSON string\n submittedAt TEXT NOT NULL -- Store timestamp as ISO string\n -- FOREIGN KEY (quizId) REFERENCES quizzes(id) ON DELETE CASCADE, -- Optional: Add if quizzes table exists\n -- FOREIGN KEY (studentId) REFERENCES users(id) ON DELETE CASCADE -- Optional: Add if users table exists\n );\n ";
this.db.exec(createTableSql);
console.log("Quiz submissions table initialized.");
};
/**
* Creates a new quiz submission record in the database.
* @param submissionData - The data for the new submission.
* @returns The newly created QuizSubmission object with id and timestamp.
*/
QuizSubmissionRepository.prototype.create = function (submissionData) {
var newSubmission = __assign(__assign({}, submissionData), { id: (0, uuid_1.v4)(), submittedAt: new Date() });
var insertSql = "\n INSERT INTO quiz_submissions (id, quizId, studentId, score, answers, submittedAt)\n VALUES (?, ?, ?, ?, ?, ?)\n ";
try {
var stmt = this.db.prepare(insertSql);
stmt.run(newSubmission.id, newSubmission.quizId, newSubmission.studentId, newSubmission.score, newSubmission.answers, // Already a JSON string
newSubmission.submittedAt.toISOString() // Store date as ISO string
);
console.log("Created quiz submission with ID: ".concat(newSubmission.id));
return newSubmission;
}
catch (error) {
console.error("Error creating quiz submission:", error);
// Re-throw the error or handle it as appropriate
throw new Error("Failed to create quiz submission in database.");
}
};
// --- Optional: Add methods to find submissions ---
/**
* Finds submissions by student ID.
* @param studentId - The ID of the student.
* @returns An array of QuizSubmission objects.
*/
QuizSubmissionRepository.prototype.findByStudentId = function (studentId) {
var selectSql = "SELECT * FROM quiz_submissions WHERE studentId = ? ORDER BY submittedAt DESC";
try {
var stmt = this.db.prepare(selectSql);
var rows = stmt.all(studentId);
return rows.map(function (row) { return (__assign(__assign({}, row), { submittedAt: new Date(row.submittedAt) })); });
}
catch (error) {
console.error("Error finding submissions for student ".concat(studentId, ":"), error);
return [];
}
};
/**
* Finds submissions by quiz ID.
* @param quizId - The ID of the quiz.
* @returns An array of QuizSubmission objects.
*/
QuizSubmissionRepository.prototype.findByQuizId = function (quizId) {
var selectSql = "SELECT * FROM quiz_submissions WHERE quizId = ? ORDER BY submittedAt DESC";
try {
var stmt = this.db.prepare(selectSql);
var rows = stmt.all(quizId);
return rows.map(function (row) { return (__assign(__assign({}, row), { submittedAt: new Date(row.submittedAt) })); });
}
catch (error) {
console.error("Error finding submissions for quiz ".concat(quizId, ":"), error);
return [];
}
};
/**
* Finds a specific submission by its ID.
* @param id - The ID of the submission.
* @returns The QuizSubmission object or null if not found.
*/
QuizSubmissionRepository.prototype.findById = function (id) {
var selectSql = "SELECT * FROM quiz_submissions WHERE id = ?";
try {
var stmt = this.db.prepare(selectSql);
var row = stmt.get(id);
if (row) {
return __assign(__assign({}, row), { submittedAt: new Date(row.submittedAt) });
}
return null;
}
catch (error) {
console.error("Error finding submission with ID ".concat(id, ":"), error);
return null;
}
};
return QuizSubmissionRepository;
}());
exports.QuizSubmissionRepository = QuizSubmissionRepository;