import appError dans le model

This commit is contained in:
NouhailaAater 2025-02-27 00:43:20 -05:00
parent 068f97ac47
commit d2bf18b88d
2 changed files with 158 additions and 144 deletions

View file

@ -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 />} />

View file

@ -1,4 +1,6 @@
const ObjectId = require('mongodb').ObjectId; const AppError = require("../middleware/AppError");
const ObjectId = require("mongodb").ObjectId;
class Rooms { class Rooms {
constructor(db) { constructor(db) {
@ -8,42 +10,43 @@ class Rooms {
async create(title, userId) { async create(title, userId) {
try { try {
if (!title || !userId) { if (!title || !userId) {
throw new AppError('Missing required parameter(s)', 400); throw new AppError("Missing required parameter(s)", 400);
} }
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 normalizedTitle = title.toLowerCase(); const normalizedTitle = title.toLowerCase();
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId }); const existingRoom = await roomsCollection.findOne({
title: normalizedTitle,
userId: userId,
});
if (existingRoom) { if (existingRoom) {
throw new AppError('Une salle avec ce nom existe déjà', 409); throw new AppError("Une salle avec ce nom existe déjà", 409);
} }
const newRoom = { const newRoom = {
userId: userId, userId: userId,
title: title, title: title,
created_at: new Date() created_at: new Date(),
}; };
const result = await roomsCollection.insertOne(newRoom); const result = await roomsCollection.insertOne(newRoom);
return result.insertedId; return result.insertedId;
} catch (error) { } catch (error) {
console.error("Error in create function:", error); console.error("Error in create function:", error);
throw new AppError(error.message || "Internal Server Error", 500); throw new AppError(error.message || "Internal Server Error", 500);
} }
} }
async getUserRooms(userId) { async getUserRooms(userId) {
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 result = await roomsCollection.find({ userId: userId }).toArray();
@ -51,12 +54,14 @@ class Rooms {
} }
async getOwner(roomId) { async getOwner(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 room = await roomsCollection.findOne({ _id: ObjectId.createFromHexString(roomId) }); const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
return room.userId; return room.userId;
} }
@ -64,7 +69,7 @@ class Rooms {
async getContent(roomId) { async getContent(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");
if (!ObjectId.isValid(roomId)) { if (!ObjectId.isValid(roomId)) {
return null; // Évite d'envoyer une requête invalide return null; // Évite d'envoyer une requête invalide
} }
@ -74,14 +79,15 @@ class Rooms {
return result; return result;
} }
async delete(roomId) { 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 roomsCollection = conn.collection("rooms");
const roomResult = await roomsCollection.deleteOne({ _id: ObjectId.createFromHexString(roomId) }); const roomResult = await roomsCollection.deleteOne({
_id: ObjectId.createFromHexString(roomId),
});
if (roomResult.deletedCount != 1) return false; if (roomResult.deletedCount != 1) return false;
@ -89,50 +95,58 @@ class Rooms {
} }
async rename(roomId, userId, newTitle) { async rename(roomId, userId, newTitle) {
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 existingRoom = await roomsCollection.findOne({ title: newTitle, userId: userId }); const existingRoom = await roomsCollection.findOne({
title: newTitle,
userId: userId,
});
if (existingRoom) throw new Error(`Room with name '${newTitle}' already exists.`); 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 } }) const result = await roomsCollection.updateOne(
{ _id: ObjectId.createFromHexString(roomId), userId: userId },
{ $set: { title: newTitle } }
);
if (result.modifiedCount != 1) return false; if (result.modifiedCount != 1) return false;
return true return true;
} }
async roomExists(title) { // Ajouter userId en paramètre async roomExists(title) {
// Ajouter userId en paramètre
try { try {
await this.db.connect(); await this.db.connect();
const conn = this.db.getConnection(); const conn = this.db.getConnection();
const existingRoom = await conn.collection('rooms').findOne({ const existingRoom = await conn.collection("rooms").findOne({
title: title.toLowerCase() title: title.toLowerCase(),
}); });
return !!existingRoom; return !!existingRoom;
} catch (error) { } catch (_error) {
throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs
} }
} }
async getRoomById(roomId) { 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 room = await roomsCollection.findOne({ _id: ObjectId.createFromHexString(roomId) }); const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
if (!room) return new Error(`Room ${roomId} not found`); if (!room) throw new AppError(`Room ${roomId} not found`, 404);
return room; return room;
} }
async getRoomWithContent(roomId) { async getRoomWithContent(roomId) {
const room = await this.getRoomById(roomId); const room = await this.getRoomById(roomId);
@ -140,22 +154,19 @@ class Rooms {
return { return {
...room, ...room,
content: content content: content,
}; };
} }
async getRoomTitleByUserId(userId) { async getRoomTitleByUserId(userId) {
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 rooms = await roomsCollection.find({ userId: userId }).toArray(); const rooms = await roomsCollection.find({ userId: userId }).toArray();
return rooms.map(room => room.title); return rooms.map((room) => room.title);
} }
} }
module.exports = Rooms; module.exports = Rooms;