Skip to content

Commit 47e798a

Browse files
committed
Fixes #1409 - adds username to url when fetching sketch from editor API
1 parent 1dcdfd3 commit 47e798a

File tree

4 files changed

+20
-32
lines changed

4 files changed

+20
-32
lines changed

Diff for: client/modules/IDE/actions/project.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export function setNewProject(project) {
4949
};
5050
}
5151

52-
export function getProject(id) {
52+
export function getProject(id, username) {
5353
return (dispatch, getState) => {
5454
dispatch(justOpenedProject());
55-
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
55+
axios.get(`${ROOT_URL}/${username}/projects/${id}`, { withCredentials: true })
5656
.then((response) => {
5757
dispatch(setProject(response.data));
5858
dispatch(setUnsavedChanges(false));

Diff for: client/modules/IDE/pages/IDEView.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class IDEView extends React.Component {
5353

5454
this.props.stopSketch();
5555
if (this.props.params.project_id) {
56-
const id = this.props.params.project_id;
56+
const { project_id: id, username } = this.props.params;
5757
if (id !== this.props.project.id) {
58-
this.props.getProject(id);
58+
this.props.getProject(id, username);
5959
}
6060
}
6161

Diff for: server/controllers/project.controller.js

+15-27
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,21 @@ export function updateProject(req, res) {
6363
}
6464

6565
export function getProject(req, res) {
66-
const projectId = req.params.project_id;
67-
Project.findById(projectId)
68-
.populate('user', 'username')
69-
.exec((err, project) => { // eslint-disable-line
70-
if (err) {
71-
return res.status(404).send({ message: 'Project with that id does not exist' });
72-
} else if (!project) {
73-
Project.findOne({ slug: projectId })
74-
.populate('user', 'username')
75-
.exec((innerErr, projectBySlug) => {
76-
if (innerErr || !projectBySlug) {
77-
return res.status(404).send({ message: 'Project with that id does not exist' });
78-
}
79-
return res.json(projectBySlug);
80-
});
81-
} else {
66+
const { project_id: projectId, username } = req.params;
67+
User.findOne({ username }, (err, user) => { // eslint-disable-line
68+
if (!user) {
69+
return res.status(404).send({ message: 'Project with that username does not exist' });
70+
}
71+
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] })
72+
.populate('user', 'username')
73+
.exec((err, project) => { // eslint-disable-line
74+
if (err) {
75+
console.log(err);
76+
return res.status(404).send({ message: 'Project with that id does not exist' });
77+
}
8278
return res.json(project);
83-
}
84-
});
79+
});
80+
});
8581
}
8682

8783
export function getProjectsForUserId(userId) {
@@ -150,18 +146,10 @@ export function projectForUserExists(username, projectId, callback) {
150146
callback(false);
151147
return;
152148
}
153-
Project.findOne({ _id: projectId, user: user._id }, (innerErr, project) => {
149+
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }, (innerErr, project) => {
154150
if (project) {
155151
callback(true);
156-
return;
157152
}
158-
Project.findOne({ slug: projectId, user: user._id }, (slugError, projectBySlug) => {
159-
if (projectBySlug) {
160-
callback(true);
161-
return;
162-
}
163-
callback(false);
164-
});
165153
});
166154
});
167155
}

Diff for: server/routes/project.routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ router.post('/projects', isAuthenticated, ProjectController.createProject);
88

99
router.put('/projects/:project_id', isAuthenticated, ProjectController.updateProject);
1010

11-
router.get('/projects/:project_id', ProjectController.getProject);
11+
router.get('/:username/projects/:project_id', ProjectController.getProject);
1212

1313
router.delete('/projects/:project_id', isAuthenticated, ProjectController.deleteProject);
1414

0 commit comments

Comments
 (0)