mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
import appError dans le model
This commit is contained in:
parent
068f97ac47
commit
d2bf18b88d
2 changed files with 158 additions and 144 deletions
|
|
@ -45,10 +45,13 @@ function App() {
|
||||||
{/* Page main */}
|
{/* Page main */}
|
||||||
<Route path="/" element={<Home />} />
|
<Route path="/" element={<Home />} />
|
||||||
|
|
||||||
{/* Pages espace enseignant */}
|
{/* Public routes */}
|
||||||
<Route path="/teacher/login" element={<Login />} />
|
<Route path="/teacher/login" element={<Login />} />
|
||||||
<Route path="/teacher/register" element={<Register />} />
|
<Route path="/teacher/register" element={<Register />} />
|
||||||
<Route path="/teacher/resetPassword" element={<ResetPassword />} />
|
<Route path="/teacher/resetPassword" element={<ResetPassword />} />
|
||||||
|
|
||||||
|
{/* Pages espace enseignant */}
|
||||||
|
|
||||||
<Route path="/teacher/dashboard" element={<Dashboard />} />
|
<Route path="/teacher/dashboard" element={<Dashboard />} />
|
||||||
<Route path="/teacher/share/:id" element={<Share />} />
|
<Route path="/teacher/share/:id" element={<Share />} />
|
||||||
<Route path="/teacher/editor-quiz/:id" element={<QuizForm />} />
|
<Route path="/teacher/editor-quiz/:id" element={<QuizForm />} />
|
||||||
|
|
|
||||||
|
|
@ -1,161 +1,172 @@
|
||||||
const ObjectId = require('mongodb').ObjectId;
|
const AppError = require("../middleware/AppError");
|
||||||
|
|
||||||
|
const ObjectId = require("mongodb").ObjectId;
|
||||||
|
|
||||||
class Rooms {
|
class Rooms {
|
||||||
constructor(db) {
|
constructor(db) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(title, userId) {
|
||||||
|
try {
|
||||||
|
if (!title || !userId) {
|
||||||
|
throw new AppError("Missing required parameter(s)", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
const normalizedTitle = title.toLowerCase();
|
||||||
|
|
||||||
|
const existingRoom = await roomsCollection.findOne({
|
||||||
|
title: normalizedTitle,
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingRoom) {
|
||||||
|
throw new AppError("Une salle avec ce nom existe déjà", 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newRoom = {
|
||||||
|
userId: userId,
|
||||||
|
title: title,
|
||||||
|
created_at: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await roomsCollection.insertOne(newRoom);
|
||||||
|
|
||||||
|
return result.insertedId;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error in create function:", error);
|
||||||
|
throw new AppError(error.message || "Internal Server Error", 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserRooms(userId) {
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
|
||||||
|
const result = await roomsCollection.find({ userId: userId }).toArray();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getOwner(roomId) {
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
|
||||||
|
const room = await roomsCollection.findOne({
|
||||||
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
|
});
|
||||||
|
|
||||||
|
return room.userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getContent(roomId) {
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
if (!ObjectId.isValid(roomId)) {
|
||||||
|
return null; // Évite d'envoyer une requête invalide
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(title, userId) {
|
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
|
||||||
try {
|
|
||||||
if (!title || !userId) {
|
return result;
|
||||||
throw new AppError('Missing required parameter(s)', 400);
|
}
|
||||||
}
|
|
||||||
|
async delete(roomId) {
|
||||||
await this.db.connect();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
const normalizedTitle = title.toLowerCase();
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
|
||||||
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId });
|
const roomResult = await roomsCollection.deleteOne({
|
||||||
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
if (existingRoom) {
|
});
|
||||||
throw new AppError('Une salle avec ce nom existe déjà', 409);
|
|
||||||
}
|
if (roomResult.deletedCount != 1) return false;
|
||||||
|
|
||||||
const newRoom = {
|
return true;
|
||||||
userId: userId,
|
}
|
||||||
title: title,
|
|
||||||
created_at: new Date()
|
async rename(roomId, userId, newTitle) {
|
||||||
};
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
const result = await roomsCollection.insertOne(newRoom);
|
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
return result.insertedId;
|
|
||||||
|
const existingRoom = await roomsCollection.findOne({
|
||||||
} catch (error) {
|
title: newTitle,
|
||||||
console.error("Error in create function:", error);
|
userId: userId,
|
||||||
throw new AppError(error.message || "Internal Server Error", 500);
|
});
|
||||||
}
|
|
||||||
|
if (existingRoom)
|
||||||
|
throw new Error(`Room with name '${newTitle}' already exists.`);
|
||||||
|
|
||||||
|
const result = await roomsCollection.updateOne(
|
||||||
|
{ _id: ObjectId.createFromHexString(roomId), userId: userId },
|
||||||
|
{ $set: { title: newTitle } }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.modifiedCount != 1) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async roomExists(title) {
|
||||||
|
// Ajouter userId en paramètre
|
||||||
|
try {
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
const existingRoom = await conn.collection("rooms").findOne({
|
||||||
|
title: title.toLowerCase(),
|
||||||
|
});
|
||||||
|
return !!existingRoom;
|
||||||
|
} catch (_error) {
|
||||||
|
throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getUserRooms(userId) {
|
async getRoomById(roomId) {
|
||||||
await this.db.connect()
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
|
||||||
const result = await roomsCollection.find({ userId: userId }).toArray();
|
const room = await roomsCollection.findOne({
|
||||||
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
if (!room) throw new AppError(`Room ${roomId} not found`, 404);
|
||||||
}
|
|
||||||
|
|
||||||
async getOwner(roomId) {
|
return room;
|
||||||
await this.db.connect()
|
}
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
async getRoomWithContent(roomId) {
|
||||||
|
const room = await this.getRoomById(roomId);
|
||||||
|
|
||||||
const room = await roomsCollection.findOne({ _id: ObjectId.createFromHexString(roomId) });
|
const content = await this.getContent(roomId);
|
||||||
|
|
||||||
return room.userId;
|
return {
|
||||||
}
|
...room,
|
||||||
|
content: content,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
async getRoomTitleByUserId(userId) {
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
async getContent(roomId) {
|
const roomsCollection = conn.collection("rooms");
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
if (!ObjectId.isValid(roomId)) {
|
|
||||||
return null; // Évite d'envoyer une requête invalide
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async delete(roomId) {
|
|
||||||
await this.db.connect()
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
|
|
||||||
const roomResult = await roomsCollection.deleteOne({ _id: ObjectId.createFromHexString(roomId) });
|
|
||||||
|
|
||||||
if (roomResult.deletedCount != 1) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
async rename(roomId, userId, newTitle) {
|
|
||||||
await this.db.connect()
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
|
|
||||||
const existingRoom = await roomsCollection.findOne({ title: newTitle, userId: userId });
|
|
||||||
|
|
||||||
if (existingRoom) throw new Error(`Room with name '${newTitle}' already exists.`);
|
|
||||||
|
|
||||||
const result = await roomsCollection.updateOne({ _id: ObjectId.createFromHexString(roomId), userId: userId }, { $set: { title: newTitle } })
|
|
||||||
|
|
||||||
if (result.modifiedCount != 1) return false;
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
async roomExists(title) { // Ajouter userId en paramètre
|
|
||||||
try {
|
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
const existingRoom = await conn.collection('rooms').findOne({
|
|
||||||
title: title.toLowerCase()
|
|
||||||
});
|
|
||||||
return !!existingRoom;
|
|
||||||
} catch (error) {
|
|
||||||
throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async getRoomById(roomId) {
|
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
|
|
||||||
const room = await roomsCollection.findOne({ _id: ObjectId.createFromHexString(roomId) });
|
|
||||||
|
|
||||||
if (!room) return new Error(`Room ${roomId} not found`);
|
|
||||||
|
|
||||||
return room;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async getRoomWithContent(roomId) {
|
|
||||||
const room = await this.getRoomById(roomId);
|
|
||||||
|
|
||||||
const content = await this.getContent(roomId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...room,
|
|
||||||
content: content
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
async getRoomTitleByUserId(userId) {
|
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection('rooms');
|
|
||||||
|
|
||||||
const rooms = await roomsCollection.find({ userId: userId }).toArray();
|
|
||||||
|
|
||||||
return rooms.map(room => room.title);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const rooms = await roomsCollection.find({ userId: userId }).toArray();
|
||||||
|
|
||||||
|
return rooms.map((room) => room.title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Rooms;
|
module.exports = Rooms;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue