aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/link/linkDELETE.js
blob: 1af704ec65899032a0569d2514fe602f5a7eec0f (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 linkDELETE extends Route {
	constructor() {
		super('/album/link/delete/:identifier', 'delete');
	}

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

		try {
			const link = await db.table('links')
				.where({ identifier, userId: user.id })
				.first();

			if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist or doesnt\'t belong to the user' });

			await db.table('links')
				.where({ id: link.id })
				.delete();
			await db.table('albumsLinks')
				.where({ linkId: link.id })
				.delete();
		} catch (error) {
			return super.error(res, error);
		}

		return res.json({
			message: 'Successfully deleted link'
		});
	}
}

module.exports = linkDELETE;