From e20cdca890299770af2d3474078315160e5197b8 Mon Sep 17 00:00:00 2001 From: Bora Alper Date: Fri, 29 Jun 2018 20:08:00 +0300 Subject: [PATCH] magneticow: orderBy now works at API endpoint, no web interface yet --- cmd/magneticow/api.go | 38 +++++++++++++++++++++++++++++++++++- cmd/magneticow/main.go | 33 +++---------------------------- pkg/persistence/interface.go | 10 ++++++---- pkg/persistence/sqlite3.go | 6 +++--- 4 files changed, 49 insertions(+), 38 deletions(-) diff --git a/cmd/magneticow/api.go b/cmd/magneticow/api.go index 538ca10..442e3f5 100644 --- a/cmd/magneticow/api.go +++ b/cmd/magneticow/api.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "net/http" "time" @@ -48,8 +49,14 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) { *tq.Ascending = true } + orderBy, err := parseOrderBy(tq.OrderBy) + if err != nil { + respondError(w, 400, err.Error()) + return + } + torrents, err := database.QueryTorrents( - *tq.Query, *tq.Epoch, persistence.ByRelevance, + *tq.Query, *tq.Epoch, orderBy, *tq.Ascending, N_TORRENTS, tq.LastOrderedValue, tq.LastID) if err != nil { respondError(w, 400, "query error: %s", err.Error()) @@ -79,3 +86,32 @@ func apiFilesInfohashHandler(w http.ResponseWriter, r *http.Request) { func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) { } + +func parseOrderBy(s *string) (persistence.OrderingCriteria, error) { + if s == nil { + return persistence.ByRelevance, nil + } + + switch *s { + case "TOTAL_SIZE": + return persistence.ByTotalSize, nil + + case "DISCOVERED_ON": + return persistence.ByDiscoveredOn, nil + + case "N_FILES": + return persistence.ByNFiles, nil + + case "UPDATED_ON": + return persistence.ByUpdatedOn, nil + + case "N_SEEDERS": + return persistence.ByNSeeders, nil + + case "N_LEECHERS": + return persistence.ByNLeechers, nil + + default: + return persistence.ByDiscoveredOn, fmt.Errorf("unknown orderBy string: %s", s) + } +} \ No newline at end of file diff --git a/cmd/magneticow/main.go b/cmd/magneticow/main.go index 12fdc7a..653941f 100644 --- a/cmd/magneticow/main.go +++ b/cmd/magneticow/main.go @@ -38,33 +38,6 @@ type TorrentsQ struct { LastID *uint64 `schema:"lastID"` } -// ========= TD: TemplateData ========= -type HomepageTD struct { - NTorrents uint -} - -type TorrentsTD struct { - CanLoadMore bool - Query string - SubscriptionURL string - Torrents []persistence.TorrentMetadata - SortedBy string - NextPageExists bool - Epoch int64 - LastOrderedValue float64 - LastID uint64 - -} - -type TorrentTD struct { -} - -type FeedTD struct { -} - -type StatisticsTD struct { -} - func main() { loggerLevel := zap.NewAtomicLevel() // Logging levels: ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal"). @@ -168,7 +141,9 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { panic(err.Error()) } - err = templates["homepage"].Execute(w, HomepageTD{ + err = templates["homepage"].Execute(w, struct { + NTorrents uint + }{ NTorrents: nTorrents, }) if err != nil { @@ -189,9 +164,7 @@ func torrentsHandler(w http.ResponseWriter, r *http.Request) { w.Write(data) } -// TODO: we might as well move torrent.html into static... func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { - // show torrents/{infohash} infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"]) if err != nil { panic(err.Error()) diff --git a/pkg/persistence/interface.go b/pkg/persistence/interface.go index 9772c53..48ea66c 100644 --- a/pkg/persistence/interface.go +++ b/pkg/persistence/interface.go @@ -28,7 +28,7 @@ type Database interface { QueryTorrents( query string, epoch int64, - orderBy orderingCriteria, + orderBy OrderingCriteria, ascending bool, limit uint, lastOrderedValue *float64, @@ -41,15 +41,17 @@ type Database interface { GetStatistics(n uint, to string) (*Statistics, error) } -type orderingCriteria uint8 +type OrderingCriteria uint8 const ( - ByRelevance orderingCriteria = iota - BySize + ByRelevance OrderingCriteria = iota + ByTotalSize ByDiscoveredOn ByNFiles ByNSeeders ByNLeechers + ByUpdatedOn ) +// TODO: search `swtich (orderBy)` and see if all cases are covered all the time type databaseEngine uint8 const ( diff --git a/pkg/persistence/sqlite3.go b/pkg/persistence/sqlite3.go index 6ebbcec..4d5927c 100644 --- a/pkg/persistence/sqlite3.go +++ b/pkg/persistence/sqlite3.go @@ -163,7 +163,7 @@ func (db *sqlite3Database) GetNumberOfTorrents() (uint, error) { func (db *sqlite3Database) QueryTorrents( query string, epoch int64, - orderBy orderingCriteria, + orderBy OrderingCriteria, ascending bool, limit uint, lastOrderedValue *float64, @@ -278,12 +278,12 @@ func (db *sqlite3Database) QueryTorrents( return torrents, nil } -func orderOn(orderBy orderingCriteria) string { +func orderOn(orderBy OrderingCriteria) string { switch orderBy { case ByRelevance: return "idx.rank" - case BySize: + case ByTotalSize: return "total_size" case ByDiscoveredOn: