create file works server side

This commit is contained in:
catarak 2016-08-24 17:29:44 -04:00
parent 5f694329db
commit 34fe78d734
3 changed files with 52 additions and 17 deletions

View File

@ -85,7 +85,8 @@ export function createFile(formProps) {
}
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data
...response.data,
parentId: rootFile.id
});
dispatch({
type: ActionTypes.HIDE_MODAL

View File

@ -1,4 +1,4 @@
import Project from '../models/project'
import Project from '../models/project';
// Bug -> timestamps don't get created, but it seems like this will
// be fixed in mongoose soon
@ -13,20 +13,36 @@ export function createFile(req, res) {
{
new: true
}, (err, updatedProject) => {
if (err) { return res.json({ success: false }); }
if (err) {
console.log(err);
return res.json({ success: false });
}
const newFile = updatedProject.files[updatedProject.files.length - 1];
Project.findByIdAndUpdate(
{"_id": req.params.project_id, "files._id": req.params.parentId},
{
$push: {
'files.$.children': newFile.id
}
},
{
new: true
}, (errAgain, updatedProjectAgain) => {
if (errAgain) { return res.json({ success: false }); }
return res.json(updatedProject.files[updatedProject.files.length - 1]);
});
updatedProject.files.id(req.body.parentId).children.push(newFile.id);
updatedProject.save(innerErr => {
if (innerErr) {
console.log(innerErr);
return res.json({ success: false });
}
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)},
// {
// $push: {
// 'files.$.children': newFile._id
// }
// },
// {
// new: true
// }, (errAgain, updatedProjectAgain) => {
// if (errAgain) {
// console.log(errAgain);
// return res.json({ success: false });
// }
// return res.json(updatedProject.files[updatedProject.files.length - 1]);
// });
});
}

View File

@ -26,7 +26,25 @@ export function updateProject(req, res) {
})
.populate('user', 'username')
.exec((err, updatedProject) => {
if (err) { return res.json({ success: false }); }
if (err) {
console.log(err);
return res.json({ success: false });
}
if (updatedProject.files.length !== req.body.files.length) {
const oldFileIds = updatedProject.files.map(file => file.id);
const newFileIds = req.body.files.map(file => file.id);
const staleIds = oldFileIds.filter(id => newFileIds.indexOf(id) === -1);
staleIds.forEach(staleId => {
updatedProject.files.id(staleId).remove();
});
updatedProject.save((innerErr) => {
if (innerErr) {
console.log(innerErr);
return res.json({ success: false });
}
return res.json(updatedProject);
});
}
return res.json(updatedProject);
});
}