aboutsummaryrefslogtreecommitdiff
path: root/controllers/albumsController.js
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/albumsController.js')
-rw-r--r--controllers/albumsController.js50
1 files changed, 39 insertions, 11 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