Fixes #1409 - adds username to url when fetching sketch from editor API

This commit is contained in:
Cassie Tarakajian 2020-05-26 17:35:13 -04:00
parent 1dcdfd39db
commit 47e798a7e6
4 changed files with 20 additions and 32 deletions

View file

@ -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));

View file

@ -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);
}
}

View file

@ -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);
});
});
});
}

View file

@ -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);