2024-03-29 20:08:34 -04:00
|
|
|
//model
|
2024-10-01 22:14:38 -04:00
|
|
|
const ObjectId = require('mongodb').ObjectId;
|
2024-10-03 15:50:53 -04:00
|
|
|
const { generateUniqueTitle } = require('./utils');
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
class Folders {
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(db, quizModel) {
|
2024-10-01 22:14:38 -04:00
|
|
|
this.db = db;
|
2024-10-02 10:23:56 -04:00
|
|
|
this.quizModel = quizModel;
|
2024-10-01 22:14:38 -04:00
|
|
|
}
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
async create(title, userId) {
|
2024-10-03 21:56:56 -04:00
|
|
|
|
|
|
|
|
console.log("LOG: create", title, userId);
|
|
|
|
|
|
|
|
|
|
if (!title || !userId) {
|
|
|
|
|
throw new Error('Missing required parameter(s)');
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
|
|
|
|
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
|
|
|
|
|
2024-10-03 13:17:14 -04:00
|
|
|
if (existingFolder) {
|
|
|
|
|
throw new Error('Folder already exists');
|
|
|
|
|
}
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const newFolder = {
|
|
|
|
|
userId: userId,
|
|
|
|
|
title: title,
|
|
|
|
|
created_at: new Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await foldersCollection.insertOne(newFolder);
|
|
|
|
|
|
|
|
|
|
return result.insertedId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getUserFolders(userId) {
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
|
|
|
|
const result = await foldersCollection.find({ userId: userId }).toArray();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getOwner(folderId) {
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
2024-10-03 21:56:56 -04:00
|
|
|
const folder = await foldersCollection.findOne({ _id: ObjectId.createFromHexString(folderId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return folder.userId;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 13:17:14 -04:00
|
|
|
// finds all quizzes in a folder
|
2024-03-29 20:08:34 -04:00
|
|
|
async getContent(folderId) {
|
2024-10-01 22:14:38 -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 result = await filesCollection.find({ folderId: folderId }).toArray();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(folderId) {
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const folderResult = await foldersCollection.deleteOne({ _id: ObjectId.createFromHexString(folderId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (folderResult.deletedCount != 1) return false;
|
2024-10-02 14:09:49 -04:00
|
|
|
await this.quizModel.deleteQuizzesByFolderId(folderId);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async rename(folderId, newTitle) {
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect()
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const result = await foldersCollection.updateOne({ _id: ObjectId.createFromHexString(folderId) }, { $set: { title: newTitle } })
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (result.modifiedCount != 1) return false;
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async duplicate(folderId, userId) {
|
2024-10-03 15:50:53 -04:00
|
|
|
console.log("LOG: duplicate", folderId, userId);
|
|
|
|
|
const conn = this.db.getConnection();
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-10-03 15:50:53 -04:00
|
|
|
const sourceFolder = await foldersCollection.findOne({ _id: ObjectId.createFromHexString(folderId), userId: userId });
|
|
|
|
|
if (!sourceFolder) {
|
|
|
|
|
throw new Error(`Folder ${folderId} not found`);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:56:56 -04:00
|
|
|
const theUserId = userId;
|
2024-10-03 15:50:53 -04:00
|
|
|
// Use the utility function to generate a unique title
|
|
|
|
|
const newFolderTitle = await generateUniqueTitle(sourceFolder.title, async (title) => {
|
2024-10-03 21:56:56 -04:00
|
|
|
console.log(`generateUniqueTitle(${title}): userId`, theUserId);
|
|
|
|
|
return await foldersCollection.findOne({ title: title, userId: theUserId });
|
2024-10-03 15:50:53 -04:00
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-10-03 21:56:56 -04:00
|
|
|
const newFolderId = await this.create(newFolderTitle, userId);
|
2024-10-03 15:50:53 -04:00
|
|
|
|
|
|
|
|
if (!newFolderId) {
|
|
|
|
|
throw new Error('Failed to create duplicate folder');
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 21:56:56 -04:00
|
|
|
// copy the quizzes from source folder to destination folder
|
|
|
|
|
const content = await this.getContent(folderId);
|
|
|
|
|
console.log("folders.duplicate: found content", content);
|
|
|
|
|
for (const quiz of content) {
|
|
|
|
|
console.log("folders.duplicate: creating quiz (copy)", quiz);
|
|
|
|
|
const result = await this.quizModel.create(quiz.title, quiz.content, newFolderId.toString(), userId);
|
|
|
|
|
if (!result) {
|
|
|
|
|
throw new Error('Failed to create duplicate quiz');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return newFolderId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async folderExists(title, userId) {
|
2024-10-01 22:14:38 -04:00
|
|
|
console.log("LOG: folderExists", title, userId);
|
|
|
|
|
await this.db.connect();
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
|
|
|
|
|
2024-10-01 22:14:38 -04:00
|
|
|
return !!existingFolder;
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async copy(folderId, userId) {
|
|
|
|
|
|
|
|
|
|
const sourceFolder = await this.getFolderWithContent(folderId);
|
|
|
|
|
const newFolderId = await this.create(sourceFolder.title, userId);
|
|
|
|
|
if (!newFolderId) {
|
|
|
|
|
throw new Error('Failed to create a new folder.');
|
|
|
|
|
}
|
|
|
|
|
for (const quiz of sourceFolder.content) {
|
2024-10-02 14:09:49 -04:00
|
|
|
await this.quizModel.create(quiz.title, quiz.content, newFolderId, userId);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newFolderId;
|
|
|
|
|
}
|
2024-10-01 22:14:38 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
async getFolderById(folderId) {
|
2024-10-01 22:14:38 -04:00
|
|
|
await this.db.connect();
|
|
|
|
|
const conn = this.db.getConnection();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
const foldersCollection = conn.collection('folders');
|
|
|
|
|
|
2024-10-03 12:05:20 -04:00
|
|
|
const folder = await foldersCollection.findOne({ _id: ObjectId.createFromHexString(folderId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-10-01 22:14:38 -04:00
|
|
|
if (!folder) return new Error(`Folder ${folderId} not found`);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return folder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getFolderWithContent(folderId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const folder = await this.getFolderById(folderId);
|
|
|
|
|
|
|
|
|
|
const content = await this.getContent(folderId);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...folder,
|
|
|
|
|
content: content
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 22:14:38 -04:00
|
|
|
module.exports = Folders;
|