aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/files/albumDelPOST.js
blob: 63da791aad18c419e3ba3f92cf76659e5d4ec0de (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
const Route = require('../../structures/Route');

class albumDelPOST extends Route {
	constructor() {
		super('/file/album/del', 'post');
	}

	async run(req, res, db, user) {
		if (!req.body) return res.status(400).json({ message: 'No body provided' });
		const { fileId, albumId } = req.body;
		if (!fileId || !albumId) return res.status(400).json({ message: 'No id provided' });

		// Make sure both file and album belong 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 album = await db.table('albums').where({ id: albumId, userId: user.id }).first();
		if (!album) return res.status(400).json({ message: 'Album doesn\'t exist.' });

		try {
			await db.table('albumsFiles')
				.where({ fileId, albumId })
				.delete()
				.wasMutated();
		} catch (error) {
			return super.error(res, error);
		}

		return res.json({
			message: 'Successfully removed file from album',
			data: { fileId, album: { id: album.id, name: album.name } }
		});
	}
}

module.exports = albumDelPOST;