v3.0.0/src/api/routes/albums/albumsGET.js

72 lines
1.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable max-classes-per-file */
2018-09-16 05:56:13 +02:00
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');
class albumsGET extends Route {
constructor() {
super('/albums/mini', 'get');
}
2019-02-19 15:52:24 +01:00
async run(req, res, db, user) {
2018-09-16 05:56:13 +02:00
/*
Let's fetch the albums. This route will only return a small portion
of the album files for displaying on the dashboard. It's probably useless
2020-12-25 12:45:22 +01:00
for anyone consuming the API outside of the chibisafe frontend.
2018-09-16 05:56:13 +02:00
*/
const albums = await db
.table('albums')
.where('albums.userId', user.id)
.select('id', 'name', 'createdAt', 'editedAt')
.orderBy('createdAt', 'desc');
2018-09-16 05:56:13 +02:00
for (const album of albums) {
// Fetch the total amount of files each album has.
const fileCount = await db // eslint-disable-line no-await-in-loop
.table('albumsFiles')
2018-09-16 05:56:13 +02:00
.where('albumId', album.id)
.count({ count: 'id' });
// Fetch the file list from each album but limit it to 5 per album
const files = await db // eslint-disable-line no-await-in-loop
.table('albumsFiles')
.join('files', { 'files.id': 'albumsFiles.fileId' })
2018-09-16 05:56:13 +02:00
.where('albumId', album.id)
.select('files.id', 'files.name')
.orderBy('albumsFiles.id', 'desc')
2018-09-16 05:56:13 +02:00
.limit(5);
// Fetch thumbnails and stuff
2018-09-16 05:56:13 +02:00
for (let file of files) {
file = Util.constructFilePublicLink(file);
}
album.fileCount = fileCount[0].count;
album.files = files;
}
return res.json({
message: 'Successfully retrieved albums',
2020-12-24 09:40:50 +01:00
albums
2018-09-16 05:56:13 +02:00
});
}
}
class albumsDropdownGET extends Route {
constructor() {
super('/albums/dropdown', 'get', { canApiKey: true });
2018-09-16 05:56:13 +02:00
}
2019-02-19 15:52:24 +01:00
async run(req, res, db, user) {
const albums = await db
.table('albums')
2018-09-16 05:56:13 +02:00
.where('userId', user.id)
.select('id', 'name');
return res.json({
message: 'Successfully retrieved albums',
2020-12-24 09:40:50 +01:00
albums
2018-09-16 05:56:13 +02:00
});
}
}
module.exports = [albumsGET, albumsDropdownGET];