Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(web): improve the clarity of the torrentstotal semantics #540

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func makeRouter() *http.ServeMux {

router.HandleFunc("/api/v0.1/statistics", BasicAuth(apiStatistics))
router.HandleFunc("/api/v0.1/torrents", BasicAuth(apiTorrents))
router.HandleFunc("/api/v0.1/torrentstotal", BasicAuth(apiTorrentsTotal))
router.HandleFunc("/api/v0.1/torrents/search/count", BasicAuth(apiTorrentsCountByKeyword))
router.HandleFunc("/api/v0.1/torrents/{infohash}", BasicAuth(infohashMiddleware(apiTorrent)))
router.HandleFunc("/api/v0.1/torrents/{infohash}/filelist", BasicAuth(infohashMiddleware(apiFileList)))

Expand Down
6 changes: 4 additions & 2 deletions web/torrents.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func apiTorrents(w http.ResponseWriter, r *http.Request) {
}
}

func apiTorrentsTotal(w http.ResponseWriter, r *http.Request) {
func apiTorrentsCountByKeyword(w http.ResponseWriter, r *http.Request) {
// @lastOrderedValue AND @lastID are either both supplied or neither of them should be supplied
// at all; and if that is NOT the case, then return an error.
if q := r.URL.Query(); !((q.Get("lastOrderedValue") != "" && q.Get("lastID") != "") ||
Expand Down Expand Up @@ -237,8 +237,10 @@ func apiTorrentsTotal(w http.ResponseWriter, r *http.Request) {
return
}

countTorrentsByKeyword := map[string]uint64{"countTorrentsByKeyword": torrentsTotal}

w.Header().Set(ContentType, ContentTypeJson)
if err = json.NewEncoder(w).Encode(torrentsTotal); err != nil {
if err = json.NewEncoder(w).Encode(countTorrentsByKeyword); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
Expand Down
6 changes: 3 additions & 3 deletions web/torrents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestParseOrderBy(t *testing.T) {
}
}

func TestApiTorrentsTotal(t *testing.T) {
func TestApiTorrentsCountByKeyword(t *testing.T) {
t.Parallel()

initDb()
Expand Down Expand Up @@ -204,13 +204,13 @@ func TestApiTorrentsTotal(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest("GET", "/api/torrents/total?"+tt.queryParams, nil)
req, err := http.NewRequest("GET", "/api/torrents/search/count?"+tt.queryParams, nil)
if err != nil {
t.Fatalf("could not create request: %v", err)
}

rec := httptest.NewRecorder()
handler := http.HandlerFunc(apiTorrentsTotal)
handler := http.HandlerFunc(apiTorrentsCountByKeyword)
handler.ServeHTTP(rec, req)

res := rec.Result()
Expand Down
Loading