From 0d36f0d69aaf10fad4608f630a8f7dfe263ea74c Mon Sep 17 00:00:00 2001 From: Pitu Date: Mon, 30 Sep 2019 07:06:35 +0000 Subject: feature: tags logic --- src/api/routes/files/tagAddPOST.js | 27 +++++++++++++++++++++++++++ src/api/routes/files/tagDelPOST.js | 27 +++++++++++++++++++++++++++ src/api/routes/tags/tagPOST.js | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/api/routes/files/tagAddPOST.js create mode 100644 src/api/routes/files/tagDelPOST.js (limited to 'src/api') diff --git a/src/api/routes/files/tagAddPOST.js b/src/api/routes/files/tagAddPOST.js new file mode 100644 index 0000000..9d334d8 --- /dev/null +++ b/src/api/routes/files/tagAddPOST.js @@ -0,0 +1,27 @@ +const Route = require('../../structures/Route'); + +class tagAddPOST extends Route { + constructor() { + super('/file/tag/add', 'post'); + } + + run(req, res, db) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { fileId, tagNames } = req.body; + if (!fileId || !tagNames.length) return res.status(400).json({ message: 'No tags provided' }); + + tagNames.forEach(async tag => { + try { + await db.table('fileTags').insert({ fileId, tag }); + } catch (error) { + return super.error(res, error); + } + }); + + return res.json({ + message: 'Successfully added file to album' + }); + } +} + +module.exports = tagAddPOST; diff --git a/src/api/routes/files/tagDelPOST.js b/src/api/routes/files/tagDelPOST.js new file mode 100644 index 0000000..fd6bbd0 --- /dev/null +++ b/src/api/routes/files/tagDelPOST.js @@ -0,0 +1,27 @@ +const Route = require('../../structures/Route'); + +class albumDelPOST extends Route { + constructor() { + super('/file/album/del', 'post'); + } + + async run(req, res, db) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { fileId, albumId } = req.body; + if (!fileId || !albumId) return res.status(400).json({ message: 'No id provided' }); + + try { + await db.table('albumsFiles') + .where({ fileId, albumId }) + .delete(); + } catch (error) { + return super.error(res, error); + } + + return res.json({ + message: 'Successfully removed file from album' + }); + } +} + +module.exports = albumDelPOST; diff --git a/src/api/routes/tags/tagPOST.js b/src/api/routes/tags/tagPOST.js index 0df36e1..489dac3 100644 --- a/src/api/routes/tags/tagPOST.js +++ b/src/api/routes/tags/tagPOST.js @@ -27,7 +27,7 @@ class tagPOST extends Route { editedAt: now }); - return res.json({ message: 'The album was created successfully' }); + return res.json({ message: 'The tag was created successfully' }); } } -- cgit v1.2.3