aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorZephyrrus <[email protected]>2020-07-20 21:28:46 +0300
committerZephyrrus <[email protected]>2020-07-20 21:28:46 +0300
commit04660dbce11ff42be2fb02cb93acdbd9b99ad8a0 (patch)
tree09a4609c13a84f88a6b7046a1b4276eaac31efb1 /src/api
parentMerge branch 'dev' into dev-zephy (diff)
downloadhost.fuwn.me-04660dbce11ff42be2fb02cb93acdbd9b99ad8a0.tar.xz
host.fuwn.me-04660dbce11ff42be2fb02cb93acdbd9b99ad8a0.zip
feat: add single tag adding to file
fix: fix the batch tag adding to properly await for a response, and use the proper column while adding the tags
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/files/tagAddBatchPOST.js40
-rw-r--r--src/api/routes/files/tagAddPOST.js26
2 files changed, 55 insertions, 11 deletions
diff --git a/src/api/routes/files/tagAddBatchPOST.js b/src/api/routes/files/tagAddBatchPOST.js
new file mode 100644
index 0000000..67b1b46
--- /dev/null
+++ b/src/api/routes/files/tagAddBatchPOST.js
@@ -0,0 +1,40 @@
+const Route = require('../../structures/Route');
+
+class tagAddPOST extends Route {
+ constructor() {
+ super('/file/tag/addBatch', 'post');
+ }
+
+ async run(req, res, db, user) {
+ 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' });
+
+ // 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.' });
+
+ const errors = {};
+ const addedTags = [];
+ for await (const tagName of tagNames) {
+ try {
+ const tag = await db.table('tags').where({ name: tagName, userId: user.id }).first();
+ if (!tag) throw new Error('Tag doesn\'t exist in the database');
+ await db.table('fileTags').insert({ fileId, tagId: tag.id });
+
+ addedTags.push(tag);
+ } catch (e) {
+ errors[tagName] = e.message;
+ }
+ }
+
+ return res.json({
+ message: 'Successfully added tags to file',
+ data: addedTags,
+ errors,
+ });
+ // eslint-disable-next-line consistent-return
+ }
+}
+
+module.exports = tagAddPOST;
diff --git a/src/api/routes/files/tagAddPOST.js b/src/api/routes/files/tagAddPOST.js
index 07ecb18..3434f24 100644
--- a/src/api/routes/files/tagAddPOST.js
+++ b/src/api/routes/files/tagAddPOST.js
@@ -7,25 +7,29 @@ class tagAddPOST extends Route {
async run(req, res, db, user) {
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' });
+
+ 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.' });
- // eslint-disable-next-line consistent-return
- tagNames.forEach(async (tag) => {
- try {
- await db.table('fileTags').insert({ fileId, tag });
- } catch (error) {
- return super.error(res, error);
- }
- });
+ // 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').insert({ fileId, tagId: tag.id });
+ } catch (error) {
+ return super.error(res, error);
+ }
return res.json({
- message: 'Successfully added file to album',
+ message: 'Successfully added tag to file',
+ data: tagName,
});
+ // eslint-disable-next-line consistent-return
}
}