mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Correction room controller
This commit is contained in:
parent
545d6551f6
commit
bd1ea4c2f6
2 changed files with 150 additions and 177 deletions
|
|
@ -25,10 +25,9 @@ class RoomsController {
|
||||||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizedTitle = title.toUpperCase();
|
const normalizedTitle = title.toUpperCase().trim();
|
||||||
|
|
||||||
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
|
||||||
|
|
||||||
|
const roomExists = await this.rooms.roomExists(normalizedTitle, req.user.userId);
|
||||||
if (roomExists) {
|
if (roomExists) {
|
||||||
throw new AppError(ROOM_ALREADY_EXISTS);
|
throw new AppError(ROOM_ALREADY_EXISTS);
|
||||||
}
|
}
|
||||||
|
|
@ -40,14 +39,11 @@ class RoomsController {
|
||||||
roomId: result.insertedId,
|
roomId: result.insertedId,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(
|
next(error);
|
||||||
error instanceof AppError
|
|
||||||
? error
|
|
||||||
: new AppError({ message: error.message, code: 500 })
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
getUserRooms = async (req, res, next) => {
|
getUserRooms = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const rooms = await this.rooms.getUserRooms(req.user.userId);
|
const rooms = await this.rooms.getUserRooms(req.user.userId);
|
||||||
|
|
@ -194,24 +190,7 @@ class RoomsController {
|
||||||
return next(error);
|
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) => {
|
getRoomTitleByUserId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { userId } = req.params;
|
const { userId } = req.params;
|
||||||
|
|
|
||||||
|
|
@ -2,178 +2,172 @@ const ObjectId = require("mongodb").ObjectId;
|
||||||
|
|
||||||
class Rooms
|
class Rooms
|
||||||
{
|
{
|
||||||
constructor(db)
|
constructor(db)
|
||||||
{
|
{
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(title, userId) {
|
||||||
|
if (!title || !userId) {
|
||||||
|
throw new Error("Missing required parameter(s)");
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(title, userId) {
|
const exists = await this.roomExists(title, userId);
|
||||||
if (!title || !userId) {
|
if (exists) {
|
||||||
throw new Error('Missing required parameter(s)');
|
throw new Error("Room already exists");
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
const roomsCollection = conn.collection("rooms");
|
||||||
|
|
||||||
async getUserRooms(userId)
|
const newRoom = {
|
||||||
{
|
userId: userId,
|
||||||
await this.db.connect();
|
title: title,
|
||||||
const conn = this.db.getConnection();
|
created_at: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
const roomsCollection = conn.collection("rooms");
|
const result = await roomsCollection.insertOne(newRoom);
|
||||||
|
|
||||||
const result = await roomsCollection.find({ userId: userId }).toArray();
|
return result.insertedId;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
async getUserRooms(userId)
|
||||||
}
|
{
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
async getOwner(roomId)
|
const roomsCollection = conn.collection("rooms");
|
||||||
{
|
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection("rooms");
|
const result = await roomsCollection.find({ userId: userId }).toArray();
|
||||||
|
|
||||||
const room = await roomsCollection.findOne({
|
return result;
|
||||||
_id: ObjectId.createFromHexString(roomId),
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return room.userId;
|
async getOwner(roomId)
|
||||||
}
|
{
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
async getContent(roomId)
|
const roomsCollection = conn.collection("rooms");
|
||||||
{
|
|
||||||
await this.db.connect();
|
const room = await roomsCollection.findOne({
|
||||||
const conn = this.db.getConnection();
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
const roomsCollection = conn.collection("rooms");
|
});
|
||||||
if (!ObjectId.isValid(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
|
return null; // Évite d'envoyer une requête invalide
|
||||||
}
|
|
||||||
|
|
||||||
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
const existingRoom = await conn.collection("rooms").findOne({
|
||||||
const roomsCollection = conn.collection("rooms");
|
title: title.toUpperCase(),
|
||||||
|
userId: userId,
|
||||||
const roomResult = await roomsCollection.deleteOne({
|
});
|
||||||
_id: ObjectId.createFromHexString(roomId),
|
return !!existingRoom;
|
||||||
});
|
} catch (error)
|
||||||
|
|
||||||
if (roomResult.deletedCount != 1) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
async rename(roomId, userId, newTitle)
|
|
||||||
{
|
{
|
||||||
await this.db.connect();
|
throw new Error(`Database error (${error})`);
|
||||||
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 getRoomById(roomId)
|
||||||
|
{
|
||||||
|
await this.db.connect();
|
||||||
|
const conn = this.db.getConnection();
|
||||||
|
|
||||||
async roomExists(title, userId)
|
const roomsCollection = conn.collection("rooms");
|
||||||
{
|
|
||||||
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 room = await roomsCollection.findOne({
|
||||||
|
_id: ObjectId.createFromHexString(roomId),
|
||||||
|
});
|
||||||
|
|
||||||
async getRoomById(roomId)
|
if (!room) throw new Error(`Room ${roomId} not found`, 404);
|
||||||
{
|
|
||||||
await this.db.connect();
|
|
||||||
const conn = this.db.getConnection();
|
|
||||||
|
|
||||||
const roomsCollection = conn.collection("rooms");
|
return room;
|
||||||
|
}
|
||||||
|
|
||||||
const room = await roomsCollection.findOne({
|
async getRoomWithContent(roomId)
|
||||||
_id: ObjectId.createFromHexString(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 roomsCollection = conn.collection("rooms");
|
||||||
{
|
|
||||||
const room = await this.getRoomById(roomId);
|
|
||||||
|
|
||||||
const content = await this.getContent(roomId);
|
const rooms = await roomsCollection.find({ userId: userId }).toArray();
|
||||||
|
|
||||||
return {
|
return rooms.map((room) => room.title);
|
||||||
...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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Rooms;
|
module.exports = Rooms;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue