2017-01-17 21:55:35 +00:00
|
|
|
const config = require('../config.js')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
|
|
|
|
let albumsController = {}
|
|
|
|
|
|
|
|
albumsController.list = function(req, res, next){
|
|
|
|
|
|
|
|
if(req.headers.auth !== config.adminToken)
|
2017-01-19 05:37:35 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-17 21:55:35 +00:00
|
|
|
|
2017-01-18 05:40:14 +00:00
|
|
|
let fields = ['id', 'name']
|
|
|
|
|
2017-01-19 05:37:35 +00:00
|
|
|
if(req.params.sidebar === undefined)
|
2017-01-18 05:40:14 +00:00
|
|
|
fields.push('timestamp')
|
|
|
|
|
2017-01-18 08:07:20 +00:00
|
|
|
db.table('albums').select(fields).where('enabled', 1).then((albums) => {
|
2017-01-18 06:29:46 +00:00
|
|
|
|
2017-01-19 05:37:35 +00:00
|
|
|
if(req.params.sidebar !== undefined)
|
2017-01-18 07:05:56 +00:00
|
|
|
return res.json({ success: true, albums })
|
2017-01-18 08:07:20 +00:00
|
|
|
|
2017-01-18 06:29:46 +00:00
|
|
|
let ids = []
|
|
|
|
for(let album of albums){
|
|
|
|
album.date = new Date(album.timestamp * 1000)
|
2017-01-19 03:31:01 +00:00
|
|
|
album.date = album.date.getFullYear() + '-' + (album.date.getMonth() + 1) + '-' + album.date.getDate() + ' ' + (album.date.getHours() < 10 ? '0' : '') + album.date.getHours() + ':' + (album.date.getMinutes() < 10 ? '0' : '') + album.date.getMinutes() + ':' + (album.date.getSeconds() < 10 ? '0' : '') + album.date.getSeconds()
|
2017-01-18 06:29:46 +00:00
|
|
|
|
|
|
|
ids.push(album.id)
|
|
|
|
}
|
2017-01-18 05:40:14 +00:00
|
|
|
|
|
|
|
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 })
|
|
|
|
})
|
2017-01-17 21:55:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:40:14 +00:00
|
|
|
albumsController.create = function(req, res, next){
|
2017-01-17 21:55:35 +00:00
|
|
|
|
|
|
|
if(req.headers.auth !== config.adminToken)
|
2017-01-19 05:37:35 +00:00
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
2017-01-17 21:55:35 +00:00
|
|
|
|
2017-01-19 05:37:35 +00:00
|
|
|
let name = req.body.name
|
2017-01-18 05:40:14 +00:00
|
|
|
if(name === undefined || name === '')
|
|
|
|
return res.json({ success: false, description: 'No album name specified' })
|
|
|
|
|
2017-01-19 20:14:28 +00:00
|
|
|
db.table('albums').where('name', name).where('enabled', 1).then((album) => {
|
2017-01-18 05:40:14 +00:00
|
|
|
if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
|
2017-01-17 21:55:35 +00:00
|
|
|
|
2017-01-18 05:40:14 +00:00
|
|
|
db.table('albums').insert({
|
|
|
|
name: name,
|
2017-01-18 08:07:20 +00:00
|
|
|
enabled: 1,
|
2017-01-18 05:40:14 +00:00
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
}).then(() => {
|
|
|
|
return res.json({ success: true })
|
|
|
|
})
|
|
|
|
})
|
2017-01-17 21:55:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 20:14:28 +00:00
|
|
|
albumsController.delete = function(req, res, next){
|
|
|
|
if(req.headers.auth !== config.adminToken)
|
|
|
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
|
|
|
|
|
|
|
let id = req.body.id
|
|
|
|
if(id === undefined || id === '')
|
|
|
|
return res.json({ success: false, description: 'No album specified' })
|
|
|
|
|
|
|
|
db.table('albums').where('id', id).update({ enabled: 0 }).then(() => {
|
|
|
|
return res.json({ success: true })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:40:14 +00:00
|
|
|
|
2017-01-17 21:55:35 +00:00
|
|
|
module.exports = albumsController
|