aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorPitu <[email protected]>2020-06-25 02:06:00 +0900
committerPitu <[email protected]>2020-06-25 02:06:00 +0900
commitd1340c26b5f72a45fb577ad7879addcb548dcf12 (patch)
tree932f5b13b5d5cdaf4dffaf6e06c2887257ead480 /src/api
parentCleanup (diff)
downloadhost.fuwn.me-d1340c26b5f72a45fb577ad7879addcb548dcf12.tar.xz
host.fuwn.me-d1340c26b5f72a45fb577ad7879addcb548dcf12.zip
Optimize album view
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/albums/albumGET.js32
1 files changed, 10 insertions, 22 deletions
diff --git a/src/api/routes/albums/albumGET.js b/src/api/routes/albums/albumGET.js
index fae7dd1..1bf3630 100644
--- a/src/api/routes/albums/albumGET.js
+++ b/src/api/routes/albums/albumGET.js
@@ -10,38 +10,26 @@ class albumGET extends Route {
const { identifier } = req.params;
if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' });
- /*
- Make sure it exists and it's enabled
- */
+ // Make sure it exists and it's enabled
const link = await db.table('links').where({ identifier, enabled: true }).first();
if (!link) return res.status(404).json({ message: 'The album could not be found' });
- /*
- Same with the album, just to make sure is not a deleted album and a leftover link
- */
+ // Same with the album, just to make sure is not a deleted album and a leftover link
const album = await db.table('albums').where('id', link.albumId).first();
if (!album) return res.status(404).json({ message: 'Album not found' });
- /*
- Grab the files in a very unoptimized way. (This should be a join between both tables)
- */
- const fileList = await db.table('albumsFiles').where('albumId', link.albumId).select('fileId');
- const fileIds = fileList.map(el => el.fileId);
- const files = await db.table('files')
- .whereIn('id', fileIds)
- .orderBy('id', 'desc')
- .select('name');
-
- /*
- Create the links for each file
- */
+ const files = await db.table('albumsFiles')
+ .where({ albumId: link.albumId })
+ .join('files', 'albumsFiles.fileId', 'files.id')
+ .select('files.name')
+ .orderBy('files.id', 'desc');
+
+ // Create the links for each file
for (let file of files) {
file = Util.constructFilePublicLink(file);
}
- /*
- Add 1 more view to the link
- */
+ // Add 1 more view to the link
await db.table('links').where({ identifier }).update('views', Number(link.views) + 1);
return res.json({