EvalueTonSavoir/server/models/quiz.js

156 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2024-03-29 20:08:34 -04:00
const { ObjectId } = require('mongodb');
const { generateUniqueTitle } = require('./utils');
2024-03-29 20:08:34 -04:00
class Quiz {
constructor(db) {
2024-10-02 14:30:53 -04:00
// console.log("Quiz constructor: db", db)
this.db = db;
}
2024-03-29 20:08:34 -04:00
async create(title, content, folderId, userId) {
// console.log(`quizzes: create title: ${title}, folderId: ${folderId}, userId: ${userId}`);
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);
// console.log("quizzes: create insertOne result", result);
2024-03-29 20:08:34 -04:00
return result.insertedId;
}
async getOwner(quizId) {
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) {
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) {
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) {
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) {
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) {
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) {
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);
}
// 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
const newQuizId = await this.create(newQuizTitle, sourceQuiz.content, sourceQuiz.folderId, userId);
2024-03-29 20:08:34 -04:00
if (!newQuizId) {
throw new Error('Failed to create duplicate quiz');
2024-03-29 20:08:34 -04:00
}
return newQuizId;
}
async quizExists(title, userId) {
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;
}
}
module.exports = Quiz;