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

View file

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