EvalueTonSavoir/server/models/room.js

173 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-02-27 00:43:20 -05:00
const AppError = require("../middleware/AppError");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const ObjectId = require("mongodb").ObjectId;
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
class Rooms {
constructor(db) {
this.db = db;
}
async create(title, userId) {
try {
if (!title || !userId) {
throw new AppError("Missing required parameter(s)", 400);
}
await this.db.connect();
const conn = this.db.getConnection();
const roomsCollection = conn.collection("rooms");
const normalizedTitle = title.toLowerCase();
const existingRoom = await roomsCollection.findOne({
title: normalizedTitle,
userId: userId,
});
if (existingRoom) {
throw new AppError("Une salle avec ce nom existe déjà", 409);
}
const newRoom = {
userId: userId,
title: title,
created_at: new Date(),
};
const result = await roomsCollection.insertOne(newRoom);
return result.insertedId;
} catch (error) {
console.error("Error in create function:", error);
throw new AppError(error.message || "Internal Server Error", 500);
2025-02-19 18:56:37 -05:00
}
2025-02-27 00:43:20 -05:00
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async getUserRooms(userId) {
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const result = await roomsCollection.find({ userId: userId }).toArray();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return result;
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async getOwner(roomId) {
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return room.userId;
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -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
}
2025-02-27 00:43:20 -05:00
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return result;
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async delete(roomId) {
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomResult = await roomsCollection.deleteOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
if (roomResult.deletedCount != 1) return false;
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return true;
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async rename(roomId, userId, newTitle) {
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const existingRoom = await roomsCollection.findOne({
title: newTitle,
userId: userId,
});
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
if (existingRoom)
throw new Error(`Room with name '${newTitle}' already exists.`);
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -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-02-27 00:43:20 -05:00
if (result.modifiedCount != 1) return false;
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return true;
}
2025-02-27 00:43:20 -05:00
async roomExists(title) {
// Ajouter userId en paramètre
try {
await this.db.connect();
const conn = this.db.getConnection();
const existingRoom = await conn.collection("rooms").findOne({
title: title.toLowerCase(),
});
return !!existingRoom;
} catch (_error) {
throw new AppError("Erreur base de données", 500); // Encapsuler les erreurs
2025-02-19 18:56:37 -05:00
}
2025-02-27 00:43:20 -05:00
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async getRoomById(roomId) {
await this.db.connect();
const conn = this.db.getConnection();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const roomsCollection = conn.collection("rooms");
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const room = await roomsCollection.findOne({
_id: ObjectId.createFromHexString(roomId),
});
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
if (!room) throw new AppError(`Room ${roomId} not found`, 404);
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
return room;
}
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
async getRoomWithContent(roomId) {
const room = await this.getRoomById(roomId);
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -05:00
const content = await this.getContent(roomId);
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -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
2025-02-27 00:43:20 -05:00
const rooms = await roomsCollection.find({ userId: userId }).toArray();
2025-02-19 18:56:37 -05:00
2025-02-27 00:43:20 -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;