@@ -63,25 +63,21 @@ export function updateProject(req, res) {
6363}
6464
6565export 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
8783export 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}
0 commit comments