EvalueTonSavoir/server/controllers/quiz.js

320 lines
9.6 KiB
JavaScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
const emailer = require('../config/email.js');
const AppError = require('../middleware/AppError.js');
2024-04-05 20:10:59 -04:00
const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, QUIZ_NOT_FOUND, FOLDER_NOT_FOUND, QUIZ_ALREADY_EXISTS, GETTING_QUIZ_ERROR, DELETE_QUIZ_ERROR, UPDATE_QUIZ_ERROR, MOVING_QUIZ_ERROR, DUPLICATE_QUIZ_ERROR, COPY_QUIZ_ERROR } = require('../constants/errorCodes');
2024-03-29 20:08:34 -04:00
class QuizController {
constructor(quizModel, foldersModel) {
this.folders = foldersModel;
console.log("QuizController constructor: folders", this.folders);
this.quizzes = quizModel;
console.log("QuizController constructor: quizzes", this.quizzes);
}
create = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { title, content, folderId } = req.body;
2024-03-29 20:08:34 -04:00
if (!title || !content || !folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// Is this folder mine
const owner = await this.folders.getOwner(folderId);
2024-03-29 20:08:34 -04:00
if (owner != req.user.userId) {
throw new AppError(FOLDER_NOT_FOUND);
}
const result = await this.quizzes.create(title, content, folderId, req.user.userId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(QUIZ_ALREADY_EXISTS);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Quiz créé avec succès.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
get = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId } = req.params;
2024-03-29 20:08:34 -04:00
if (!quizId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const content = await this.quizzes.getContent(quizId);
2024-03-29 20:08:34 -04:00
if (!content) {
throw new AppError(GETTING_QUIZ_ERROR);
}
2024-03-29 20:08:34 -04:00
// Is this quiz mine
if (content.userId != req.user.userId) {
throw new AppError(QUIZ_NOT_FOUND);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
data: content
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
delete = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId } = req.params;
2024-03-29 20:08:34 -04:00
if (!quizId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// Is this quiz mine
const owner = await this.quizzes.getOwner(quizId);
2024-03-29 20:08:34 -04:00
if (owner != req.user.userId) {
throw new AppError(QUIZ_NOT_FOUND);
}
const result = await this.quizzes.delete(quizId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(DELETE_QUIZ_ERROR);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Quiz supprimé avec succès.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
update = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId, newTitle, newContent } = req.body;
2024-03-29 20:08:34 -04:00
if (!newTitle || !newContent || !quizId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// Is this quiz mine
const owner = await this.quizzes.getOwner(quizId);
2024-03-29 20:08:34 -04:00
if (owner != req.user.userId) {
throw new AppError(QUIZ_NOT_FOUND);
}
const result = await this.quizzes.update(quizId, newTitle, newContent);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(UPDATE_QUIZ_ERROR);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Quiz mis à jours avec succès.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
move = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId, newFolderId } = req.body;
2024-03-29 20:08:34 -04:00
if (!quizId || !newFolderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// Is this quiz mine
const quizOwner = await this.quizzes.getOwner(quizId);
2024-03-29 20:08:34 -04:00
if (quizOwner != req.user.userId) {
throw new AppError(QUIZ_NOT_FOUND);
}
2024-03-29 20:08:34 -04:00
// Is this folder mine
const folderOwner = await this.folders.getOwner(newFolderId);
2024-03-29 20:08:34 -04:00
if (folderOwner != req.user.userId) {
throw new AppError(FOLDER_NOT_FOUND);
}
const result = await this.quizzes.move(quizId, newFolderId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(MOVING_QUIZ_ERROR);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Utilisateur déplacé avec succès.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
2024-03-29 20:08:34 -04:00
copy = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
const { quizId, newTitle, folderId } = req.body;
2024-03-29 20:08:34 -04:00
if (!quizId || !newTitle || !folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
throw new AppError(NOT_IMPLEMENTED);
// const { quizId } = req.params;
// const { newUserId } = req.body;
2024-03-29 20:08:34 -04:00
// try {
// //Trouver le quiz a dupliquer
// const conn = db.getConnection();
2024-10-03 12:05:20 -04:00
// const quiztoduplicate = await conn.collection('quiz').findOne({ _id: ObjectId.createFromHexString(quizId) });
2024-03-29 20:08:34 -04:00
// if (!quiztoduplicate) {
// throw new Error("Quiz non trouvé");
// }
// console.log(quiztoduplicate);
// //Suppression du id du quiz pour ne pas le répliquer
// delete quiztoduplicate._id;
// //Ajout du duplicata
2024-10-03 12:05:20 -04:00
// await conn.collection('quiz').insertOne({ ...quiztoduplicate, userId: ObjectId.createFromHexString(newUserId) });
2024-03-29 20:08:34 -04:00
// res.json(Response.ok("Dossier dupliqué avec succès pour un autre utilisateur"));
2024-03-29 20:08:34 -04:00
// } catch (error) {
// if (error.message.startsWith("Quiz non trouvé")) {
// return res.status(404).json(Response.badRequest(error.message));
// }
// res.status(500).json(Response.serverError(error.message));
// }
};
deleteQuizzesByFolderId = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId } = req.body;
2024-03-29 20:08:34 -04:00
if (!folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// Call the method from the Quiz model to delete quizzes by folder ID
await Quiz.deleteQuizzesByFolderId(folderId);
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Quizzes deleted successfully.'
});
} catch (error) {
return next(error);
}
};
duplicate = async (req, res, next) => {
const { quizId } = req.body;
2024-03-29 20:08:34 -04:00
try {
const newQuizId = await this.quizzes.duplicate(quizId, req.user.userId);
2024-03-29 20:08:34 -04:00
res.status(200).json({ success: true, newQuizId });
} catch (error) {
return next(error);
}
};
quizExists = async (title, userId) => {
2024-03-29 20:08:34 -04:00
try {
const existingFile = await this.quizzes.quizExists(title, userId);
2024-03-29 20:08:34 -04:00
return existingFile !== null;
} catch (error) {
throw new AppError(GETTING_QUIZ_ERROR);
}
};
share = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId, email } = req.body;
if (!quizId || !email) {
2024-03-29 20:08:34 -04:00
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
const link = `${process.env.FRONTEND_URL}/teacher/Share/${quizId}`;
2024-03-29 20:08:34 -04:00
emailer.quizShare(email, link);
return res.status(200).json({
message: 'Quiz partagé avec succès.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
2024-03-29 20:08:34 -04:00
getShare = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId } = req.params;
if (!quizId) {
2024-03-29 20:08:34 -04:00
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const content = await this.quizzes.getContent(quizId);
2024-03-29 20:08:34 -04:00
if (!content) {
throw new AppError(GETTING_QUIZ_ERROR);
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
data: content.title
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
receiveShare = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { quizId, folderId } = req.body;
if (!quizId || !folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
const folderOwner = await this.folders.getOwner(folderId);
2024-03-29 20:08:34 -04:00
if (folderOwner != req.user.userId) {
throw new AppError(FOLDER_NOT_FOUND);
}
const content = await this.quizzes.getContent(quizId);
2024-03-29 20:08:34 -04:00
if (!content) {
throw new AppError(GETTING_QUIZ_ERROR);
}
const result = await this.quizzes.create(content.title, content.content, folderId, req.user.userId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(QUIZ_ALREADY_EXISTS);
}
return res.status(200).json({
message: 'Quiz partagé reçu.'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
};
2024-03-29 20:08:34 -04:00
}
module.exports = QuizController;