adding and deleting files works server side
This commit is contained in:
parent
f2fedd8457
commit
69bb0d0077
1 changed files with 29 additions and 21 deletions
|
@ -26,32 +26,40 @@ export function createFile(req, res) {
|
||||||
}
|
}
|
||||||
return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
||||||
});
|
});
|
||||||
// console.log(updatedProject.files);
|
});
|
||||||
// console.log(req.body.parentId);
|
}
|
||||||
// Project.findByIdAndUpdate(
|
|
||||||
// {"_id": req.params.project_id, "files._id": ObjectId(req.body.parentId)},
|
function getAllDescendantIds(files, nodeId) {
|
||||||
// {
|
return files.find(file => file.id === nodeId).children
|
||||||
// $push: {
|
.reduce((acc, childId) => (
|
||||||
// 'files.$.children': newFile._id
|
[...acc, childId, ...getAllDescendantIds(files, childId)]
|
||||||
// }
|
), []);
|
||||||
// },
|
}
|
||||||
// {
|
|
||||||
// new: true
|
function deleteMany(files, ids) {
|
||||||
// }, (errAgain, updatedProjectAgain) => {
|
ids.forEach(id => {
|
||||||
// if (errAgain) {
|
files.id(id).remove();
|
||||||
// console.log(errAgain);
|
});
|
||||||
// return res.json({ success: false });
|
}
|
||||||
// }
|
|
||||||
// return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
function deleteChild(files, parentId, id) {
|
||||||
// });
|
files = files.map((file) => {
|
||||||
|
if (file.id === parentId) {
|
||||||
|
file.children = file.children.filter(child => child !== id);
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
return file;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteFile(req, res) {
|
export function deleteFile(req, res) {
|
||||||
Project.findById(req.params.project_id, (err, project) => {
|
Project.findById(req.params.project_id, (err, project) => {
|
||||||
project.files.id(req.params.file_id).remove();
|
const idsToDelete = getAllDescendantIds(project.files, req.params.file_id);
|
||||||
const childrenArray = project.files.id(req.query.parentId).children;
|
deleteMany(project.files, [req.params.file_id, ...idsToDelete]);
|
||||||
project.files.id(req.query.parentId).children = childrenArray.filter(id => id !== req.params.file_id);
|
deleteChild(project.files, req.query.parentId, req.params.file_id);
|
||||||
|
// project.files.id(req.params.file_id).remove();
|
||||||
|
// const childrenArray = project.files.id(req.query.parentId).children;
|
||||||
|
// project.files.id(req.query.parentId).children = childrenArray.filter(id => id !== req.params.file_id);
|
||||||
project.save(innerErr => {
|
project.save(innerErr => {
|
||||||
res.json(project.files);
|
res.json(project.files);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue