diff options
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/albumsController.js | 50 | ||||
| -rw-r--r-- | controllers/uploadController.js | 2 |
2 files changed, 40 insertions, 12 deletions
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 diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 120a52b..cb97eaa 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -46,7 +46,7 @@ uploadsController.upload = function(req, res, next){ size: file.size, ip: req.ip, albumid: album, - created_at: Math.floor(Date.now() / 1000) + timestamp: Math.floor(Date.now() / 1000) }) }) |