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

class albumAddPOST extends Route {
	constructor() {
		super('/file/album/add', 'post', { canApiKey: true });
	}

	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')
				.insert({ fileId, albumId }).wasMutated();
		} catch (error) {
			return super.error(res, error);
		}

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

module.exports = albumAddPOST;