EvalueTonSavoir/server/models/folders.js

179 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
//model
2024-10-01 22:14:38 -04:00
const ObjectId = require('mongodb').ObjectId;
2024-03-29 20:08:34 -04:00
class Folders {
constructor(db, quizModel) {
2024-10-01 22:14:38 -04:00
this.db = db;
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-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 });
if (existingFolder) return 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');
const folder = await foldersCollection.findOne({ _id: ObjectId.createFromTime(folderId) });
2024-03-29 20:08:34 -04:00
return folder.userId;
}
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');
const folderResult = await foldersCollection.deleteOne({ _id: ObjectId.createFromTime(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');
const result = await foldersCollection.updateOne({ _id: ObjectId.createFromTime(folderId) }, { $set: { title: newTitle } })
2024-03-29 20:08:34 -04:00
if (result.modifiedCount != 1) return false;
return true
}
async duplicate(folderId, userId) {
const sourceFolder = await this.getFolderWithContent(folderId);
// Check if the new title already exists
let newFolderTitle = sourceFolder.title + "-copie";
let counter = 1;
while (await this.folderExists(newFolderTitle, userId)) {
newFolderTitle = `${sourceFolder.title}-copie(${counter})`;
counter++;
}
const newFolderId = await this.create(newFolderTitle, userId);
if (!newFolderId) {
throw new Error('Failed to create a duplicate folder.');
}
for (const quiz of sourceFolder.content) {
const { title, content } = quiz;
//console.log(title);
//console.log(content);
2024-10-02 14:09:49 -04:00
await this.quizModel.create(title, content, newFolderId.toString(), userId);
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');
const folder = await foldersCollection.findOne({ _id: ObjectId.createFromTime(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;