Skip to content

Commit 21cd4d4

Browse files
committed
refactor listCollections to async/await
1 parent fa7de89 commit 21cd4d4

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

server/controllers/collection.controller/listCollections.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)