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

@ -18,17 +18,16 @@ class RoomsController {
try { try {
if (!req.user || !req.user.userId) { if (!req.user || !req.user.userId) {
throw new AppError(MISSING_REQUIRED_PARAMETER); throw new AppError(MISSING_REQUIRED_PARAMETER);
} }
const { title } = req.body; const { title } = req.body;
if (!title) { if (!title) {
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,13 +39,10 @@ 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 {
@ -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

@ -1,179 +1,173 @@
const ObjectId = require("mongodb").ObjectId; const ObjectId = require("mongodb").ObjectId;
class Rooms class Rooms
{ {
constructor(db) constructor(db)
{ {
this.db = db; this.db = db;
}
async create(title, userId) {
if (!title || !userId) {
throw new Error("Missing required parameter(s)");
} }
async create(title, userId) { const exists = await this.roomExists(title, userId);
if (!title || !userId) { if (exists) {
throw new Error('Missing required parameter(s)'); 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 newRoom = {
userId: userId,
title: title,
created_at: new Date()
};
const result = await roomsCollection.insertOne(newRoom);
return result.insertedId;
}
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();
await this.db.connect(); const roomsCollection = conn.collection("rooms");
const conn = this.db.getConnection();
const roomsCollection = conn.collection("rooms"); const newRoom = {
userId: userId,
title: title,
created_at: new Date(),
};
const room = await roomsCollection.findOne({ const result = await roomsCollection.insertOne(newRoom);
_id: ObjectId.createFromHexString(roomId),
});
return room.userId; return result.insertedId;
} }
async getContent(roomId) 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");
if (!ObjectId.isValid(roomId)) 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 return null; // Évite d'envoyer une requête invalide
}
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
return result;
} }
async delete(roomId) 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, userId)
{
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 roomsCollection = conn.collection("rooms"); title: title.toUpperCase(),
userId: userId,
const roomResult = await roomsCollection.deleteOne({ });
_id: ObjectId.createFromHexString(roomId), return !!existingRoom;
}); } catch (error)
if (roomResult.deletedCount != 1) return false;
return true;
}
async rename(roomId, userId, newTitle)
{ {
await this.db.connect(); throw new Error(`Database error (${error})`);
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 getRoomById(roomId)
{
await this.db.connect();
const conn = this.db.getConnection();
async roomExists(title, userId) const roomsCollection = conn.collection("rooms");
{
try
{
await this.db.connect();
const conn = this.db.getConnection();
const existingRoom = await conn.collection('rooms').findOne({
title: title.toUpperCase(),
userId: userId
});
return existingRoom ? true : false;
} catch (error)
{
throw new Error(`Database error (${error})`, 500);
}
}
const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
async getRoomById(roomId) if (!room) throw new Error(`Room ${roomId} not found`, 404);
{
await this.db.connect();
const conn = this.db.getConnection();
const roomsCollection = conn.collection("rooms"); return room;
}
const room = await roomsCollection.findOne({ async getRoomWithContent(roomId)
_id: ObjectId.createFromHexString(roomId), {
}); const room = await this.getRoomById(roomId);
if (!room) throw new Error(`Room ${roomId} not found`, 404); const content = await this.getContent(roomId);
return room; return {
} ...room,
content: content,
};
}
async getRoomTitleByUserId(userId)
{
await this.db.connect();
const conn = this.db.getConnection();
async getRoomWithContent(roomId) const roomsCollection = conn.collection("rooms");
{
const room = await this.getRoomById(roomId);
const content = await this.getContent(roomId); const rooms = await roomsCollection.find({ userId: userId }).toArray();
return { return rooms.map((room) => room.title);
...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);
}
} }
module.exports = Rooms; module.exports = Rooms;