refactor: shorten function names in jikan search
This commit is contained in:
@@ -24,7 +24,7 @@ func setQueryValue(values url.Values, key, value string) {
|
|||||||
values.Set(key, value)
|
values.Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setPositiveIntQueryValue(values url.Values, key string, value int) {
|
func setPositiveInt(values url.Values, key string, value int) {
|
||||||
if value <= 0 {
|
if value <= 0 {
|
||||||
values.Del(key)
|
values.Del(key)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func normalizeSearchPagination(page, limit int) (int, int) {
|
func normalizePage(page, limit int) (int, int) {
|
||||||
if page < 1 {
|
if page < 1 {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
@@ -32,31 +32,31 @@ func joinGenreIDs(genres []int) string {
|
|||||||
return strings.Join(ids, ",")
|
return strings.Join(ids, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildAdvancedSearchURL(baseURL, query, animeType, status, orderBy, sort, genres string, studioID int, sfw bool, page, limit int) string {
|
func advancedURL(baseURL, query, animeType, status, orderBy, sort, genres string, studioID int, sfw bool, page, limit int) string {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("page", strconv.Itoa(page))
|
params.Set("page", strconv.Itoa(page))
|
||||||
setTrueQueryValue(params, "sfw", sfw)
|
setTrueQueryValue(params, "sfw", sfw)
|
||||||
setQueryValue(params, "q", query)
|
setQueryValue(params, "q", query)
|
||||||
setQueryValue(params, "type", animeType)
|
setQueryValue(params, "type", animeType)
|
||||||
setQueryValue(params, "status", status)
|
setQueryValue(params, "status", status)
|
||||||
setPositiveIntQueryValue(params, "producers", studioID)
|
setPositiveInt(params, "producers", studioID)
|
||||||
setQueryValue(params, "order_by", orderBy)
|
setQueryValue(params, "order_by", orderBy)
|
||||||
setQueryValue(params, "sort", sort)
|
setQueryValue(params, "sort", sort)
|
||||||
setQueryValue(params, "genres", genres)
|
setQueryValue(params, "genres", genres)
|
||||||
setPositiveIntQueryValue(params, "limit", limit)
|
setPositiveInt(params, "limit", limit)
|
||||||
|
|
||||||
return buildRequestURL(baseURL, "/anime", params)
|
return buildRequestURL(baseURL, "/anime", params)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchAdvanced performs a filtered anime search with type, status, ordering, genre filters, and studio (producer) filters.
|
// SearchAdvanced performs a filtered anime search with type, status, ordering, genre filters, and studio (producer) filters.
|
||||||
func (c *Client) SearchAdvanced(ctx context.Context, query, animeType, status, orderBy, sort string, genres []int, studioID int, sfw bool, page, limit int) (SearchResult, error) {
|
func (c *Client) SearchAdvanced(ctx context.Context, query, animeType, status, orderBy, sort string, genres []int, studioID int, sfw bool, page, limit int) (SearchResult, error) {
|
||||||
page, limit = normalizeSearchPagination(page, limit)
|
page, limit = normalizePage(page, limit)
|
||||||
genresParam := joinGenreIDs(genres)
|
genresParam := joinGenreIDs(genres)
|
||||||
|
|
||||||
cacheKey := fmt.Sprintf("search:%s:%s:%s:%s:%s:%s:%d:%v:%d:%d", query, animeType, status, orderBy, sort, genresParam, studioID, sfw, page, limit)
|
cacheKey := fmt.Sprintf("search:%s:%s:%s:%s:%s:%s:%d:%v:%d:%d", query, animeType, status, orderBy, sort, genresParam, studioID, sfw, page, limit)
|
||||||
|
|
||||||
var result SearchResponse
|
var result SearchResponse
|
||||||
reqURL := buildAdvancedSearchURL(c.baseURL, query, animeType, status, orderBy, sort, genresParam, studioID, sfw, page, limit)
|
reqURL := advancedURL(c.baseURL, query, animeType, status, orderBy, sort, genresParam, studioID, sfw, page, limit)
|
||||||
|
|
||||||
if err := c.getWithCache(ctx, cacheKey, shortCacheTTL, reqURL, &result); err != nil {
|
if err := c.getWithCache(ctx, cacheKey, shortCacheTTL, reqURL, &result); err != nil {
|
||||||
return SearchResult{}, err
|
return SearchResult{}, err
|
||||||
|
|||||||
Reference in New Issue
Block a user