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

class filesGET extends Route {
	constructor() {
		super('/file/:id/albums', 'get');
	}

	async run(req, res, db, user) {
		const { id } = req.params;
		if (!id) return res.status(400).json({ message: 'Invalid file ID supplied' });

		const file = await db.table('files').where({ id, userId: user.id }).first();
		if (!file) return res.status(400).json({ message: 'The file doesn\'t exist or doesn\'t belong to the user' });

		let albums = [];
		let albumFiles = await db.table('albumsFiles')
			.where('fileId', id)
			.select('albumId');

		if (albumFiles.length) {
			albumFiles = albumFiles.map(a => a.albumId);
			albums = await db.table('albums')
				.whereIn('id', albumFiles)
				.select('id', 'name');
		}

		return res.json({
			message: 'Successfully retrieved file albums',
			albums
		});
	}
}

module.exports = filesGET;