2025-02-27 00:43:20 -05:00
|
|
|
const ObjectId = require("mongodb").ObjectId;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
class Rooms
|
2025-02-27 09:06:58 -05:00
|
|
|
{
|
2025-03-02 02:56:49 -05:00
|
|
|
constructor(db)
|
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(title, userId) {
|
|
|
|
|
if (!title || !userId) {
|
|
|
|
|
throw new Error("Missing required parameter(s)");
|
2025-02-19 18:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const exists = await this.roomExists(title, userId);
|
|
|
|
|
if (exists) {
|
|
|
|
|
throw new Error("Room already exists");
|
2025-02-27 09:06:58 -05:00
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const newRoom = {
|
|
|
|
|
userId: userId,
|
|
|
|
|
title: title,
|
|
|
|
|
created_at: new Date(),
|
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const result = await roomsCollection.insertOne(newRoom);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return result.insertedId;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async getUserRooms(userId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const result = await roomsCollection.find({ userId: userId }).toArray();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return result;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async getOwner(roomId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const room = await roomsCollection.findOne({
|
|
|
|
|
_id: ObjectId.createFromHexString(roomId),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return room.userId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getContent(roomId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
|
|
|
|
if (!ObjectId.isValid(roomId))
|
|
|
|
|
{
|
|
|
|
|
return null; // Évite d'envoyer une requête invalide
|
2025-02-27 09:06:58 -05:00
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return result;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async delete(roomId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomResult = await roomsCollection.deleteOne({
|
|
|
|
|
_id: ObjectId.createFromHexString(roomId),
|
|
|
|
|
});
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
if (roomResult.deletedCount != 1) return false;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async rename(roomId, userId, newTitle)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-26 14:07:18 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const existingRoom = await roomsCollection.findOne({
|
|
|
|
|
title: newTitle,
|
|
|
|
|
userId: userId,
|
|
|
|
|
});
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
if (existingRoom)
|
|
|
|
|
throw new Error(`Room with name '${newTitle}' already exists.`);
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const result = await roomsCollection.updateOne(
|
|
|
|
|
{ _id: ObjectId.createFromHexString(roomId), userId: userId },
|
|
|
|
|
{ $set: { title: newTitle } }
|
|
|
|
|
);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
if (result.modifiedCount != 1) return false;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async roomExists(title, userId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-03-02 02:56:49 -05:00
|
|
|
const existingRoom = await conn.collection("rooms").findOne({
|
|
|
|
|
title: title.toUpperCase(),
|
|
|
|
|
userId: userId,
|
|
|
|
|
});
|
|
|
|
|
return !!existingRoom;
|
|
|
|
|
} catch (error)
|
2025-02-27 09:06:58 -05:00
|
|
|
{
|
2025-03-02 02:56:49 -05:00
|
|
|
throw new Error(`Database error (${error})`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async getRoomById(roomId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const room = await roomsCollection.findOne({
|
|
|
|
|
_id: ObjectId.createFromHexString(roomId),
|
|
|
|
|
});
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
if (!room) throw new Error(`Room ${roomId} not found`, 404);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return room;
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
async getRoomWithContent(roomId)
|
|
|
|
|
{
|
|
|
|
|
const room = await this.getRoomById(roomId);
|
2025-02-27 00:43:20 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const content = await this.getContent(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return {
|
|
|
|
|
...room,
|
|
|
|
|
content: content,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
async getRoomTitleByUserId(userId)
|
|
|
|
|
{
|
2025-03-14 00:42:49 -04:00
|
|
|
const conn = await this.db.getConnection();
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const roomsCollection = conn.collection("rooms");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
const rooms = await roomsCollection.find({ userId: userId }).toArray();
|
2025-02-27 09:06:58 -05:00
|
|
|
|
2025-03-02 02:56:49 -05:00
|
|
|
return rooms.map((room) => room.title);
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-27 00:43:20 -05:00
|
|
|
module.exports = Rooms;
|