mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Ensure room title uniqueness by normalizing case sensitivity
This commit is contained in:
parent
bf2e6502f3
commit
b5547cb100
3 changed files with 235 additions and 226 deletions
|
|
@ -26,7 +26,7 @@ export const RoomProvider = ({ children }: { children: React.ReactNode }) => {
|
|||
setSelectedRoom(defaultRoom);
|
||||
localStorage.setItem('selectedRoomId', defaultRoom._id);
|
||||
} else {
|
||||
const randomRoomName = `Room-${Math.floor(Math.random() * 10000)}`;
|
||||
const randomRoomName = `Room-${Math.floor(Math.random() * 1000000)}`;
|
||||
await createRoom(randomRoomName);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,14 +1,20 @@
|
|||
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');
|
||||
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) => {
|
||||
create = async (req, res, next) => {
|
||||
try {
|
||||
const { title } = req.body;
|
||||
|
||||
|
|
@ -16,21 +22,24 @@ create = async (req, res, next) => {
|
|||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||
}
|
||||
|
||||
const result = await this.rooms.create(title, req.user.userId);
|
||||
const normalizedTitle = title.toLowerCase();
|
||||
|
||||
if (!result) {
|
||||
const roomExists = await this.rooms.roomExists(normalizedTitle);
|
||||
|
||||
if (roomExists) {
|
||||
throw new AppError(ROOM_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
message: 'Room cree avec succes.',
|
||||
roomId: result.insertedId
|
||||
});
|
||||
const result = await this.rooms.create(normalizedTitle, req.user.userId);
|
||||
|
||||
return res.status(200).json({
|
||||
message: "Room cree avec succes.",
|
||||
roomId: result.insertedId,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getUserRooms = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -41,13 +50,12 @@ create = async (req, res, next) => {
|
|||
}
|
||||
|
||||
return res.status(200).json({
|
||||
data: rooms
|
||||
data: rooms,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getRoomContent = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -70,13 +78,12 @@ create = async (req, res, next) => {
|
|||
}
|
||||
|
||||
return res.status(200).json({
|
||||
data: content
|
||||
data: content,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
delete = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -99,13 +106,12 @@ create = async (req, res, next) => {
|
|||
}
|
||||
|
||||
return res.status(200).json({
|
||||
message: 'Salle supprimé avec succès.'
|
||||
message: "Salle supprimé avec succès.",
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
rename = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -134,13 +140,12 @@ create = async (req, res, next) => {
|
|||
}
|
||||
|
||||
return res.status(200).json({
|
||||
message: 'Salle mis <20> jours avec succ<63>s.'
|
||||
message: "Salle mis <20> jours avec succ<63>s.",
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getRoomById = async (req, res, next) => {
|
||||
try {
|
||||
|
|
@ -164,12 +169,12 @@ create = async (req, res, next) => {
|
|||
}
|
||||
|
||||
return res.status(200).json({
|
||||
data: room
|
||||
data: room,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
getRoomTitle = async (req, res, next) => {
|
||||
try {
|
||||
const { roomId } = req.params;
|
||||
|
|
@ -188,7 +193,7 @@ create = async (req, res, next) => {
|
|||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
roomExists = async (req, res, next) => {
|
||||
try {
|
||||
const { title } = req.body;
|
||||
|
|
@ -197,15 +202,18 @@ create = async (req, res, next) => {
|
|||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||
}
|
||||
|
||||
const exists = await this.rooms.roomExists(title);
|
||||
const normalizedTitle = title.toLowerCase();
|
||||
|
||||
const exists = await this.rooms.roomExists(normalizedTitle);
|
||||
|
||||
return res.status(200).json({
|
||||
exists: exists
|
||||
message: `La salle avec le titre "${title}" existe déjà.`,
|
||||
exists: exists,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
getRoomTitleByUserId = async (req, res, next) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
|
|
@ -220,17 +228,15 @@ create = async (req, res, next) => {
|
|||
throw new AppError(ROOM_NOT_FOUND);
|
||||
}
|
||||
|
||||
const roomTitles = rooms.map(room => room.title);
|
||||
const roomTitles = rooms.map((room) => room.title);
|
||||
|
||||
return res.status(200).json({
|
||||
titles: roomTitles
|
||||
titles: roomTitles,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = RoomsController;
|
||||
|
|
@ -16,7 +16,9 @@ class Rooms {
|
|||
|
||||
const roomsCollection = conn.collection('rooms');
|
||||
|
||||
const existingRoom = await roomsCollection.findOne({ title: title, userId: userId });
|
||||
const normalizedTitle = title.toLowerCase();
|
||||
|
||||
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle, userId: userId });
|
||||
|
||||
if (existingRoom) {
|
||||
throw new Error('Room already exists');
|
||||
|
|
@ -101,7 +103,8 @@ class Rooms {
|
|||
const conn = this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection('rooms');
|
||||
const existingRoom = await roomsCollection.findOne({ title: title });
|
||||
const normalizedTitle = title.toLowerCase();
|
||||
const existingRoom = await roomsCollection.findOne({ title: normalizedTitle });
|
||||
return existingRoom ? true : false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue