2019-03-02 14:07:24 +01:00
|
|
|
const Route = require('../../structures/Route');
|
|
|
|
|
|
|
|
class albumAddPOST extends Route {
|
|
|
|
constructor() {
|
2021-11-03 14:17:34 +01:00
|
|
|
super('/file/album/add', 'post', { canApiKey: true });
|
2019-03-02 14:07:24 +01:00
|
|
|
}
|
|
|
|
|
2019-10-12 14:14:19 +02:00
|
|
|
async run(req, res, db, user) {
|
2019-03-02 14:07:24 +01:00
|
|
|
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' });
|
|
|
|
|
2019-10-12 14:14:19 +02:00
|
|
|
// 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.' });
|
|
|
|
|
2019-03-02 14:07:24 +01:00
|
|
|
try {
|
|
|
|
await db.table('albumsFiles')
|
2021-01-07 20:36:56 +01:00
|
|
|
.insert({ fileId, albumId }).wasMutated();
|
2019-03-02 14:07:24 +01:00
|
|
|
} catch (error) {
|
|
|
|
return super.error(res, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({
|
2020-07-08 02:37:50 +02:00
|
|
|
message: 'Successfully added file to album',
|
2020-12-24 09:40:50 +01:00
|
|
|
data: { fileId, album: { id: album.id, name: album.name } }
|
2019-03-02 14:07:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = albumAddPOST;
|