aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/routes')
-rw-r--r--src/api/routes/tags/tagDELETE.js37
-rw-r--r--src/api/routes/tags/tagPOST.js34
-rw-r--r--src/api/routes/tags/tagsGET.js31
3 files changed, 102 insertions, 0 deletions
diff --git a/src/api/routes/tags/tagDELETE.js b/src/api/routes/tags/tagDELETE.js
new file mode 100644
index 0000000..c03ca64
--- /dev/null
+++ b/src/api/routes/tags/tagDELETE.js
@@ -0,0 +1,37 @@
+const Route = require('../../structures/Route');
+const Util = require('../../utils/Util');
+
+class tagDELETE extends Route {
+ constructor() {
+ super('/tag/:id/:purge*?', 'delete');
+ }
+
+ async run(req, res, db, user) {
+ const { id, purge } = req.params;
+ if (!id) return res.status(400).json({ message: 'Invalid tag supplied' });
+
+ /*
+ Check if the tag exists
+ */
+ const tag = await db.table('tags').where({ id, userId: user.id }).first();
+ if (!tag) return res.status(400).json({ message: 'The tag doesn\'t exist or doesn\'t belong to the user' });
+
+ try {
+ /*
+ Should we also delete every file of that tag?
+ */
+ if (purge) {
+ await Util.deleteAllFilesFromTag(id);
+ }
+ /*
+ Delete the tag
+ */
+ await db.table('tags').where({ id }).delete();
+ return res.json({ message: 'The tag was deleted successfully' });
+ } catch (error) {
+ return super.error(res, error);
+ }
+ }
+}
+
+module.exports = tagDELETE;
diff --git a/src/api/routes/tags/tagPOST.js b/src/api/routes/tags/tagPOST.js
new file mode 100644
index 0000000..0df36e1
--- /dev/null
+++ b/src/api/routes/tags/tagPOST.js
@@ -0,0 +1,34 @@
+const Route = require('../../structures/Route');
+const moment = require('moment');
+const util = require('../../utils/Util');
+
+class tagPOST extends Route {
+ constructor() {
+ super('/tag/new', 'post');
+ }
+
+ async run(req, res, db, user) {
+ if (!req.body) return res.status(400).json({ message: 'No body provided' });
+ const { name } = req.body;
+ if (!name) return res.status(400).json({ message: 'No name provided' });
+
+ /*
+ Check that a tag with that name doesn't exist yet
+ */
+ const tag = await db.table('tags').where({ name, userId: user.id }).first();
+ if (tag) return res.status(401).json({ message: 'There\'s already a tag with that name' });
+
+ const now = moment.utc().toDate();
+ await db.table('tags').insert({
+ name,
+ uuid: util.uuid(),
+ userId: user.id,
+ createdAt: now,
+ editedAt: now
+ });
+
+ return res.json({ message: 'The album was created successfully' });
+ }
+}
+
+module.exports = tagPOST;
diff --git a/src/api/routes/tags/tagsGET.js b/src/api/routes/tags/tagsGET.js
new file mode 100644
index 0000000..871148e
--- /dev/null
+++ b/src/api/routes/tags/tagsGET.js
@@ -0,0 +1,31 @@
+const Route = require('../../structures/Route');
+const Util = require('../../utils/Util');
+
+class tagsGET extends Route {
+ constructor() {
+ super('/tags', 'get');
+ }
+
+ async run(req, res, db, user) {
+ try {
+ const tags = await db.table('tags')
+ .where('userId', user.id);
+
+ for (const tag of tags) {
+ const files = await db.table('fileTags')
+ .where({ tagId: tag.id });
+
+ tag.count = files.length ? files.length : 0;
+ }
+
+ return res.json({
+ message: 'Successfully retrieved tags',
+ tags
+ });
+ } catch (error) {
+ return super.error(res, error);
+ }
+ }
+}
+
+module.exports = tagsGET;