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) => {
|
return (dispatch, getState) => {
|
||||||
dispatch(justOpenedProject());
|
dispatch(justOpenedProject());
|
||||||
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
|
axios.get(`${ROOT_URL}/${username}/projects/${id}`, { withCredentials: true })
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
dispatch(setProject(response.data));
|
dispatch(setProject(response.data));
|
||||||
dispatch(setUnsavedChanges(false));
|
dispatch(setUnsavedChanges(false));
|
||||||
|
|
|
@ -53,9 +53,9 @@ class IDEView extends React.Component {
|
||||||
|
|
||||||
this.props.stopSketch();
|
this.props.stopSketch();
|
||||||
if (this.props.params.project_id) {
|
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) {
|
if (id !== this.props.project.id) {
|
||||||
this.props.getProject(id);
|
this.props.getProject(id, username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,25 +63,21 @@ export function updateProject(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProject(req, res) {
|
export function getProject(req, res) {
|
||||||
const projectId = req.params.project_id;
|
const { project_id: projectId, username } = req.params;
|
||||||
Project.findById(projectId)
|
User.findOne({ username }, (err, user) => { // eslint-disable-line
|
||||||
.populate('user', 'username')
|
if (!user) {
|
||||||
.exec((err, project) => { // eslint-disable-line
|
return res.status(404).send({ message: 'Project with that username does not exist' });
|
||||||
if (err) {
|
}
|
||||||
return res.status(404).send({ message: 'Project with that id does not exist' });
|
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] })
|
||||||
} else if (!project) {
|
.populate('user', 'username')
|
||||||
Project.findOne({ slug: projectId })
|
.exec((err, project) => { // eslint-disable-line
|
||||||
.populate('user', 'username')
|
if (err) {
|
||||||
.exec((innerErr, projectBySlug) => {
|
console.log(err);
|
||||||
if (innerErr || !projectBySlug) {
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
||||||
return res.status(404).send({ message: 'Project with that id does not exist' });
|
}
|
||||||
}
|
|
||||||
return res.json(projectBySlug);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return res.json(project);
|
return res.json(project);
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getProjectsForUserId(userId) {
|
export function getProjectsForUserId(userId) {
|
||||||
|
@ -150,18 +146,10 @@ export function projectForUserExists(username, projectId, callback) {
|
||||||
callback(false);
|
callback(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Project.findOne({ _id: projectId, user: user._id }, (innerErr, project) => {
|
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }, (innerErr, project) => {
|
||||||
if (project) {
|
if (project) {
|
||||||
callback(true);
|
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.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);
|
router.delete('/projects/:project_id', isAuthenticated, ProjectController.deleteProject);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue