From 46ed1c6a824252fc5ae0dad5b5c2a369bad9ad39 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Mon, 17 Sep 2018 04:37:27 -0300 Subject: This route should handle more stuff, so it does now --- src/api/routes/albums/link/linkEditPOST.js | 38 +++++++++++++++++++++++++++ src/api/routes/albums/link/linkEnabledPOST.js | 34 ------------------------ 2 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 src/api/routes/albums/link/linkEditPOST.js delete mode 100644 src/api/routes/albums/link/linkEnabledPOST.js (limited to 'src/api') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js new file mode 100644 index 0000000..46b851a --- /dev/null +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -0,0 +1,38 @@ +const Route = require('../../../structures/Route'); +const config = require('../../../../../config'); +const db = require('knex')(config.server.database); +const log = require('../../../utils/Log'); + +class linkEditPOST extends Route { + constructor() { + super('/album/link/edit', 'post'); + } + + async run(req, res, user) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { identifier, enabled, enableDownload, expiresAt } = req.body; + if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); + + const link = await db.table('links').where({ + identifier, + userId: user.id + }).first(); + + if (!link) return res.status(400).json({ message: 'The link doesn\'t exist or doesn\'t belong to the user' }); + try { + await db.table('links') + .where({ identifier }) + .update({ + enabled: enabled || false, + enableDownload: enableDownload || false, + expiresAt // This one should be null if not supplied + }); + return res.json({ message: 'Editing the link was successfully' }); + } catch (error) { + log.error(error); + return res.json({ message: 'There was a problem editing the link' }); + } + } +} + +module.exports = linkEditPOST; diff --git a/src/api/routes/albums/link/linkEnabledPOST.js b/src/api/routes/albums/link/linkEnabledPOST.js deleted file mode 100644 index 863fe0b..0000000 --- a/src/api/routes/albums/link/linkEnabledPOST.js +++ /dev/null @@ -1,34 +0,0 @@ -const Route = require('../../../structures/Route'); -const config = require('../../../../../config'); -const db = require('knex')(config.server.database); -const log = require('../../../utils/Log'); - -class linkEnabledPOST extends Route { - constructor() { - super('/album/link/enabled', 'post'); - } - - async run(req, res, user) { - if (!req.body) return res.status(400).json({ message: 'No body provided' }); - const { identifier, enabled } = req.body; - if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); - - const link = await db.table('links').where({ - identifier, - userId: user.id - }).first(); - - if (!link) return res.status(400).json({ message: 'The link doesn\'t exist or doesn\'t belong to the user' }); - try { - await db.table('links') - .where({ identifier }) - .update({ enabled }); - return res.json({ message: 'The link status was changed successfully' }); - } catch (error) { - log.error(error); - return res.json({ message: 'There was a problem changing the status of the link' }); - } - } -} - -module.exports = linkEnabledPOST; -- cgit v1.2.3