2024-03-29 20:08:34 -04:00
|
|
|
const { ObjectId } = require('mongodb');
|
|
|
|
|
|
|
|
|
|
class Quiz {
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(db) {
|
|
|
|
|
console.log("Quiz constructor: db", db)
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
async create(title, content, folderId, 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 })
|
|
|
|
|
|
|
|
|
|
if (existingQuiz) return null;
|
|
|
|
|
|
|
|
|
|
const newQuiz = {
|
|
|
|
|
folderId: folderId,
|
|
|
|
|
userId: userId,
|
|
|
|
|
title: title,
|
|
|
|
|
content: content,
|
|
|
|
|
created_at: new Date(),
|
|
|
|
|
updated_at: new Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await quizCollection.insertOne(newQuiz);
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const quiz = await quizCollection.findOne({ _id: new ObjectId(quizId) });
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const quiz = await quizCollection.findOne({ _id: new ObjectId(quizId) });
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const result = await quizCollection.deleteOne({ _id: new ObjectId(quizId) });
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
await quizzesCollection.deleteMany({ folderId: folderId });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const result = await quizCollection.updateOne({ _id: new ObjectId(quizId) }, { $set: { title: newTitle, content: newContent } });
|
|
|
|
|
//Ne fonctionne pas si rien n'est chngé dans le quiz
|
|
|
|
|
//if (result.modifiedCount != 1) return false;
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
const result = await quizCollection.updateOne({ _id: new ObjectId(quizId) }, { $set: { folderId: newFolderId } });
|
|
|
|
|
|
|
|
|
|
if (result.modifiedCount != 1) return false;
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async duplicate(quizId, userId) {
|
|
|
|
|
|
|
|
|
|
const sourceQuiz = await this.getContent(quizId);
|
|
|
|
|
|
|
|
|
|
let newQuizTitle = `${sourceQuiz.title}-copy`;
|
|
|
|
|
let counter = 1;
|
|
|
|
|
while (await this.quizExists(newQuizTitle, userId)) {
|
|
|
|
|
newQuizTitle = `${sourceQuiz.title}-copy(${counter})`;
|
|
|
|
|
counter++;
|
|
|
|
|
}
|
|
|
|
|
//console.log(newQuizTitle);
|
|
|
|
|
const newQuizId = await this.create(newQuizTitle, sourceQuiz.content,sourceQuiz.folderId, userId);
|
|
|
|
|
|
|
|
|
|
if (!newQuizId) {
|
|
|
|
|
throw new Error('Failed to create a duplicate quiz.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|