EvalueTonSavoir/server/controllers/folders.js

264 lines
7.2 KiB
JavaScript
Raw Permalink Normal View History

2024-03-29 20:08:34 -04:00
//controller
const AppError = require('../middleware/AppError.js');
2024-04-05 20:10:59 -04:00
const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, FOLDER_NOT_FOUND, FOLDER_ALREADY_EXISTS, GETTING_FOLDER_ERROR, DELETE_FOLDER_ERROR, UPDATE_FOLDER_ERROR, MOVING_FOLDER_ERROR, DUPLICATE_FOLDER_ERROR, COPY_FOLDER_ERROR } = require('../constants/errorCodes');
2024-03-29 20:08:34 -04:00
2024-10-01 22:14:38 -04:00
// controllers must use arrow functions to bind 'this' to the class instance in order to access class properties as callbacks in Express
2024-03-29 20:08:34 -04:00
class FoldersController {
constructor(foldersModel) {
this.folders = foldersModel;
2024-10-01 22:14:38 -04:00
}
2024-03-29 20:08:34 -04:00
/***
* Basic queries
*/
2024-10-01 22:14:38 -04:00
create = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { title } = req.body;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!title) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
const result = await this.folders.create(title, req.user.userId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(FOLDER_ALREADY_EXISTS);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Dossier créé avec succès.'
});
2024-10-01 22:14:38 -04:00
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
2024-10-01 22:14:38 -04:00
getUserFolders = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
2024-10-01 22:14:38 -04:00
const folders = await this.folders.getUserFolders(req.user.userId);
2024-03-29 20:08:34 -04:00
if (!folders) {
throw new AppError(FOLDER_NOT_FOUND);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
data: folders
});
2024-10-01 22:14:38 -04:00
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
2024-10-01 22:14:38 -04:00
getFolderContent = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId } = req.params;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
const content = await this.folders.getContent(folderId);
2024-03-29 20:08:34 -04:00
if (!content) {
throw new AppError(GETTING_FOLDER_ERROR);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
data: content
});
2024-10-01 22:14:38 -04:00
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
2024-10-01 22:14:38 -04:00
delete = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId } = req.params;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
const result = await this.folders.delete(folderId);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(DELETE_FOLDER_ERROR);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Dossier supprimé avec succès.'
});
2024-10-01 22:14:38 -04:00
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
2024-10-01 22:14:38 -04:00
rename = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId, newTitle } = req.body;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!folderId || !newTitle) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
const result = await this.folders.rename(folderId, newTitle);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(UPDATE_FOLDER_ERROR);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Dossier mis à jours avec succès.'
});
2024-10-01 22:14:38 -04:00
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
2024-10-01 22:14:38 -04:00
duplicate = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
2024-10-01 22:14:38 -04:00
const { folderId } = req.body;
if (!folderId) {
2024-03-29 20:08:34 -04:00
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
const userId = req.user.userId;
const newFolderId = await this.folders.duplicate(folderId, userId);
2024-03-29 20:08:34 -04:00
if (!newFolderId) {
throw new AppError(DUPLICATE_FOLDER_ERROR);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Dossier dupliqué avec succès.',
newFolderId: newFolderId
});
} catch (error) {
return next(error);
}
}
2024-10-01 22:14:38 -04:00
copy = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId, newTitle } = req.body;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!folderId || !newTitle) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
const userId = req.user.userId; // Assuming userId is obtained from authentication
2024-10-01 22:14:38 -04:00
const newFolderId = await this.folders.copy(folderId, userId);
2024-03-29 20:08:34 -04:00
if (!newFolderId) {
throw new AppError(COPY_FOLDER_ERROR);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Dossier copié avec succès.',
newFolderId: newFolderId
});
} catch (error) {
return next(error);
}
}
2024-10-01 22:14:38 -04:00
getFolderById = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { folderId } = req.params;
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
if (!folderId) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
// Is this folder mine
2024-10-01 22:14:38 -04:00
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);
}
2024-10-01 22:14:38 -04:00
const folder = await this.folders.getFolderById(folderId);
2024-03-29 20:08:34 -04:00
if (!folder) {
throw new AppError(FOLDER_NOT_FOUND);
}
2024-10-01 22:14:38 -04:00
2024-03-29 20:08:34 -04:00
return res.status(200).json({
data: folder
});
} catch (error) {
return next(error);
}
}
2024-10-01 22:14:38 -04:00
folderExists = async (req, res, next) => {
2024-03-29 20:08:34 -04:00
try {
const { title } = req.body;
if (!title) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-10-01 22:14:38 -04:00
const userId = req.user.userId;
2024-03-29 20:08:34 -04:00
// Vérifie si le dossier existe pour l'utilisateur donné
2024-10-01 22:14:38 -04:00
const exists = await this.folders.folderExists(title, userId);
2024-03-29 20:08:34 -04:00
return res.status(200).json({
exists: exists
});
} catch (error) {
return next(error);
}
}
}
module.exports = FoldersController;