EvalueTonSavoir/server/models/room.js

180 lines
4.4 KiB
JavaScript
Raw Normal View History

2025-02-27 00:43:20 -05:00
const ObjectId = require("mongodb").ObjectId;
2025-02-19 18:56:37 -05:00
class Rooms
{
constructor(db)
{
this.db = db;
2025-02-19 18:56:37 -05:00
}
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;
}
2025-02-19 18:56:37 -05:00
async getUserRooms(userId)
{
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
const result = await roomsCollection.find({ userId: userId }).toArray();
2025-02-19 18:56:37 -05:00
return result;
2025-02-19 18:56:37 -05:00
}
async getOwner(roomId)
{
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
return room.userId;
}
2025-02-19 18:56:37 -05:00
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
}
2025-02-19 18:56:37 -05:00
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
2025-02-19 18:56:37 -05:00
return result;
}
2025-02-19 18:56:37 -05:00
async delete(roomId)
{
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
const roomResult = await roomsCollection.deleteOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
if (roomResult.deletedCount != 1) return false;
return true;
}
2025-02-19 18:56:37 -05:00
async rename(roomId, userId, newTitle)
{
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
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;
2025-02-19 18:56:37 -05:00
}
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);
}
}
2025-02-19 18:56:37 -05:00
async getRoomById(roomId)
{
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
if (!room) throw new Error(`Room ${roomId} not found`, 404);
2025-02-19 18:56:37 -05:00
return room;
}
2025-02-19 18:56:37 -05:00
async getRoomWithContent(roomId)
{
const room = await this.getRoomById(roomId);
2025-02-27 00:43:20 -05:00
const content = await this.getContent(roomId);
2025-02-19 18:56:37 -05:00
return {
...room,
content: content,
};
}
async getRoomTitleByUserId(userId)
{
await this.db.connect();
const conn = this.db.getConnection();
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
const rooms = await roomsCollection.find({ userId: userId }).toArray();
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;