2017-01-17 22:55:35 +01:00
|
|
|
const config = require('../config.js')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
|
|
|
|
let albumsController = {}
|
|
|
|
|
|
|
|
albumsController.list = function(req, res, next){
|
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let token = req.headers.token
|
|
|
|
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
|
2017-01-17 22:55:35 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('users').where('token', token).then((user) => {
|
|
|
|
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let fields = ['id', 'name']
|
|
|
|
|
|
|
|
if(req.params.sidebar === undefined)
|
|
|
|
fields.push('timestamp')
|
2017-01-18 07:29:46 +01:00
|
|
|
|
2017-01-30 08:42:15 +01:00
|
|
|
db.table('albums').select(fields).where({enabled: 1, userid: user[0].id}).then((albums) => {
|
2017-01-30 02:17:49 +01:00
|
|
|
|
|
|
|
if(req.params.sidebar !== undefined)
|
|
|
|
return res.json({ success: true, albums })
|
2017-01-18 09:07:20 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let ids = []
|
|
|
|
for(let album of albums){
|
|
|
|
album.date = new Date(album.timestamp * 1000)
|
|
|
|
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 07:29:46 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
ids.push(album.id)
|
|
|
|
}
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
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]
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
return res.json({ success: true, albums })
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-20 07:28:26 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-30 02:17:49 +01:00
|
|
|
|
2017-01-17 22:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 06:40:14 +01:00
|
|
|
albumsController.create = function(req, res, next){
|
2017-01-17 22:55:35 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let token = req.headers.token
|
|
|
|
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
|
2017-01-17 22:55:35 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('users').where('token', token).then((user) => {
|
|
|
|
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let name = req.body.name
|
|
|
|
if(name === undefined || name === '')
|
|
|
|
return res.json({ success: false, description: 'No album name specified' })
|
2017-01-17 22:55:35 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('albums').where({
|
|
|
|
name: name,
|
2017-01-18 09:07:20 +01:00
|
|
|
enabled: 1,
|
2017-01-30 08:42:15 +01:00
|
|
|
userid: user[0].id
|
2017-01-30 02:17:49 +01:00
|
|
|
}).then((album) => {
|
|
|
|
if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
|
|
|
|
|
|
|
|
db.table('albums').insert({
|
|
|
|
name: name,
|
|
|
|
enabled: 1,
|
2017-01-30 08:42:15 +01:00
|
|
|
userid: user[0].id,
|
2017-01-30 02:17:49 +01:00
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
}).then(() => {
|
|
|
|
return res.json({ success: true })
|
|
|
|
})
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-20 07:28:26 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-30 02:17:49 +01:00
|
|
|
|
|
|
|
|
2017-01-17 22:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 21:14:28 +01:00
|
|
|
albumsController.delete = function(req, res, next){
|
2017-01-30 02:17:49 +01:00
|
|
|
let token = req.headers.token
|
|
|
|
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
|
2017-01-19 21:14:28 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('users').where('token', token).then((user) => {
|
|
|
|
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
2017-01-19 21:14:28 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let id = req.body.id
|
|
|
|
if(id === undefined || id === '')
|
|
|
|
return res.json({ success: false, description: 'No album specified' })
|
|
|
|
|
2017-01-30 08:42:15 +01:00
|
|
|
db.table('albums').where({id: id, userid: user[0].id}).update({ enabled: 0 }).then(() => {
|
2017-01-30 02:17:49 +01:00
|
|
|
return res.json({ success: true })
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-20 07:28:26 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-19 21:14:28 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 07:28:26 +01:00
|
|
|
albumsController.rename = function(req, res, next){
|
2017-01-30 02:17:49 +01:00
|
|
|
let token = req.headers.token
|
|
|
|
if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
|
2017-01-20 07:28:26 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
db.table('users').where('token', token).then((user) => {
|
|
|
|
if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
|
2017-01-20 07:28:26 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let id = req.body.id
|
|
|
|
if(id === undefined || id === '')
|
|
|
|
return res.json({ success: false, description: 'No album specified' })
|
2017-01-20 07:28:26 +01:00
|
|
|
|
2017-01-30 02:17:49 +01:00
|
|
|
let name = req.body.name
|
|
|
|
if(name === undefined || name === '')
|
|
|
|
return res.json({ success: false, description: 'No name specified' })
|
2017-01-20 07:28:26 +01:00
|
|
|
|
2017-01-30 08:42:15 +01:00
|
|
|
db.table('albums').where({name: name, userid: user[0].id}).then((results) => {
|
2017-01-30 02:17:49 +01:00
|
|
|
if(results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
|
|
|
|
|
2017-01-30 08:42:15 +01:00
|
|
|
db.table('albums').where({id: id, userid: user[0].id}).update({ name: name }).then(() => {
|
2017-01-30 02:17:49 +01:00
|
|
|
return res.json({ success: true })
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
2017-01-20 07:28:26 +01:00
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
|
|
|
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
|
|
|
|
|
|
|
|
}
|
2017-01-18 06:40:14 +01:00
|
|
|
|
2017-01-17 22:55:35 +01:00
|
|
|
module.exports = albumsController
|