@@ -63,25 +63,21 @@ export function updateProject(req, res) {
63
63
}
64
64
65
65
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
+ }
82
78
return res . json ( project ) ;
83
- }
84
- } ) ;
79
+ } ) ;
80
+ } ) ;
85
81
}
86
82
87
83
export function getProjectsForUserId ( userId ) {
@@ -150,18 +146,10 @@ export function projectForUserExists(username, projectId, callback) {
150
146
callback ( false ) ;
151
147
return ;
152
148
}
153
- Project . findOne ( { _id : projectId , user : user . _id } , ( innerErr , project ) => {
149
+ Project . findOne ( { user : user . _id , $or : [ { _id : projectId } , { slug : projectId } ] } , ( innerErr , project ) => {
154
150
if ( project ) {
155
151
callback ( true ) ;
156
- return ;
157
152
}
158
- Project . findOne ( { slug : projectId , user : user . _id } , ( slugError , projectBySlug ) => {
159
- if ( projectBySlug ) {
160
- callback ( true ) ;
161
- return ;
162
- }
163
- callback ( false ) ;
164
- } ) ;
165
153
} ) ;
166
154
} ) ;
167
155
}
0 commit comments