aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/link/linkPOST.js
diff options
context:
space:
mode:
authorZephyrrus <[email protected]>2020-07-04 03:25:04 +0300
committerZephyrrus <[email protected]>2020-07-04 03:25:04 +0300
commitb620aa981546cb42e19f64d5349a9372e3d0c269 (patch)
tree48f3981b3b6ed264a9c8dc5117fc95127a1d28a5 /src/api/routes/albums/link/linkPOST.js
parentfeat: separate album view into multiple components and use vuex (diff)
downloadhost.fuwn.me-b620aa981546cb42e19f64d5349a9372e3d0c269.tar.xz
host.fuwn.me-b620aa981546cb42e19f64d5349a9372e3d0c269.zip
feat: refactor some of the queries to returned added/updated data
Diffstat (limited to 'src/api/routes/albums/link/linkPOST.js')
-rw-r--r--src/api/routes/albums/link/linkPOST.js24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js
index 6009922..7ecc5cb 100644
--- a/src/api/routes/albums/link/linkPOST.js
+++ b/src/api/routes/albums/link/linkPOST.js
@@ -14,23 +14,32 @@ class linkPOST extends Route {
/*
Make sure the album exists
*/
- const exists = await db.table('albums').where({ id: albumId, userId: user.id }).first();
+ const exists = await db
+ .table('albums')
+ .where({ id: albumId, userId: user.id })
+ .first();
if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' });
/*
Count the amount of links created for that album already and error out if max was reached
*/
- const count = await db.table('links').where('albumId', albumId).count({ count: 'id' });
- if (count[0].count >= parseInt(process.env.MAX_LINKS_PER_ALBUM, 10)) return res.status(400).json({ message: 'Maximum links per album reached' });
+ const count = await db
+ .table('links')
+ .where('albumId', albumId)
+ .count({ count: 'id' })
+ .first();
+ if (count >= parseInt(process.env.MAX_LINKS_PER_ALBUM, 10))
+ return res.status(400).json({ message: 'Maximum links per album reached' });
/*
Try to allocate a new identifier on the db
*/
const identifier = await Util.getUniqueAlbumIdentifier();
- if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' });
+ if (!identifier)
+ return res.status(500).json({ message: 'There was a problem allocating a link for your album' });
try {
- await db.table('links').insert({
+ const insertObj = {
identifier,
userId: user.id,
albumId,
@@ -38,11 +47,12 @@ class linkPOST extends Route {
enableDownload: true,
expiresAt: null,
views: 0
- });
+ };
+ await db.table('links').insert(insertObj);
return res.json({
message: 'The link was created successfully',
- identifier
+ data: insertObj
});
} catch (error) {
return super.error(res, error);