mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
238 lines
No EOL
6.7 KiB
TypeScript
238 lines
No EOL
6.7 KiB
TypeScript
//controller
|
|
import AppError from '../middleware/app-error.js';
|
|
import { 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 } from '../constants/error-codes.js';
|
|
import FolderRepository from '../repository/folder-repository.js';
|
|
import Folder from '../models/folder-model.js';
|
|
|
|
|
|
// controllers must use arrow functions to bind 'this' to the class instance in order to access class properties as callbacks in Express
|
|
class FolderController{
|
|
|
|
/***
|
|
* Basic queries
|
|
*/
|
|
|
|
getUserFolders = async (req, res, next) => {
|
|
try {
|
|
const folders = await this.folders.getUserFolders(req.user.userId);
|
|
|
|
if (!folders) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
data: folders
|
|
});
|
|
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
getFolderContent = async (req, res, next) => {
|
|
try {
|
|
const { folderId } = req.params;
|
|
|
|
if (!folderId) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const content = await this.folders.getContent(folderId);
|
|
|
|
if (!content) {
|
|
throw new AppError(GETTING_FOLDER_ERROR);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
data: content
|
|
});
|
|
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
delete = async (req, res, next) => {
|
|
try {
|
|
const { folderId } = req.params;
|
|
|
|
if (!folderId) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const result = await this.folders.delete(folderId);
|
|
|
|
if (!result) {
|
|
throw new AppError(DELETE_FOLDER_ERROR);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
message: 'Dossier supprimé avec succès.'
|
|
});
|
|
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
rename = async (req, res, next) => {
|
|
try {
|
|
const { folderId, newTitle } = req.body;
|
|
|
|
if (!folderId || !newTitle) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const result = await this.folders.rename(folderId, newTitle);
|
|
|
|
if (!result) {
|
|
throw new AppError(UPDATE_FOLDER_ERROR);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
message: 'Dossier mis à jours avec succès.'
|
|
});
|
|
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
duplicate = async (req, res, next) => {
|
|
try {
|
|
const { folderId } = req.body;
|
|
|
|
if (!folderId) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const userId = req.user.userId;
|
|
|
|
const newFolderId = await this.folders.duplicate(folderId, userId);
|
|
|
|
if (!newFolderId) {
|
|
throw new AppError(DUPLICATE_FOLDER_ERROR);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
message: 'Dossier dupliqué avec succès.',
|
|
newFolderId: newFolderId
|
|
});
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
copy = async (req, res, next) => {
|
|
try {
|
|
const { folderId, newTitle } = req.body;
|
|
|
|
if (!folderId || !newTitle) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const userId = req.user.userId; // Assuming userId is obtained from authentication
|
|
|
|
const newFolderId = await this.folders.copy(folderId, userId);
|
|
|
|
if (!newFolderId) {
|
|
throw new AppError(COPY_FOLDER_ERROR);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
message: 'Dossier copié avec succès.',
|
|
newFolderId: newFolderId
|
|
});
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
getFolderById = async (req, res, next) => {
|
|
try {
|
|
const { folderId } = req.params;
|
|
|
|
if (!folderId) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
// Is this folder mine
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
if (owner != req.user.userId) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
const folder = await this.folders.getFolderById(folderId);
|
|
|
|
if (!folder) {
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
}
|
|
|
|
return res.status(200).json({
|
|
data: folder
|
|
});
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
folderExists = async (req, res, next) => {
|
|
try {
|
|
const { title } = req.body;
|
|
|
|
if (!title) {
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
}
|
|
|
|
const userId = req.user.userId;
|
|
|
|
// Vérifie si le dossier existe pour l'utilisateur donné
|
|
const exists = await this.folders.folderExists(title, userId);
|
|
|
|
return res.status(200).json({
|
|
exists: exists
|
|
});
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
export default FolderController |