Correction room controller

This commit is contained in:
NouhailaAater 2025-03-02 02:56:49 -05:00
parent 545d6551f6
commit bd1ea4c2f6
2 changed files with 150 additions and 177 deletions

View file

@ -25,10 +25,9 @@ class RoomsController {
throw new AppError(MISSING_REQUIRED_PARAMETER); throw new AppError(MISSING_REQUIRED_PARAMETER);
} }
const normalizedTitle = title.toUpperCase(); const normalizedTitle = title.toUpperCase().trim();
const roomExists = await this.rooms.roomExists(normalizedTitle);
const roomExists = await this.rooms.roomExists(normalizedTitle, req.user.userId);
if (roomExists) { if (roomExists) {
throw new AppError(ROOM_ALREADY_EXISTS); throw new AppError(ROOM_ALREADY_EXISTS);
} }
@ -40,14 +39,11 @@ class RoomsController {
roomId: result.insertedId, roomId: result.insertedId,
}); });
} catch (error) { } catch (error) {
next( next(error);
error instanceof AppError
? error
: new AppError({ message: error.message, code: 500 })
);
} }
}; };
getUserRooms = async (req, res, next) => { getUserRooms = async (req, res, next) => {
try { try {
const rooms = await this.rooms.getUserRooms(req.user.userId); const rooms = await this.rooms.getUserRooms(req.user.userId);
@ -194,24 +190,7 @@ class RoomsController {
return next(error); return next(error);
} }
}; };
roomExists = async (req, res, next) => {
try {
const { title } = req.body;
if (!title) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const userId = req.user.userId;
const exists = await this.rooms.roomExists(title, userId);
return res.status(200).json({
exists: exists,
});
} catch (error) {
return next(error);
}
};
getRoomTitleByUserId = async (req, res, next) => { getRoomTitleByUserId = async (req, res, next) => {
try { try {
const { userId } = req.params; const { userId } = req.params;

View file

@ -9,25 +9,22 @@ class Rooms
async create(title, userId) { async create(title, userId) {
if (!title || !userId) { if (!title || !userId) {
throw new Error('Missing required parameter(s)'); throw new Error("Missing required parameter(s)");
}
const exists = await this.roomExists(title, userId);
if (exists) {
throw new Error("Room already exists");
} }
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.toUpperCase();
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle });
if (existingRoom) {
// Lancer une erreur générique ici
throw new Error('Room already exists');
}
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);
@ -35,7 +32,6 @@ class Rooms
return result.insertedId; return result.insertedId;
} }
async getUserRooms(userId) async getUserRooms(userId)
{ {
await this.db.connect(); await this.db.connect();
@ -124,18 +120,16 @@ class Rooms
{ {
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.toUpperCase(), title: title.toUpperCase(),
userId: userId userId: userId,
}); });
return existingRoom ? true : false; return !!existingRoom;
} catch (error) } catch (error)
{ {
throw new Error(`Database error (${error})`, 500); throw new Error(`Database error (${error})`);
} }
} }
async getRoomById(roomId) async getRoomById(roomId)
{ {
await this.db.connect(); await this.db.connect();