aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/files/tagAddPOST.js
blob: 0a0ab425faf064f6d7191311960301c6a532f926 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const Route = require('../../structures/Route');

class tagAddPOST extends Route {
	constructor() {
		super('/file/tag/add', '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').insert({ fileId, tagId: tag.id }).wasMutated();
		} catch (error) {
			return super.error(res, error);
		}

		return res.json({
			message: 'Successfully added tag to file',
			data: { fileId, tag }
		});
		// eslint-disable-next-line consistent-return
	}
}

module.exports = tagAddPOST;