blob: 12b88faa8d9c23bbf7c81dfe72f65b7adcf7ba72 (
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');
const config = require('../../../../config');
const db = require('knex')(config.server.database);
const moment = require('moment');
class albumPOST extends Route {
constructor() {
super('/album/new', 'post');
}
async run(req, res, user) {
if (!req.body) return res.status(400).json({ message: 'No body provided' });
const { name } = req.body;
if (!name) return res.status(400).json({ message: 'No name provided' });
/*
Check that an album with that name doesn't exist yet
*/
const album = await db.table('albums').where({ name, userId: user.id }).first();
if (album) return res.status(401).json({ message: 'There\'s already an album with that name' });
const now = moment.utc().toDate();
await db.table('albums').insert({
name,
userId: user.id,
createdAt: now,
editedAt: now
});
return res.json({ message: 'The album was created successfully' });
}
}
module.exports = albumPOST;
|