feat: backend pagination

serverLoad++;
userRamUsage--;
This commit is contained in:
Zephyrrus 2020-06-29 22:18:42 +03:00
parent c8ad345d12
commit 520062508c
5 changed files with 34 additions and 18 deletions

View File

@ -7,10 +7,23 @@ class filesGET extends Route {
} }
async run(req, res, db, user) { async run(req, res, db, user) {
// Get all the files from the user let count = 0;
const files = await db.table('files')
let files = db.table('files')
.where('userId', user.id) .where('userId', user.id)
.orderBy('id', 'desc'); .orderBy('createdAt', 'desc');
const { page, limit = 100 } = req.query;
if (page && page >= 0) {
files = await files.offset((page - 1) * limit).limit(limit);
count = (await db.table('files')
.count('id as count')
.where('userId', user.id)
.first()).count;
} else {
count = files.length;
}
// For each file, create the public link to be able to display the file // For each file, create the public link to be able to display the file
for (let file of files) { for (let file of files) {
@ -19,7 +32,8 @@ class filesGET extends Route {
return res.json({ return res.json({
message: 'Successfully retrieved files', message: 'Successfully retrieved files',
files files,
count
}); });
} }
} }

View File

@ -122,7 +122,7 @@ class Util {
/* /*
It's funny but if you do i++ the asignment never gets done resulting in an infinite loop It's funny but if you do i++ the asignment never gets done resulting in an infinite loop
*/ */
if (i < 5) return retry(i + 1); if (i < 5) return retry(++i);
log.error('Couldnt allocate identifier for album'); log.error('Couldnt allocate identifier for album');
return null; return null;
}; };

View File

@ -17,7 +17,8 @@
</div> </div>
<template v-if="!showList"> <template v-if="!showList">
<Waterfall :gutterWidth="10" <Waterfall v-if="showWaterfall"
:gutterWidth="10"
:gutterHeight="4"> :gutterHeight="4">
<!-- <!--
TODO: Implement search based on originalName, albumName and tags TODO: Implement search based on originalName, albumName and tags
@ -32,7 +33,6 @@
<!-- TODO: Implement pagination --> <!-- TODO: Implement pagination -->
<WaterfallItem v-for="(item, index) in gridFiles" <WaterfallItem v-for="(item, index) in gridFiles"
v-if="showWaterfall"
:key="index" :key="index"
:width="width" :width="width"
move-class="item-move"> move-class="item-move">

View File

@ -8,15 +8,16 @@
<div class="column"> <div class="column">
<h2 class="subtitle">Your uploaded files</h2> <h2 class="subtitle">Your uploaded files</h2>
<hr> <hr>
<!-- TODO: Add a list view so the user can see the files that don't have thumbnails, like text documents --> <!-- TODO: Add a list view so the user can see the files that don't have thumbnails, like text documents -->
<Grid v-if="files.length" <Grid v-if="count"
:files="paginatedFiles" :files="files"
:enableSearch="false" :enableSearch="false"
class="grid" /> class="grid" />
<b-pagination <b-pagination
v-if="files.length > perPage" v-if="count > perPage"
:total="files.length" :total="count"
:per-page="perPage" :per-page="perPage"
:current.sync="current" :current.sync="current"
class="pagination" class="pagination"
@ -46,25 +47,26 @@ export default {
data() { data() {
return { return {
files: [], files: [],
count: 0,
current: 1, current: 1,
perPage: 20 perPage: 20
}; };
}, },
computed: {
paginatedFiles() {
return this.files.slice((this.current - 1) * this.perPage, this.current * this.perPage);
}
},
metaInfo() { metaInfo() {
return { title: 'Uploads' }; return { title: 'Uploads' };
}, },
watch: {
current: 'getFiles'
},
mounted() { mounted() {
this.getFiles(); this.getFiles();
}, },
methods: { methods: {
async getFiles() { async getFiles() {
const response = await this.$axios.$get(`files`); // TODO: Cache a few pages once fetched
const response = await this.$axios.$get(`files`, { params: { page: this.current, limit: this.perPage }});
this.files = response.files; this.files = response.files;
this.count = response.count;
} }
} }
}; };

View File

@ -1,5 +1,5 @@
<template> <template>
<div> <div class="section">
<div class="container"> <div class="container">
<div class="columns"> <div class="columns">
<div class="column is-3 is-offset-2"> <div class="column is-3 is-offset-2">