From 34fe78d734ab220c8120791d36bb9478cecd2bc4 Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 24 Aug 2016 17:29:44 -0400 Subject: [PATCH] create file works server side --- client/modules/IDE/actions/files.js | 3 +- server/controllers/file.controller.js | 46 ++++++++++++++++-------- server/controllers/project.controller.js | 20 ++++++++++- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 03d44d6c..f4db7d00 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -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 diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index 6e3dd053..ceed3332 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -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]); + // }); }); } \ No newline at end of file diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 6698f8a7..bdcd728d 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -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); }); }