aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/link
diff options
context:
space:
mode:
authorPitu <[email protected]>2018-09-16 00:56:13 -0300
committerPitu <[email protected]>2018-09-16 00:56:13 -0300
commite7767ac7095f93393a627fd5e867af4a1ca4b011 (patch)
treec571d5a13ac949bc8c826a27772b78992b320d58 /src/api/routes/albums/link
parentUtils (diff)
downloadhost.fuwn.me-e7767ac7095f93393a627fd5e867af4a1ca4b011.tar.xz
host.fuwn.me-e7767ac7095f93393a627fd5e867af4a1ca4b011.zip
Routes
Diffstat (limited to 'src/api/routes/albums/link')
-rw-r--r--src/api/routes/albums/link/linkEnabledPOST.js34
-rw-r--r--src/api/routes/albums/link/linkPOST.js43
2 files changed, 77 insertions, 0 deletions
diff --git a/src/api/routes/albums/link/linkEnabledPOST.js b/src/api/routes/albums/link/linkEnabledPOST.js
new file mode 100644
index 0000000..863fe0b
--- /dev/null
+++ b/src/api/routes/albums/link/linkEnabledPOST.js
@@ -0,0 +1,34 @@
+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;
diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js
new file mode 100644
index 0000000..e8f3731
--- /dev/null
+++ b/src/api/routes/albums/link/linkPOST.js
@@ -0,0 +1,43 @@
+const Route = require('../../../structures/Route');
+const config = require('../../../../../config');
+const db = require('knex')(config.server.database);
+const Util = require('../../../utils/Util');
+const log = require('../../../utils/Log');
+
+class linkPOST extends Route {
+ constructor() {
+ super('/album/link/new', 'post');
+ }
+
+ async run(req, res) {
+ if (!req.body) return res.status(400).json({ message: 'No body provided' });
+ const { albumId, enabled, enableDownload, expiresAt } = req.body;
+ if (!albumId) return res.status(400).json({ message: 'No album provided' });
+
+ const exists = await db.table('albums').where('id', albumId).first();
+ if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' });
+
+ const identifier = Util.getUniqueAlbumIdentifier();
+ if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' });
+
+ try {
+ await db.table('links').insert({
+ identifier,
+ albumId,
+ enabled,
+ enableDownload,
+ expiresAt
+ });
+
+ return res.json({
+ message: 'The link was created successfully',
+ identifier
+ });
+ } catch (error) {
+ log.error(error);
+ return res.status(500).json({ message: 'There was a problem creating the link' });
+ }
+ }
+}
+
+module.exports = linkPOST;