magneticow: orderBy now works at API endpoint, no web interface yet
This commit is contained in:
parent
3a45f17647
commit
e20cdca890
@ -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)
|
||||
}
|
||||
}
|
@ -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())
|
||||
|
@ -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 (
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user