From 30c3ae51da4f6b3ceeae901338c9f92cb455e674 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 24 Dec 2024 21:00:35 +0800 Subject: [PATCH] refactor(web): improve the clarity of the `torrentstotal` semantics --- web/router.go | 2 +- web/torrents.go | 6 ++++-- web/torrents_test.go | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/web/router.go b/web/router.go index 8bc15b25..9389b487 100644 --- a/web/router.go +++ b/web/router.go @@ -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))) diff --git a/web/torrents.go b/web/torrents.go index f478faf8..8b603e74 100644 --- a/web/torrents.go +++ b/web/torrents.go @@ -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") != "") || @@ -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) } } diff --git a/web/torrents_test.go b/web/torrents_test.go index 43d3133d..06291a03 100644 --- a/web/torrents_test.go +++ b/web/torrents_test.go @@ -155,7 +155,7 @@ func TestParseOrderBy(t *testing.T) { } } -func TestApiTorrentsTotal(t *testing.T) { +func TestApiTorrentsCountByKeyword(t *testing.T) { t.Parallel() initDb() @@ -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()