magneticow: orderBy now works at API endpoint, no web interface yet

This commit is contained in:
Bora Alper 2018-06-29 20:08:00 +03:00
parent 3a45f17647
commit e20cdca890
4 changed files with 49 additions and 38 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"time" "time"
@ -48,8 +49,14 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
*tq.Ascending = true *tq.Ascending = true
} }
orderBy, err := parseOrderBy(tq.OrderBy)
if err != nil {
respondError(w, 400, err.Error())
return
}
torrents, err := database.QueryTorrents( torrents, err := database.QueryTorrents(
*tq.Query, *tq.Epoch, persistence.ByRelevance, *tq.Query, *tq.Epoch, orderBy,
*tq.Ascending, N_TORRENTS, tq.LastOrderedValue, tq.LastID) *tq.Ascending, N_TORRENTS, tq.LastOrderedValue, tq.LastID)
if err != nil { if err != nil {
respondError(w, 400, "query error: %s", err.Error()) 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 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)
}
}

View File

@ -38,33 +38,6 @@ type TorrentsQ struct {
LastID *uint64 `schema:"lastID"` 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() { func main() {
loggerLevel := zap.NewAtomicLevel() loggerLevel := zap.NewAtomicLevel()
// Logging levels: ("debug", "info", "warn", "error", "dpanic", "panic", and "fatal"). // 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()) panic(err.Error())
} }
err = templates["homepage"].Execute(w, HomepageTD{ err = templates["homepage"].Execute(w, struct {
NTorrents uint
}{
NTorrents: nTorrents, NTorrents: nTorrents,
}) })
if err != nil { if err != nil {
@ -189,9 +164,7 @@ func torrentsHandler(w http.ResponseWriter, r *http.Request) {
w.Write(data) w.Write(data)
} }
// TODO: we might as well move torrent.html into static...
func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) {
// show torrents/{infohash}
infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"]) infoHash, err := hex.DecodeString(mux.Vars(r)["infohash"])
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())

View File

@ -28,7 +28,7 @@ type Database interface {
QueryTorrents( QueryTorrents(
query string, query string,
epoch int64, epoch int64,
orderBy orderingCriteria, orderBy OrderingCriteria,
ascending bool, ascending bool,
limit uint, limit uint,
lastOrderedValue *float64, lastOrderedValue *float64,
@ -41,15 +41,17 @@ type Database interface {
GetStatistics(n uint, to string) (*Statistics, error) GetStatistics(n uint, to string) (*Statistics, error)
} }
type orderingCriteria uint8 type OrderingCriteria uint8
const ( const (
ByRelevance orderingCriteria = iota ByRelevance OrderingCriteria = iota
BySize ByTotalSize
ByDiscoveredOn ByDiscoveredOn
ByNFiles ByNFiles
ByNSeeders ByNSeeders
ByNLeechers ByNLeechers
ByUpdatedOn
) )
// TODO: search `swtich (orderBy)` and see if all cases are covered all the time
type databaseEngine uint8 type databaseEngine uint8
const ( const (

View File

@ -163,7 +163,7 @@ func (db *sqlite3Database) GetNumberOfTorrents() (uint, error) {
func (db *sqlite3Database) QueryTorrents( func (db *sqlite3Database) QueryTorrents(
query string, query string,
epoch int64, epoch int64,
orderBy orderingCriteria, orderBy OrderingCriteria,
ascending bool, ascending bool,
limit uint, limit uint,
lastOrderedValue *float64, lastOrderedValue *float64,
@ -278,12 +278,12 @@ func (db *sqlite3Database) QueryTorrents(
return torrents, nil return torrents, nil
} }
func orderOn(orderBy orderingCriteria) string { func orderOn(orderBy OrderingCriteria) string {
switch orderBy { switch orderBy {
case ByRelevance: case ByRelevance:
return "idx.rank" return "idx.rank"
case BySize: case ByTotalSize:
return "total_size" return "total_size"
case ByDiscoveredOn: case ByDiscoveredOn: