fix: limit quick search to five results

This commit is contained in:
2026-04-15 00:35:57 +02:00
parent 90c80b9d1e
commit 058aedd5e0
3 changed files with 20 additions and 5 deletions

View File

@@ -213,7 +213,7 @@ func (h *Handler) HandleQuickSearch(w http.ResponseWriter, r *http.Request) {
return
}
res, err := h.svc.Search(r.Context(), query, 1)
res, err := h.svc.QuickSearch(r.Context(), query, 1, 5)
if err != nil {
log.Printf("quick search error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
@@ -221,9 +221,6 @@ func (h *Handler) HandleQuickSearch(w http.ResponseWriter, r *http.Request) {
}
results := res.Animes
if len(results) > 5 {
results = results[:5]
}
output := make([]quickSearchResult, len(results))
for i, anime := range results {

View File

@@ -25,6 +25,10 @@ func (s *Service) Search(ctx context.Context, query string, page int) (jikan.Sea
return s.jikanClient.Search(ctx, query, page)
}
func (s *Service) QuickSearch(ctx context.Context, query string, page int, limit int) (jikan.SearchResult, error) {
return s.jikanClient.SearchWithLimit(ctx, query, page, limit)
}
func (s *Service) GetTopAnime(ctx context.Context, page int) (jikan.TopAnimeResult, error) {
return s.jikanClient.GetTopAnime(ctx, page)
}

View File

@@ -7,14 +7,25 @@ import (
)
func (c *Client) Search(ctx context.Context, query string, page int) (SearchResult, error) {
return c.search(ctx, query, page, 0)
}
func (c *Client) SearchWithLimit(ctx context.Context, query string, page int, limit int) (SearchResult, error) {
return c.search(ctx, query, page, limit)
}
func (c *Client) search(ctx context.Context, query string, page int, limit int) (SearchResult, error) {
if query == "" {
return SearchResult{}, nil
}
if page < 1 {
page = 1
}
if limit < 0 {
limit = 0
}
cacheKey := fmt.Sprintf("search:%s:%d", query, page)
cacheKey := fmt.Sprintf("search:%s:%d:%d", query, page, limit)
var cached SearchResult
if c.getCache(ctx, cacheKey, &cached) {
return cached, nil
@@ -25,6 +36,9 @@ func (c *Client) Search(ctx context.Context, query string, page int) (SearchResu
var result SearchResponse
reqURL := fmt.Sprintf("%s/anime?q=%s&page=%d", c.baseURL, url.QueryEscape(query), page)
if limit > 0 {
reqURL = fmt.Sprintf("%s&limit=%d", reqURL, limit)
}
if err := c.fetchWithRetry(ctx, reqURL, &result); err != nil {
if hasStale {