diff options
| author | Zephyrrus <[email protected]> | 2020-07-20 22:40:00 +0300 |
|---|---|---|
| committer | Zephyrrus <[email protected]> | 2020-07-20 22:40:00 +0300 |
| commit | 9de50b26f1868217a547737741863c7ee6e760b8 (patch) | |
| tree | fc2180a898c024ac1dfd5f68e623c44b2bc56d2b /src | |
| parent | fix: join tags by the proper key (diff) | |
| download | host.fuwn.me-9de50b26f1868217a547737741863c7ee6e760b8.tar.xz host.fuwn.me-9de50b26f1868217a547737741863c7ee6e760b8.zip | |
feat: add tag deletion from images
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/routes/files/tagDelPOST.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/api/routes/files/tagDelPOST.js b/src/api/routes/files/tagDelPOST.js new file mode 100644 index 0000000..4d45493 --- /dev/null +++ b/src/api/routes/files/tagDelPOST.js @@ -0,0 +1,38 @@ +const Route = require('../../structures/Route'); + +class tagDelPost extends Route { + constructor() { + super('/file/tag/del', 'post'); + } + + async run(req, res, db, user) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + + const { fileId, tagName } = req.body; + if (!fileId || !tagName.length) return res.status(400).json({ message: 'No tag provided' }); + + // Make sure the file belongs to the user + const file = await db.table('files').where({ id: fileId, userId: user.id }).first(); + if (!file) return res.status(400).json({ message: 'File doesn\'t exist.' }); + + // Make sure user has a tag like that + const tag = await db.table('tags').where({ name: tagName, userId: user.id }).first(); + if (!tag) return res.status(400).json({ message: 'Tag doesn\'t exist. ' }); + + try { + await db.table('fileTags') + .where({ fileId, tagId: tag.id }) + .delete(); + } catch (error) { + return super.error(res, error); + } + + return res.json({ + message: 'Successfully removed tag from file', + data: { fileId, tag }, + }); + // eslint-disable-next-line consistent-return + } +} + +module.exports = tagDelPost; |