2025-02-24 04:09:55 -05:00
|
|
|
|
const AppError = require("../middleware/AppError.js");
|
|
|
|
|
|
const {
|
|
|
|
|
|
MISSING_REQUIRED_PARAMETER,
|
|
|
|
|
|
ROOM_NOT_FOUND,
|
|
|
|
|
|
ROOM_ALREADY_EXISTS,
|
|
|
|
|
|
GETTING_ROOM_ERROR,
|
|
|
|
|
|
DELETE_ROOM_ERROR,
|
|
|
|
|
|
UPDATE_ROOM_ERROR,
|
|
|
|
|
|
} = require("../constants/errorCodes");
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
|
|
|
|
|
class RoomsController {
|
2025-02-24 04:09:55 -05:00
|
|
|
|
constructor(roomsModel) {
|
|
|
|
|
|
this.rooms = roomsModel;
|
|
|
|
|
|
this.getRoomTitle = this.getRoomTitle.bind(this);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
create = async (req, res, next) => {
|
2025-02-19 18:56:37 -05:00
|
|
|
|
try {
|
2025-02-26 14:07:18 -05:00
|
|
|
|
if (!req.user || !req.user.userId) {
|
|
|
|
|
|
throw new AppError("Utilisateur non authentifié", 401);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-26 14:07:18 -05:00
|
|
|
|
const { title } = req.body;
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!title) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-27 16:07:00 -05:00
|
|
|
|
const normalizedTitle = title.toUpperCase();
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (roomExists) {
|
2025-02-28 02:28:47 -05:00
|
|
|
|
const error = new Error("Room already exists");
|
2025-02-26 14:07:18 -05:00
|
|
|
|
error.statusCode = 409;
|
|
|
|
|
|
throw error;
|
2025-02-24 04:09:55 -05:00
|
|
|
|
}
|
2025-02-28 02:28:47 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const result = await this.rooms.create(normalizedTitle, req.user.userId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-26 14:07:18 -05:00
|
|
|
|
return res.status(201).json({
|
|
|
|
|
|
message: "Room créée avec succès.",
|
2025-02-24 04:09:55 -05:00
|
|
|
|
roomId: result.insertedId,
|
|
|
|
|
|
});
|
2025-02-19 18:56:37 -05:00
|
|
|
|
} catch (error) {
|
2025-02-28 02:28:47 -05:00
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
|
if (error.message === "Room already exists") {
|
|
|
|
|
|
return next(new AppError(ROOM_ALREADY_EXISTS));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-28 02:28:47 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
getUserRooms = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const rooms = await this.rooms.getUserRooms(req.user.userId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!rooms) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
data: rooms,
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
getRoomContent = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { roomId } = req.params;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!roomId) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
|
|
|
|
|
const content = await this.rooms.getContent(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!content) {
|
|
|
|
|
|
throw new AppError(GETTING_ROOM_ERROR);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
data: content,
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
delete = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { roomId } = req.params;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!roomId) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const owner = await this.rooms.getOwner(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const result = await this.rooms.delete(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!result) {
|
|
|
|
|
|
throw new AppError(DELETE_ROOM_ERROR);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
message: "Salle supprimé avec succès.",
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
rename = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { roomId, newTitle } = req.body;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!roomId || !newTitle) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const owner = await this.rooms.getOwner(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const exists = await this.rooms.roomExists(newTitle, req.user.userId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (exists) {
|
|
|
|
|
|
throw new AppError(ROOM_ALREADY_EXISTS);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const result = await this.rooms.rename(roomId, req.user.userId, newTitle);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!result) {
|
|
|
|
|
|
throw new AppError(UPDATE_ROOM_ERROR);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
message: "Salle mis <20> jours avec succ<63>s.",
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
getRoomById = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { roomId } = req.params;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!roomId) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
// Is this room mine
|
|
|
|
|
|
const owner = await this.rooms.getOwner(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const room = await this.rooms.getRoomById(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!room) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
data: room,
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
|
|
|
|
|
getRoomTitle = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { roomId } = req.params;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!roomId) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const room = await this.rooms.getRoomById(roomId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (room instanceof Error) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({ title: room.title });
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
|
|
|
|
|
roomExists = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { title } = req.body;
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!title) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
2025-02-28 02:28:47 -05:00
|
|
|
|
const userId = req.user.userId;
|
|
|
|
|
|
|
|
|
|
|
|
const exists = await this.rooms.roomExists(title, userId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
exists: exists,
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
2025-02-24 04:09:55 -05:00
|
|
|
|
};
|
|
|
|
|
|
getRoomTitleByUserId = async (req, res, next) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { userId } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const rooms = await this.rooms.getUserRooms(userId);
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
if (!rooms || rooms.length === 0) {
|
|
|
|
|
|
throw new AppError(ROOM_NOT_FOUND);
|
|
|
|
|
|
}
|
2025-02-19 18:56:37 -05:00
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
const roomTitles = rooms.map((room) => room.title);
|
|
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
|
titles: roomTitles,
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return next(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-02-19 18:56:37 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 04:09:55 -05:00
|
|
|
|
module.exports = RoomsController;
|