@@ -14,21 +14,23 @@ async function getOwnerUserId(req) {
1414 return null ;
1515}
1616
17- export default function listCollections ( req , res ) {
18- function sendFailure ( { code = 500 , message = 'Something went wrong' } ) {
17+ export default async function listCollections ( req , res ) {
18+ const sendFailure = ( { code = 500 , message = 'Something went wrong' } ) => {
1919 res . status ( code ) . json ( { success : false , message } ) ;
20- }
20+ } ;
2121
22- function sendSuccess ( collections ) {
22+ const sendSuccess = ( collections ) => {
2323 res . status ( 200 ) . json ( collections ) ;
24- }
24+ } ;
25+
26+ try {
27+ const owner = await getOwnerUserId ( req ) ;
2528
26- function findCollections ( owner ) {
27- if ( owner == null ) {
28- sendFailure ( { code : 404 , message : 'User not found' } ) ;
29+ if ( ! owner ) {
30+ sendFailure ( '404' , 'User not found' ) ;
2931 }
3032
31- return Collection . find ( { owner } ) . populate ( [
33+ const collections = await Collection . find ( { owner } ) . populate ( [
3234 { path : 'owner' , select : [ 'id' , 'username' ] } ,
3335 {
3436 path : 'items.project' ,
@@ -39,10 +41,9 @@ export default function listCollections(req, res) {
3941 }
4042 }
4143 ] ) ;
42- }
4344
44- return getOwnerUserId ( req )
45- . then ( findCollections )
46- . then ( sendSuccess )
47- . catch ( sendFailure ) ;
45+ sendSuccess ( collections ) ;
46+ } catch ( error ) {
47+ sendFailure ( error . code || 500 , error . message || 'Something went wrong' ) ;
48+ }
4849}
0 commit comments