Merge pull request #1436 from processing/slug-api-endpoint
Fixes #1409 - adds username to url when fetching sketch from editor API
This commit is contained in:
commit
90195f4dd0
4 changed files with 20 additions and 32 deletions
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,24 +63,20 @@ export function updateProject(req, res) {
|
|||
}
|
||||
|
||||
export function getProject(req, res) {
|
||||
const projectId = req.params.project_id;
|
||||
Project.findById(projectId)
|
||||
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) {
|
||||
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) {
|
||||
console.log(err);
|
||||
return res.status(404).send({ message: 'Project with that id does not exist' });
|
||||
}
|
||||
return res.json(projectBySlug);
|
||||
});
|
||||
} else {
|
||||
return res.json(project);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue