2016-07-18 04:38:41 +00:00
|
|
|
import Project from '../models/project'
|
2016-07-13 22:53:56 +00:00
|
|
|
|
|
|
|
// Bug -> timestamps don't get created, but it seems like this will
|
|
|
|
// be fixed in mongoose soon
|
|
|
|
// https://github.com/Automattic/mongoose/issues/4049
|
|
|
|
export function createFile(req, res) {
|
|
|
|
Project.findByIdAndUpdate(req.params.project_id,
|
|
|
|
{
|
|
|
|
$push: {
|
|
|
|
'files': req.body
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true
|
|
|
|
}, (err, updatedProject) => {
|
|
|
|
if (err) { return res.json({ success: false }); }
|
2016-08-23 23:40:47 +00:00
|
|
|
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]);
|
|
|
|
});
|
2016-07-13 22:53:56 +00:00
|
|
|
});
|
|
|
|
}
|