From 3dfed2a11e8e7caa3d838647f8edfa75fb346d3c Mon Sep 17 00:00:00 2001 From: catarak Date: Wed, 24 Aug 2016 17:59:15 -0400 Subject: [PATCH] delete files works, server side --- client/modules/IDE/actions/files.js | 24 ++++++++++++++++++++---- server/controllers/file.controller.js | 11 +++++++++++ server/models/project.js | 2 +- server/routes/file.routes.js | 1 + 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index f4db7d00..e5ffa68d 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -155,9 +155,25 @@ export function updateFileName(id, name) { } export function deleteFile(id, parentId) { - return { - type: ActionTypes.DELETE_FILE, - id, - parentId + return (dispatch, getState) => { + const state = getState(); + const deleteConfig = { + params: { + parentId + } + }; + axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true }) + .then(() => { + dispatch({ + type: ActionTypes.DELETE_FILE, + id, + parentId + }); + }) + .catch(response => dispatch({ + type: ActionTypes.ERROR, + error: response.data + }) + ); }; } diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index ceed3332..29136e4d 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -45,4 +45,15 @@ export function createFile(req, res) { // return res.json(updatedProject.files[updatedProject.files.length - 1]); // }); }); +} + +export function deleteFile(req, res) { + Project.findById(req.params.project_id, (err, project) => { + 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 => { + res.json(project.files); + }) + }); } \ No newline at end of file diff --git a/server/models/project.js b/server/models/project.js index 6e25e792..3811f38c 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -35,7 +35,7 @@ const defaultCSS = const fileSchema = new Schema({ name: { type: String, default: 'sketch.js' }, - content: { type: String }, + content: { type: String, default: '' }, url: { type: String }, children: { type: [ String ], default: [] }, fileType: { type: String }, diff --git a/server/routes/file.routes.js b/server/routes/file.routes.js index c7035ac6..8870208e 100644 --- a/server/routes/file.routes.js +++ b/server/routes/file.routes.js @@ -4,5 +4,6 @@ import * as FileController from '../controllers/file.controller'; const router = new Router(); router.route('/projects/:project_id/files').post(FileController.createFile); +router.route('/projects/:project_id/files/:file_id').delete(FileController.deleteFile); export default router; \ No newline at end of file