magneticow: most recent torrents now works correctly

This commit is contained in:
Bora Alper 2018-07-01 17:30:06 +03:00
parent cab54c6ec1
commit 80881c42c3
2 changed files with 35 additions and 17 deletions

View File

@ -49,10 +49,20 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) {
*tq.Ascending = true *tq.Ascending = true
} }
orderBy, err := parseOrderBy(tq.OrderBy) var orderBy persistence.OrderingCriteria
if err != nil { if tq.OrderBy == nil {
respondError(w, 400, err.Error()) if *tq.Query == "" {
return orderBy = persistence.ByDiscoveredOn
} else {
orderBy = persistence.ByRelevance
}
} else {
var err error
orderBy, err = parseOrderBy(*tq.OrderBy)
if err != nil {
respondError(w, 400, err.Error())
return
}
} }
torrents, err := database.QueryTorrents( torrents, err := database.QueryTorrents(
@ -87,12 +97,8 @@ func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) {
} }
func parseOrderBy(s *string) (persistence.OrderingCriteria, error) { func parseOrderBy(s string) (persistence.OrderingCriteria, error) {
if s == nil { switch s {
return persistence.ByRelevance, nil
}
switch *s {
case "TOTAL_SIZE": case "TOTAL_SIZE":
return persistence.ByTotalSize, nil return persistence.ByTotalSize, nil

View File

@ -17,10 +17,20 @@ window.onload = function() {
title.textContent = query + " - magneticow"; title.textContent = query + " - magneticow";
const input = document.getElementsByTagName("input")[0]; const input = document.getElementsByTagName("input")[0];
input.setAttribute("value", query); input.setAttribute("value", query);
}
else setOrderBy("RELEVANCE");
} else {
title.textContent = "Most recent torrents - magneticow"; title.textContent = "Most recent torrents - magneticow";
ascending = false;
setOrderBy("DISCOVERED_ON");
}
if (query) {
const feedAnchor = document.getElementById("feed-anchor");
feedAnchor.setAttribute("href", "/feed?query=" + encodeURIComponent(query));
}
load(); load();
}; };
@ -63,7 +73,9 @@ function load() {
query : query, query : query,
epoch : epoch, epoch : epoch,
lastID : lastID, lastID : lastID,
lastOrderedValue: lastOrderedValue lastOrderedValue: lastOrderedValue,
orderBy : orderBy,
ascending : ascending
}); });
console.log("reqURL", reqURL); console.log("reqURL", reqURL);
@ -86,6 +98,10 @@ function load() {
return; return;
} }
const last = torrents[torrents.length - 1];
lastID = last.id;
lastOrderedValue = orderedValue(last);
for (let t of torrents) { for (let t of torrents) {
t.size = fileSize(t.size); t.size = fileSize(t.size);
t.discoveredOn = (new Date(t.discoveredOn * 1000)).toLocaleDateString("en-GB", { t.discoveredOn = (new Date(t.discoveredOn * 1000)).toLocaleDateString("en-GB", {
@ -96,10 +112,6 @@ function load() {
tbody.innerHTML += Mustache.render(template, t); tbody.innerHTML += Mustache.render(template, t);
} }
const last = torrents[torrents.length - 1];
lastID = last.id;
lastOrderedValue = orderedValue(last);
}; };
req.open("GET", reqURL); req.open("GET", reqURL);