aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/albumEditPOST.js
blob: 1022bbd74ac074bf176356b661dd0a2ad743ca97 (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 albumEditPOST extends Route {
	constructor() {
		super('/album/edit', 'post');
	}

	async run(req, res, db, user) {
		if (!req.body) return res.status(400).json({ message: 'No body provided' });
		const { id, name, nsfw } = req.body;
		if (!id) return res.status(400).json({ message: 'Invalid album identifier supplied' });


		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 {
			const updateObj = {
				name: name || album.name,
				nsfw: nsfw === true ? true : nsfw === false ? false : album.nsfw
			};
			await db
				.table('albums')
				.where({ id })
				.update(updateObj);
			return res.json({ message: 'Editing the album was successful', data: updateObj });
		} catch (error) {
			return super.error(res, error);
		}
	}
}

module.exports = albumEditPOST;