mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Fix existing room error handling with AppError
This commit is contained in:
parent
4cc6ee79e4
commit
2fc922d01f
2 changed files with 30 additions and 22 deletions
|
|
@ -29,13 +29,12 @@ class RoomsController {
|
||||||
|
|
||||||
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
||||||
|
|
||||||
|
|
||||||
if (roomExists) {
|
if (roomExists) {
|
||||||
const error = new AppError(ROOM_ALREADY_EXISTS);
|
const error = new Error("Room already exists");
|
||||||
error.message = "Cette salle existe déjà";
|
|
||||||
error.statusCode = 409;
|
error.statusCode = 409;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.rooms.create(normalizedTitle, req.user.userId);
|
const result = await this.rooms.create(normalizedTitle, req.user.userId);
|
||||||
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
|
|
@ -43,10 +42,17 @@ class RoomsController {
|
||||||
roomId: result.insertedId,
|
roomId: result.insertedId,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(Object.assign(error, { message: error.message }));
|
if (error instanceof Error) {
|
||||||
|
if (error.message === "Room already exists") {
|
||||||
|
return next(new AppError(ROOM_ALREADY_EXISTS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -200,7 +206,9 @@ class RoomsController {
|
||||||
if (!title) {
|
if (!title) {
|
||||||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||||
}
|
}
|
||||||
const exists = await this.rooms.roomExists(title);
|
const userId = req.user.userId;
|
||||||
|
|
||||||
|
const exists = await this.rooms.roomExists(title, userId);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
exists: exists,
|
exists: exists,
|
||||||
|
|
|
||||||
|
|
@ -7,21 +7,21 @@ class Rooms
|
||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(title, userId)
|
async create(title, userId) {
|
||||||
{
|
|
||||||
if (!title || !userId) {
|
if (!title || !userId) {
|
||||||
throw new Error('Missing required parameter(s)', 400);
|
throw new Error('Missing required parameter(s)');
|
||||||
}
|
}
|
||||||
|
|
||||||
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.toUpperCase();
|
||||||
|
|
||||||
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId });
|
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle });
|
||||||
|
|
||||||
if (existingRoom) {
|
if (existingRoom) {
|
||||||
throw new Error('Room already exists', 409);
|
// Lancer une erreur générique ici
|
||||||
|
throw new Error('Room already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
const newRoom = {
|
const newRoom = {
|
||||||
|
|
@ -33,7 +33,6 @@ class Rooms
|
||||||
const result = await roomsCollection.insertOne(newRoom);
|
const result = await roomsCollection.insertOne(newRoom);
|
||||||
|
|
||||||
return result.insertedId;
|
return result.insertedId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -119,14 +118,15 @@ class Rooms
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async roomExists(title)
|
async roomExists(title, userId)
|
||||||
{ // 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.toUpperCase(),
|
||||||
|
userId: userId
|
||||||
});
|
});
|
||||||
return existingRoom ? true : false;
|
return existingRoom ? true : false;
|
||||||
} catch (error)
|
} catch (error)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue