From 84ff2241ba81fc6a1a2201074d30f971dad8a0de Mon Sep 17 00:00:00 2001 From: Pitu Date: Wed, 18 Jan 2017 02:40:14 -0300 Subject: Shit ton of things on this update --- controllers/albumsController.js | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'controllers/albumsController.js') diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 7639b3d..1f2d695 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -8,25 +8,53 @@ albumsController.list = function(req, res, next){ if(req.headers.auth !== config.adminToken) return res.status(401).send('not-authorized') - db.table('albums').select('id', 'name').then((albums) => { - return res.json({ albums }) + let fields = ['id', 'name'] + + if(req.headers.extended !== undefined) + fields.push('timestamp') + + db.table('albums').select(fields).then((albums) => { + + if(req.headers.extended === undefined) + return res.json({ success: true, albums }) + + let ids = [] + for(let album of albums) + ids.push(album.id) + + db.table('files').whereIn('albumid', ids).select('albumid').then((files) => { + + let albumsCount = {} + + for(let id of ids) albumsCount[id] = 0 + for(let file of files) albumsCount[file.albumid] += 1 + for(let album of albums) album.files = albumsCount[album.id] + + return res.json({ success: true, albums }) + }) }) } -albumsController.test = function(req, res, next){ +albumsController.create = function(req, res, next){ if(req.headers.auth !== config.adminToken) return res.status(401).send('not-authorized') - let testdata = [ - {name: 'Test 1'}, - {name: 'Test 2'}, - {name: 'Test 3'}, - {name: 'Test 4'}, - {name: 'Test 5'} - ] + let name = req.headers.name + if(name === undefined || name === '') + return res.json({ success: false, description: 'No album name specified' }) + + db.table('albums').where('name', name).then((album) => { + if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' }) - db.table('albums').insert(testdata).then(() => {}) + db.table('albums').insert({ + name: name, + timestamp: Math.floor(Date.now() / 1000) + }).then(() => { + return res.json({ success: true }) + }) + }) } + module.exports = albumsController \ No newline at end of file -- cgit v1.2.3