aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/albumDELETE.js
blob: 8fd79caf6abb91ae44a06ad3f71a43f66ccbc7f2 (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 albumDELETE extends Route {
	constructor() {
		super('/album/:id', 'delete');
	}

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

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

		try {
			// Delete the album
			await db.table('albums').where({ id }).delete();

			// Delete the relation of any files attached to this album
			await db.table('albumsFiles').where({ albumId: id }).delete();

			// Delete the relation of any links attached to this album
			await db.table('albumsLinks').where({ albumId: id }).delete();

			// Delete any album links created for this album
			await db.table('links').where({ albumId: id }).delete()
				.wasMutated();

			return res.json({ message: 'The album was deleted successfully' });
		} catch (error) {
			return super.error(res, error);
		}
	}
}

module.exports = albumDELETE;