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