2024-03-29 20:08:34 -04:00
|
|
|
const { ObjectId } = require('mongodb');
|
2024-10-03 15:50:53 -04:00
|
|
|
const { generateUniqueTitle } = require('./utils');
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
class Quiz {
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(db) {
|
2024-10-02 14:30:53 -04:00
|
|
|
// console.log("Quiz constructor: db", db)
|
2024-10-02 10:23:56 -04:00
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
async create(title, content, folderId, userId) {
|
2024-10-03 21:56:56 -04:00
|
|
|
console.log(`quizzes: create title: ${title}, folderId: ${folderId}, userId: ${userId}`);
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
|
|
|
|
const existingQuiz = await quizCollection.findOne({ title: title, folderId: folderId, userId: userId })
|
|
|
|
|
|
2024-10-03 21:56:56 -04:00
|
|
|
if (existingQuiz) {
|
|
|
|
|
throw new Error(`Quiz already exists with title: ${title}, folderId: ${folderId}, userId: ${userId}`);
|
|
|
|
|
}
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const newQuiz = {
|
|
|
|
|
folderId: folderId,
|
|
|
|
|
userId: userId,
|
|
|
|
|
title: title,
|
|
|
|
|
content: content,
|
|
|
|
|
created_at: new Date(),
|
|
|
|
|
updated_at: new Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await quizCollection.insertOne(newQuiz);
|
2024-10-03 21:56:56 -04:00
|
|
|
console.log("quizzes: create insertOne result", result);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return result.insertedId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getOwner(quizId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const quiz = await quizCollection.findOne({ _id: ObjectId.createFromHexString(quizId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return quiz.userId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getContent(quizId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const quiz = await quizCollection.findOne({ _id: ObjectId.createFromHexString(quizId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return quiz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(quizId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const result = await quizCollection.deleteOne({ _id: ObjectId.createFromHexString(quizId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (result.deletedCount != 1) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
async deleteQuizzesByFolderId(folderId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect();
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizzesCollection = conn.collection('files');
|
|
|
|
|
|
|
|
|
|
// Delete all quizzes with the specified folderId
|
2024-10-03 12:05:20 -04:00
|
|
|
const result = await quizzesCollection.deleteMany({ folderId: folderId });
|
|
|
|
|
return result.deletedCount > 0;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(quizId, newTitle, newContent) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const result = await quizCollection.updateOne(
|
|
|
|
|
{ _id: ObjectId.createFromHexString(quizId) },
|
|
|
|
|
{
|
|
|
|
|
$set: {
|
|
|
|
|
title: newTitle,
|
|
|
|
|
content: newContent,
|
|
|
|
|
updated_at: new Date()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result.modifiedCount === 1;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async move(quizId, newFolderId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const result = await quizCollection.updateOne(
|
|
|
|
|
{ _id: ObjectId.createFromHexString(quizId) },
|
|
|
|
|
{ $set: { folderId: newFolderId } }
|
|
|
|
|
);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (result.modifiedCount != 1) return false;
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async duplicate(quizId, userId) {
|
2024-10-03 15:50:53 -04:00
|
|
|
const conn = this.db.getConnection();
|
|
|
|
|
const quizCollection = conn.collection('files');
|
|
|
|
|
|
|
|
|
|
const sourceQuiz = await quizCollection.findOne({ _id: ObjectId.createFromHexString(quizId), userId: userId });
|
2024-10-03 12:05:20 -04:00
|
|
|
if (!sourceQuiz) {
|
|
|
|
|
throw new Error('Quiz not found for quizId: ' + quizId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 15:50:53 -04:00
|
|
|
// Use the utility function to generate a unique title
|
|
|
|
|
const newQuizTitle = await generateUniqueTitle(sourceQuiz.title, async (title) => {
|
|
|
|
|
return await quizCollection.findOne({ title: title, folderId: sourceQuiz.folderId, userId: userId });
|
|
|
|
|
});
|
2024-10-03 12:05:20 -04:00
|
|
|
|
2024-10-03 15:50:53 -04:00
|
|
|
const newQuizId = await this.create(newQuizTitle, sourceQuiz.content, sourceQuiz.folderId, userId);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (!newQuizId) {
|
2024-10-03 15:50:53 -04:00
|
|
|
throw new Error('Failed to create duplicate quiz');
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newQuizId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async quizExists(title, userId) {
|
2024-10-02 10:23:56 -04:00
|
|
|
await this.db.connect();
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const filesCollection = conn.collection('files');
|
|
|
|
|
const existingFolder = await filesCollection.findOne({ title: title, userId: userId });
|
|
|
|
|
|
|
|
|
|
return existingFolder !== null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
module.exports = Quiz;
|