From 26c09e7b1bfff8951fcc0b9d0597cc37d4fb05b0 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 7 May 2017 21:04:21 +0200 Subject: [PATCH] Fix NULL problems when sorting by size, fixes #151 --- service/torrent/torrent.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 89d4b049..d977ce94 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -56,7 +56,13 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs var torrents []model.Torrents var dbQuery *gorm.DB var count int - conditions := "torrent_hash is not null" //filter out broken entries + conditions := "torrent_hash IS NOT NULL" // filter out broken entries + if strings.HasPrefix(orderBy, "filesize") { + // torrents w/ NULL filesize fuck up the sorting on postgres + // TODO: fix this properly + conditions += " AND filesize IS NOT NULL" + } + var params []interface{} if parameters != nil { // if there is where parameters conditions += " AND " + parameters.Conditions @@ -65,9 +71,9 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs db.ORM.Model(&torrents).Where(conditions, params...).Count(&count) dbQuery = db.ORM.Model(&torrents).Where(conditions, params...) - if orderBy == "" { + if orderBy == "" { // default OrderBy orderBy = "torrent_id DESC" - } // Default OrderBy + } if limit != 0 || offset != 0 { // if limits provided dbQuery = dbQuery.Limit(limit).Offset(offset) }