diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 192e55dd..d42c307d 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -49,10 +49,10 @@ export function setNewProject(project) { }; } -export function getProject(id) { +export function getProject(id, username) { return (dispatch, getState) => { dispatch(justOpenedProject()); - axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true }) + axios.get(`${ROOT_URL}/${username}/projects/${id}`, { withCredentials: true }) .then((response) => { dispatch(setProject(response.data)); dispatch(setUnsavedChanges(false)); diff --git a/client/modules/IDE/pages/IDEView.jsx b/client/modules/IDE/pages/IDEView.jsx index 14ac255c..50acea22 100644 --- a/client/modules/IDE/pages/IDEView.jsx +++ b/client/modules/IDE/pages/IDEView.jsx @@ -53,9 +53,9 @@ class IDEView extends React.Component { this.props.stopSketch(); if (this.props.params.project_id) { - const id = this.props.params.project_id; + const { project_id: id, username } = this.props.params; if (id !== this.props.project.id) { - this.props.getProject(id); + this.props.getProject(id, username); } } diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 1e15619b..ab4d4a63 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -63,25 +63,21 @@ export function updateProject(req, res) { } export function getProject(req, res) { - const projectId = req.params.project_id; - Project.findById(projectId) - .populate('user', 'username') - .exec((err, project) => { // eslint-disable-line - if (err) { - return res.status(404).send({ message: 'Project with that id does not exist' }); - } else if (!project) { - Project.findOne({ slug: projectId }) - .populate('user', 'username') - .exec((innerErr, projectBySlug) => { - if (innerErr || !projectBySlug) { - return res.status(404).send({ message: 'Project with that id does not exist' }); - } - return res.json(projectBySlug); - }); - } else { + const { project_id: projectId, username } = req.params; + User.findOne({ username }, (err, user) => { // eslint-disable-line + if (!user) { + return res.status(404).send({ message: 'Project with that username does not exist' }); + } + Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }) + .populate('user', 'username') + .exec((err, project) => { // eslint-disable-line + if (err) { + console.log(err); + return res.status(404).send({ message: 'Project with that id does not exist' }); + } return res.json(project); - } - }); + }); + }); } export function getProjectsForUserId(userId) { @@ -150,18 +146,10 @@ export function projectForUserExists(username, projectId, callback) { callback(false); return; } - Project.findOne({ _id: projectId, user: user._id }, (innerErr, project) => { + Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }, (innerErr, project) => { if (project) { callback(true); - return; } - Project.findOne({ slug: projectId, user: user._id }, (slugError, projectBySlug) => { - if (projectBySlug) { - callback(true); - return; - } - callback(false); - }); }); }); } diff --git a/server/routes/project.routes.js b/server/routes/project.routes.js index 5ccd2d91..073ab90d 100644 --- a/server/routes/project.routes.js +++ b/server/routes/project.routes.js @@ -8,7 +8,7 @@ router.post('/projects', isAuthenticated, ProjectController.createProject); router.put('/projects/:project_id', isAuthenticated, ProjectController.updateProject); -router.get('/projects/:project_id', ProjectController.getProject); +router.get('/:username/projects/:project_id', ProjectController.getProject); router.delete('/projects/:project_id', isAuthenticated, ProjectController.deleteProject);