2024-11-10 15:42:46 -05:00
|
|
|
class Room {
|
2024-11-12 01:22:54 -05:00
|
|
|
constructor(id, name, host, nbStudents = 0,) { // Default nbStudents to 0
|
2024-11-10 15:42:46 -05:00
|
|
|
this.id = id;
|
|
|
|
|
this.name = name;
|
2024-11-12 01:22:54 -05:00
|
|
|
|
|
|
|
|
if (!host.startsWith('http://') && !host.startsWith('https://')) {
|
|
|
|
|
host = 'http://' + host;
|
|
|
|
|
}
|
2024-11-10 15:42:46 -05:00
|
|
|
this.host = host;
|
2024-11-12 01:22:54 -05:00
|
|
|
|
2024-11-10 15:42:46 -05:00
|
|
|
this.nbStudents = nbStudents;
|
2024-11-12 01:22:54 -05:00
|
|
|
this.mustBeCleaned = false;
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
class RoomRepository {
|
2024-11-10 15:42:46 -05:00
|
|
|
constructor(db) {
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.connection = null;
|
|
|
|
|
this.collection = null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
async init() {
|
|
|
|
|
if (!this.connection) {
|
|
|
|
|
await this.db.connect();
|
2024-11-10 15:42:46 -05:00
|
|
|
this.connection = this.db.getConnection();
|
|
|
|
|
}
|
2024-11-11 11:32:46 -05:00
|
|
|
if (!this.collection) this.collection = this.connection.collection('rooms');
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
async create(room) {
|
2024-11-10 15:42:46 -05:00
|
|
|
await this.init();
|
|
|
|
|
const existingRoom = await this.collection.findOne({ id: room.id });
|
|
|
|
|
if (existingRoom) {
|
|
|
|
|
throw new Error(`Room already exists with id: ${room.id}`);
|
|
|
|
|
}
|
2024-11-10 16:33:45 -05:00
|
|
|
const returnedId = await this.collection.insertOne(room);
|
|
|
|
|
return await this.collection.findOne({ _id: returnedId.insertedId });
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async get(id) {
|
|
|
|
|
await this.init();
|
|
|
|
|
const existingRoom = await this.collection.findOne({ id: id });
|
2024-11-11 11:32:46 -05:00
|
|
|
if (!existingRoom) {
|
|
|
|
|
console.warn(`Room with id ${id} not found.`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return existingRoom;
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAll() {
|
|
|
|
|
await this.init();
|
2024-11-11 11:32:46 -05:00
|
|
|
return await this.collection.find().toArray();
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
2024-11-11 11:32:46 -05:00
|
|
|
|
2024-11-12 01:22:54 -05:00
|
|
|
async update(room,roomId = null) {
|
2024-11-10 15:42:46 -05:00
|
|
|
await this.init();
|
2024-11-12 01:22:54 -05:00
|
|
|
|
|
|
|
|
const searchId = roomId ?? room.id;
|
|
|
|
|
|
2024-11-10 15:42:46 -05:00
|
|
|
const result = await this.collection.updateOne(
|
2024-11-12 01:22:54 -05:00
|
|
|
{ id: searchId },
|
|
|
|
|
{ $set: room },
|
|
|
|
|
{ upsert: false }
|
2024-11-10 15:42:46 -05:00
|
|
|
);
|
2024-11-12 01:22:54 -05:00
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
if (result.modifiedCount === 0) {
|
2024-11-12 01:22:54 -05:00
|
|
|
if (result.matchedCount > 0) {
|
|
|
|
|
return true; // Document exists but no changes needed
|
|
|
|
|
}
|
2024-11-11 11:32:46 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
async delete(id) {
|
2024-11-10 15:42:46 -05:00
|
|
|
await this.init();
|
2024-11-11 11:32:46 -05:00
|
|
|
const result = await this.collection.deleteOne({ id: id });
|
|
|
|
|
if (result.deletedCount === 0) {
|
|
|
|
|
console.warn(`Room with id ${id} not found for deletion.`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2024-11-10 15:42:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 11:32:46 -05:00
|
|
|
module.exports = { Room, RoomRepository };
|