Fix limit when fetching entries, allow moment timestamps

This commit is contained in:
Bryan Ashby 2018-01-11 21:17:26 -07:00
parent 4e4ee6b8ce
commit fa1bffeaf8
1 changed files with 14 additions and 7 deletions

View File

@ -13,10 +13,11 @@ const paths = require('path');
const fse = require('fs-extra');
const { unlink, readFile } = require('graceful-fs');
const crypto = require('crypto');
const moment = require('moment');
const FILE_TABLE_MEMBERS = [
'file_id', 'area_tag', 'file_sha256', 'file_name', 'storage_tag',
'desc', 'desc_long', 'upload_timestamp'
'desc', 'desc_long', 'upload_timestamp'
];
const FILE_WELL_KNOWN_META = {
@ -424,7 +425,11 @@ module.exports = class FileEntry {
let sqlWhere = '';
let sqlOrderBy;
const sqlOrderDir = 'ascending' === filter.order ? 'ASC' : 'DESC';
if(moment.isMoment(filter.newerThanTimestamp)) {
filter.newerThanTimestamp = getISOTimestampString(filter.newerThanTimestamp);
}
function getOrderByWithCast(ob) {
if( [ 'dl_count', 'est_release_year', 'byte_size' ].indexOf(filter.sort) > -1 ) {
return `ORDER BY CAST(${ob} AS INTEGER)`;
@ -444,7 +449,7 @@ module.exports = class FileEntry {
if(filter.sort && filter.sort.length > 0) {
if(Object.keys(FILE_WELL_KNOWN_META).indexOf(filter.sort) > -1) { // sorting via a meta value?
sql =
sql =
`SELECT DISTINCT f.file_id
FROM file f, file_meta m`;
@ -461,7 +466,7 @@ module.exports = class FileEntry {
WHERE file_id = f.file_id)
AS avg_rating
FROM file f`;
sqlOrderBy = `ORDER BY avg_rating ${sqlOrderDir}`;
} else {
sql =
@ -472,7 +477,7 @@ module.exports = class FileEntry {
}
}
} else {
sql =
sql =
`SELECT DISTINCT f.file_id
FROM file f`;
@ -552,12 +557,14 @@ module.exports = class FileEntry {
appendWhereClause(`f.file_id > ${filter.newerThanFileId}`);
}
sql += `${sqlWhere} ${sqlOrderBy};`;
sql += `${sqlWhere} ${sqlOrderBy}`;
if(_.isNumber(filter.limit)) {
sql += `LIMIT ${filter.limit}`;
sql += ` LIMIT ${filter.limit}`;
}
sql += ';';
const matchingFileIds = [];
fileDb.each(sql, (err, fileId) => {
if(fileId) {