@@ -14,21 +14,23 @@ async function getOwnerUserId(req) {
14
14
return null ;
15
15
}
16
16
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' } ) => {
19
19
res . status ( code ) . json ( { success : false , message } ) ;
20
- }
20
+ } ;
21
21
22
- function sendSuccess ( collections ) {
22
+ const sendSuccess = ( collections ) => {
23
23
res . status ( 200 ) . json ( collections ) ;
24
- }
24
+ } ;
25
+
26
+ try {
27
+ const owner = await getOwnerUserId ( req ) ;
25
28
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' ) ;
29
31
}
30
32
31
- return Collection . find ( { owner } ) . populate ( [
33
+ const collections = await Collection . find ( { owner } ) . populate ( [
32
34
{ path : 'owner' , select : [ 'id' , 'username' ] } ,
33
35
{
34
36
path : 'items.project' ,
@@ -39,10 +41,9 @@ export default function listCollections(req, res) {
39
41
}
40
42
}
41
43
] ) ;
42
- }
43
44
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
+ }
48
49
}
0 commit comments