mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Correction room controller
This commit is contained in:
parent
545d6551f6
commit
bd1ea4c2f6
2 changed files with 150 additions and 177 deletions
|
|
@ -25,10 +25,9 @@ class RoomsController {
|
|||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||
}
|
||||
|
||||
const normalizedTitle = title.toUpperCase();
|
||||
|
||||
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
||||
const normalizedTitle = title.toUpperCase().trim();
|
||||
|
||||
const roomExists = await this.rooms.roomExists(normalizedTitle, req.user.userId);
|
||||
if (roomExists) {
|
||||
throw new AppError(ROOM_ALREADY_EXISTS);
|
||||
}
|
||||
|
|
@ -40,14 +39,11 @@ class RoomsController {
|
|||
roomId: result.insertedId,
|
||||
});
|
||||
} catch (error) {
|
||||
next(
|
||||
error instanceof AppError
|
||||
? error
|
||||
: new AppError({ message: error.message, code: 500 })
|
||||
);
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
getUserRooms = async (req, res, next) => {
|
||||
try {
|
||||
const rooms = await this.rooms.getUserRooms(req.user.userId);
|
||||
|
|
@ -194,24 +190,7 @@ class RoomsController {
|
|||
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) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
|
|
|
|||
|
|
@ -9,25 +9,22 @@ class Rooms
|
|||
|
||||
async create(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();
|
||||
const conn = this.db.getConnection();
|
||||
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 roomsCollection = conn.collection("rooms");
|
||||
|
||||
const newRoom = {
|
||||
userId: userId,
|
||||
title: title,
|
||||
created_at: new Date()
|
||||
created_at: new Date(),
|
||||
};
|
||||
|
||||
const result = await roomsCollection.insertOne(newRoom);
|
||||
|
|
@ -35,7 +32,6 @@ class Rooms
|
|||
return result.insertedId;
|
||||
}
|
||||
|
||||
|
||||
async getUserRooms(userId)
|
||||
{
|
||||
await this.db.connect();
|
||||
|
|
@ -124,18 +120,16 @@ class Rooms
|
|||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const existingRoom = await conn.collection('rooms').findOne({
|
||||
const existingRoom = await conn.collection("rooms").findOne({
|
||||
title: title.toUpperCase(),
|
||||
userId: userId
|
||||
userId: userId,
|
||||
});
|
||||
return existingRoom ? true : false;
|
||||
return !!existingRoom;
|
||||
} catch (error)
|
||||
{
|
||||
throw new Error(`Database error (${error})`, 500);
|
||||
throw new Error(`Database error (${error})`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getRoomById(roomId)
|
||||
{
|
||||
await this.db.connect();
|
||||
|
|
|
|||
Loading…
Reference in a new issue