albumsController.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const config = require('../config.js')
  2. const db = require('knex')(config.database)
  3. let albumsController = {}
  4. albumsController.list = function(req, res, next){
  5. let token = req.headers.token
  6. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  7. db.table('users').where('token', token).then((user) => {
  8. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  9. let fields = ['id', 'name']
  10. if(req.params.sidebar === undefined)
  11. fields.push('timestamp')
  12. db.table('albums').select(fields).where({enabled: 1, userid: user[0].id}).then((albums) => {
  13. if(req.params.sidebar !== undefined)
  14. return res.json({ success: true, albums })
  15. let ids = []
  16. for(let album of albums){
  17. album.date = new Date(album.timestamp * 1000)
  18. 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()
  19. ids.push(album.id)
  20. }
  21. db.table('files').whereIn('albumid', ids).select('albumid').then((files) => {
  22. let albumsCount = {}
  23. for(let id of ids) albumsCount[id] = 0
  24. for(let file of files) albumsCount[file.albumid] += 1
  25. for(let album of albums) album.files = albumsCount[album.id]
  26. return res.json({ success: true, albums })
  27. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  28. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  29. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  30. }
  31. albumsController.create = function(req, res, next){
  32. let token = req.headers.token
  33. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  34. db.table('users').where('token', token).then((user) => {
  35. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  36. let name = req.body.name
  37. if(name === undefined || name === '')
  38. return res.json({ success: false, description: 'No album name specified' })
  39. db.table('albums').where({
  40. name: name,
  41. enabled: 1,
  42. userid: user[0].id
  43. }).then((album) => {
  44. if(album.length !== 0) return res.json({ success: false, description: 'There\'s already an album with that name' })
  45. db.table('albums').insert({
  46. name: name,
  47. enabled: 1,
  48. userid: user[0].id,
  49. timestamp: Math.floor(Date.now() / 1000)
  50. }).then(() => {
  51. return res.json({ success: true })
  52. })
  53. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  54. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  55. }
  56. albumsController.delete = function(req, res, next){
  57. let token = req.headers.token
  58. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  59. db.table('users').where('token', token).then((user) => {
  60. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  61. let id = req.body.id
  62. if(id === undefined || id === '')
  63. return res.json({ success: false, description: 'No album specified' })
  64. db.table('albums').where({id: id, userid: user[0].id}).update({ enabled: 0 }).then(() => {
  65. return res.json({ success: true })
  66. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  67. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  68. }
  69. albumsController.rename = function(req, res, next){
  70. let token = req.headers.token
  71. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  72. db.table('users').where('token', token).then((user) => {
  73. if(user.length === 0) return res.status(401).json({ success: false, description: 'Invalid token'})
  74. let id = req.body.id
  75. if(id === undefined || id === '')
  76. return res.json({ success: false, description: 'No album specified' })
  77. let name = req.body.name
  78. if(name === undefined || name === '')
  79. return res.json({ success: false, description: 'No name specified' })
  80. db.table('albums').where({name: name, userid: user[0].id}).then((results) => {
  81. if(results.length !== 0) return res.json({ success: false, description: 'Name already in use' })
  82. db.table('albums').where({id: id, userid: user[0].id}).update({ name: name }).then(() => {
  83. return res.json({ success: true })
  84. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  85. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  86. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  87. }
  88. module.exports = albumsController