EvalueTonSavoir/server/controllers/room.js
C. Fuhrman 4cc6ee79e4 Permet d'ajouter une première salle
Nom de salle toujours en majuscules (bd)
2025-02-27 16:07:00 -05:00

237 lines
5.3 KiB
JavaScript
Raw Blame History

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");
class RoomsController {
constructor(roomsModel) {
this.rooms = roomsModel;
this.getRoomTitle = this.getRoomTitle.bind(this);
}
create = async (req, res, next) => {
try {
if (!req.user || !req.user.userId) {
throw new AppError("Utilisateur non authentifié", 401);
}
const { title } = req.body;
if (!title) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const normalizedTitle = title.toUpperCase();
const roomExists = await this.rooms.roomExists(normalizedTitle);
if (roomExists) {
const error = new AppError(ROOM_ALREADY_EXISTS);
error.message = "Cette salle existe déjà";
error.statusCode = 409;
throw error;
}
const result = await this.rooms.create(normalizedTitle, req.user.userId);
return res.status(201).json({
message: "Room créée avec succès.",
roomId: result.insertedId,
});
} catch (error) {
next(Object.assign(error, { message: error.message }));
}
};
getUserRooms = async (req, res, next) => {
try {
const rooms = await this.rooms.getUserRooms(req.user.userId);
if (!rooms) {
throw new AppError(ROOM_NOT_FOUND);
}
return res.status(200).json({
data: rooms,
});
} catch (error) {
return next(error);
}
};
getRoomContent = async (req, res, next) => {
try {
const { roomId } = req.params;
if (!roomId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const content = await this.rooms.getContent(roomId);
if (!content) {
throw new AppError(GETTING_ROOM_ERROR);
}
return res.status(200).json({
data: content,
});
} catch (error) {
return next(error);
}
};
delete = async (req, res, next) => {
try {
const { roomId } = req.params;
if (!roomId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const owner = await this.rooms.getOwner(roomId);
if (owner != req.user.userId) {
throw new AppError(ROOM_NOT_FOUND);
}
const result = await this.rooms.delete(roomId);
if (!result) {
throw new AppError(DELETE_ROOM_ERROR);
}
return res.status(200).json({
message: "Salle supprimé avec succès.",
});
} catch (error) {
return next(error);
}
};
rename = async (req, res, next) => {
try {
const { roomId, newTitle } = req.body;
if (!roomId || !newTitle) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const owner = await this.rooms.getOwner(roomId);
if (owner != req.user.userId) {
throw new AppError(ROOM_NOT_FOUND);
}
const exists = await this.rooms.roomExists(newTitle, req.user.userId);
if (exists) {
throw new AppError(ROOM_ALREADY_EXISTS);
}
const result = await this.rooms.rename(roomId, req.user.userId, newTitle);
if (!result) {
throw new AppError(UPDATE_ROOM_ERROR);
}
return res.status(200).json({
message: "Salle mis <20> jours avec succ<63>s.",
});
} catch (error) {
return next(error);
}
};
getRoomById = async (req, res, next) => {
try {
const { roomId } = req.params;
if (!roomId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
// Is this room mine
const owner = await this.rooms.getOwner(roomId);
if (owner != req.user.userId) {
throw new AppError(ROOM_NOT_FOUND);
}
const room = await this.rooms.getRoomById(roomId);
if (!room) {
throw new AppError(ROOM_NOT_FOUND);
}
return res.status(200).json({
data: room,
});
} catch (error) {
return next(error);
}
};
getRoomTitle = async (req, res, next) => {
try {
const { roomId } = req.params;
if (!roomId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const room = await this.rooms.getRoomById(roomId);
if (room instanceof Error) {
throw new AppError(ROOM_NOT_FOUND);
}
return res.status(200).json({ title: room.title });
} catch (error) {
return next(error);
}
};
roomExists = async (req, res, next) => {
try {
const { title } = req.body;
if (!title) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const exists = await this.rooms.roomExists(title);
return res.status(200).json({
exists: exists,
});
} catch (error) {
return next(error);
}
};
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);
if (!rooms || rooms.length === 0) {
throw new AppError(ROOM_NOT_FOUND);
}
const roomTitles = rooms.map((room) => room.title);
return res.status(200).json({
titles: roomTitles,
});
} catch (error) {
return next(error);
}
};
}
module.exports = RoomsController;