reworked folder hierarchy fetch + adde folder creation

This commit is contained in:
helori_ollivier
2026-04-26 14:42:43 +02:00
parent 132b8cacd5
commit a0e23b672a
3 changed files with 74 additions and 16 deletions
+68 -14
View File
@@ -1,18 +1,72 @@
const folder = require('../models/folder.model');
exports.getFoldersHierarchy = async (req, res) => {
const folders = await folder.findAll({
where: {
folderParent: null,
},
include: [
{
model: folder,
as: 'subFolders',
hierarchy: true,
}
]
});
exports.getFoldersByHierarchy = async (req, res) => {
let folders = await folder.findAll();
for (let i = 0; i < folders.length; i++) {
folders[i].dataValues.subFolders = []
}
console.log(folders);
let toRemove = []
for (let i = (folders.length - 1); i >= 0; i--) {
parent = findByValue(folders, "folderId", folders[i].folderParent);
if (parent != null) {
console.log("parent: ", parent);
parent.dataValues.subFolders.push(folders[i]);
toRemove.push(i);
}
}
console.log(toRemove)
for (el of toRemove) {
folders.splice(el, 1);
}
console.log(folders);
res.json(folders);
}
}
exports.getFolders = async (req, res) => {
const folders = await folder.findAll();
res.json(folders);
}
exports.getFoldersById = async (req, res) => {
const id = req.params.id
const folders = await folder.findAll({
where: {
folderId: id,
}},
);
res.json(folders);
}
exports.createFolder = async (req, res) => {
const josnFolder = req.body;
await folder.create({
folderName: josnFolder.name,
folderParent: josnFolder.parent,
});
res.status(201).json("ok");
}
function findByValue(list, key, value) {
for (el of list) {
if (el[key] == value) {
return el;
}
}
return null;
}