diff --git a/server/models/folder.ts b/server/models/folder.ts new file mode 100644 index 0000000..4c99335 --- /dev/null +++ b/server/models/folder.ts @@ -0,0 +1,166 @@ +import db from '../config/db'; +import { ObjectId } from 'mongodb'; +import { Quiz } from './quiz'; +import { User } from './user'; + +export class Folder { + private created_at: Date; + + constructor (public title: string, public user: User) { + this.title = title; + this.user = user; + this.created_at = new Date(); + } + + // async create(title: string, userId: string): Promise { + // // await db.connect(); + // // const conn = db.getConnection(); + + // // const foldersCollection = conn.collection('folders'); + + // // const existingFolder = await foldersCollection.findOne({ title, userId }); + + // // if (existingFolder) throw new Error('Folder already exists'); + + // // const newFolder = { + // // userId, + // // title, + // // created_at: new Date(), + // // }; + + // // const result = await foldersCollection.insertOne(newFolder); + + // // return result.insertedId; + // } + + // async getUserFolders(userId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + + // const result = await foldersCollection.find({ userId }).toArray(); + + // return result; + // } + + // async getOwner(folderId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + + // const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); + + // return folder.userId; + // } + + // async getContent(folderId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const filesCollection = conn.collection('files'); + + // const result = await filesCollection.find({ folderId }).toArray(); + + // return result; + // } + + // async delete(folderId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + + // const folderResult = await foldersCollection.deleteOne({ _id: new ObjectId(folderId) }); + + // if (folderResult.deletedCount !== 1) return false; + // await Quiz.deleteQuizzesByFolderId(folderId); + + // return true; + // } + + // async rename(folderId: string, newTitle: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + + // const result = await foldersCollection.updateOne({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } }); + + // if (result.modifiedCount !== 1) return false; + + // return true; + // } + + // async duplicate(folderId: string, userId: string): Promise { + // const sourceFolder = await this.getFolderWithContent(folderId); + + // 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; + // await Quiz.create(title, content, newFolderId.toString(), userId); + // } + + // return newFolderId; + // } + + // async folderExists(title: string, userId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + // const existingFolder = await foldersCollection.findOne({ title, userId }); + + // return existingFolder !== null; + // } + + // async copy(folderId: string, userId: string): Promise { + // 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) { + // await this.createQuiz(quiz.title, quiz.content, newFolderId, userId); + // } + + // return newFolderId; + // } + + // async getFolderById(folderId: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const foldersCollection = conn.collection('folders'); + + // const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); + + // return folder; + // } + + // async getFolderWithContent(folderId: string): Promise { + // const folder = await this.getFolderById(folderId); + // const content = await this.getContent(folderId); + + // return { + // ...folder, + // content, + // }; + // } +} diff --git a/server/models/folders.ts b/server/models/folders.ts deleted file mode 100644 index 5ecfca5..0000000 --- a/server/models/folders.ts +++ /dev/null @@ -1,174 +0,0 @@ -//model -const db = require('../config/db.js') -const { ObjectId } = require('mongodb'); -const Quiz = require('./quiz.js'); - -class Folders { - - async create(title, userId) { - await db.connect() - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const existingFolder = await foldersCollection.findOne({ title: title, userId: userId }); - - if (existingFolder) return new Error('Folder already exists'); - - const newFolder = { - userId: userId, - title: title, - created_at: new Date() - } - - const result = await foldersCollection.insertOne(newFolder); - - return result.insertedId; - } - - async getUserFolders(userId) { - await db.connect() - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const result = await foldersCollection.find({ userId: userId }).toArray(); - - return result; - } - - async getOwner(folderId) { - await db.connect() - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); - - return folder.userId; - } - - async getContent(folderId) { - await db.connect() - const conn = db.getConnection(); - - const filesCollection = conn.collection('files'); - - const result = await filesCollection.find({ folderId: folderId }).toArray(); - - return result; - } - - async delete(folderId) { - await db.connect() - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const folderResult = await foldersCollection.deleteOne({ _id: new ObjectId(folderId) }); - - if (folderResult.deletedCount != 1) return false; - await Quiz.deleteQuizzesByFolderId(folderId); - - return true; - } - - async rename(folderId, newTitle) { - await db.connect() - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const result = await foldersCollection.updateOne({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } }) - - 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); - await Quiz.create(title, content, newFolderId.toString(), userId); - } - - return newFolderId; - - } - - async folderExists(title, userId) { - await db.connect(); - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - const existingFolder = await foldersCollection.findOne({ title: title, userId: userId }); - - return existingFolder !== null; - } - - - 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) { - await this.createQuiz(quiz.title, quiz.content, newFolderId, userId); - } - - return newFolderId; - - } - async getFolderById(folderId) { - await db.connect(); - const conn = db.getConnection(); - - const foldersCollection = conn.collection('folders'); - - const folder = await foldersCollection.findOne({ _id: new ObjectId(folderId) }); - - return folder; - } - - - async getFolderWithContent(folderId) { - - - const folder = await this.getFolderById(folderId); - - const content = await this.getContent(folderId); - - return { - ...folder, - content: content - }; - - } - -} - -module.exports = new Folders; diff --git a/server/models/images.ts b/server/models/image.ts similarity index 61% rename from server/models/images.ts rename to server/models/image.ts index 5dfa954..8dc8508 100644 --- a/server/models/images.ts +++ b/server/models/image.ts @@ -1,10 +1,9 @@ -const db = require('../config/db.js') -const { ObjectId } = require('mongodb'); +import db from '../config/db'; +import { ObjectId } from 'mongodb'; -class Images { - - async upload(file, userId) { - await db.connect() +export class Image { + async upload(file: Express.Multer.File, userId: string): Promise { + await db.connect(); const conn = db.getConnection(); const imagesCollection = conn.collection('images'); @@ -14,16 +13,16 @@ class Images { file_name: file.originalname, file_content: file.buffer.toString('base64'), mime_type: file.mimetype, - created_at: new Date() + created_at: new Date(), }; const result = await imagesCollection.insertOne(newImage); - return result.insertedId; + return result.insertedId.toString(); } - async get(id) { - await db.connect() + async get(id: string): Promise<{ file_name: string; file_content: Buffer; mime_type: string } | null> { + await db.connect(); const conn = db.getConnection(); const imagesCollection = conn.collection('images'); @@ -35,10 +34,9 @@ class Images { return { file_name: result.file_name, file_content: Buffer.from(result.file_content, 'base64'), - mime_type: result.mime_type + mime_type: result.mime_type, }; } - } -module.exports = new Images; \ No newline at end of file +export default new Image(); diff --git a/server/models/user.ts b/server/models/user.ts new file mode 100644 index 0000000..40526c2 --- /dev/null +++ b/server/models/user.ts @@ -0,0 +1,128 @@ +import db from '../config/db'; +import bcrypt from 'bcrypt'; +import AppError from '../middleware/AppError'; +import { USER_ALREADY_EXISTS } from '../constants/errorCodes'; +import { Folder } from './folder'; +import { Image } from './image'; +import { Quiz } from './quiz'; + +export class User { + constructor(public email: string, public hashedPassword: string, public created_at?: Date, public _id?: string, public folders?: Array, public images?: Array, public quizzes?: Array) { + this.email = email; + this.hashedPassword = hashedPassword; + this.created_at = created_at || new Date(); + this.folders = folders || new Array(); + this.images = images || new Array(); + this.quizzes = quizzes || new Array(); + } + + // async hashPassword(password: string): Promise { + // return await bcrypt.hash(password, 10); + // } + + // generatePassword(): string { + // return Math.random().toString(36).slice(-8); + // } + + // async verify(password: string, hash: string): Promise { + // return await bcrypt.compare(password, hash); + // } + + // async register(email: string, password: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const userCollection = conn.collection('users'); + + // const existingUser = await userCollection.findOne({ email }); + + // if (existingUser) { + // throw new AppError(USER_ALREADY_EXISTS); + // } + + // const newUser = { + // email, + // password: await this.hashPassword(password), + // created_at: new Date(), + // }; + + // await userCollection.insertOne(newUser); + + // const folderTitle = 'Dossier par Défaut'; + // const userId = newUser._id.toString(); + // await Folders.create(folderTitle, userId); + + // // TODO: verif if inserted properly... + // } + + // async login(email: string, password: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const userCollection = conn.collection('users'); + + // const user = await userCollection.findOne({ email }); + + // if (!user) { + // return false; + // } + + // const passwordMatch = await this.verify(password, user.password); + + // if (!passwordMatch) { + // return false; + // } + + // return user; + // } + + // async resetPassword(email: string): Promise { + // const newPassword = this.generatePassword(); + + // return await this.changePassword(email, newPassword); + // } + + // async changePassword(email: string, newPassword: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const userCollection = conn.collection('users'); + + // const hashedPassword = await this.hashPassword(newPassword); + + // const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } }); + + // if (result.modifiedCount !== 1) return null; + + // return newPassword; + // } + + // async delete(email: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const userCollection = conn.collection('users'); + + // const result = await userCollection.deleteOne({ email }); + + // if (result.deletedCount !== 1) return false; + + // return true; + // } + + // async getId(email: string): Promise { + // await db.connect(); + // const conn = db.getConnection(); + + // const userCollection = conn.collection('users'); + + // const user = await userCollection.findOne({ email }); + + // if (!user) { + // return false; + // } + + // return user._id; + // } +} + diff --git a/server/models/users.ts b/server/models/users.ts deleted file mode 100644 index 3790fce..0000000 --- a/server/models/users.ts +++ /dev/null @@ -1,121 +0,0 @@ -//user -const db = require('../config/db.js'); -const bcrypt = require('bcrypt'); -const AppError = require('../middleware/AppError.js'); -const { USER_ALREADY_EXISTS } = require('../constants/errorCodes'); -const Folders = require('../models/folders.js'); - -class Users { - - async hashPassword(password) { - return await bcrypt.hash(password, 10) - } - - generatePassword() { - return Math.random().toString(36).slice(-8); - } - - async verify(password, hash) { - return await bcrypt.compare(password, hash) - } - - async register(email, password) { - await db.connect() - const conn = db.getConnection(); - - const userCollection = conn.collection('users'); - - const existingUser = await userCollection.findOne({ email: email }); - - if (existingUser) { - throw new AppError(USER_ALREADY_EXISTS); - } - - const newUser = { - email: email, - password: await this.hashPassword(password), - created_at: new Date() - }; - - await userCollection.insertOne(newUser); - - const folderTitle = 'Dossier par Défaut'; - const userId = newUser._id.toString(); - await Folders.create(folderTitle, userId); - - // TODO: verif if inserted properly... - } - - async login(email, password) { - await db.connect() - const conn = db.getConnection(); - - const userCollection = conn.collection('users'); - - const user = await userCollection.findOne({ email: email }); - - if (!user) { - return false; - } - - const passwordMatch = await this.verify(password, user.password); - - if (!passwordMatch) { - return false; - } - - return user; - } - - async resetPassword(email) { - const newPassword = this.generatePassword(); - - return await this.changePassword(email, newPassword); - } - - async changePassword(email, newPassword) { - await db.connect() - const conn = db.getConnection(); - - const userCollection = conn.collection('users'); - - const hashedPassword = await this.hashPassword(newPassword); - - const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } }); - - if (result.modifiedCount != 1) return null; - - return newPassword - } - - async delete(email) { - await db.connect() - const conn = db.getConnection(); - - const userCollection = conn.collection('users'); - - const result = await userCollection.deleteOne({ email }); - - if (result.deletedCount != 1) return false; - - return true; - } - - async getId(email) { - await db.connect() - const conn = db.getConnection(); - - const userCollection = conn.collection('users'); - - const user = await userCollection.findOne({ email: email }); - - if (!user) { - return false; - } - - return user._id; - } - -} - -module.exports = new Users;