blob: 78e461bda703f451bd704d60482a0fa984329dd1 (
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
37
38
39
|
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()
.wasMutated();
} 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;
|