diff --git a/internal/features/anime/handler.go b/internal/features/anime/handler.go index 18b6eb5..50c9310 100644 --- a/internal/features/anime/handler.go +++ b/internal/features/anime/handler.go @@ -1,6 +1,7 @@ package anime import ( + "encoding/json" "log" "net/http" "strconv" @@ -124,3 +125,48 @@ func (h *Handler) HandleAPIAnimeRelations(w http.ResponseWriter, r *http.Request relations := h.svc.GetRelations(id) templates.AnimeRelationsList(relations).Render(r.Context(), w) } + +func (h *Handler) HandleQuickSearch(w http.ResponseWriter, r *http.Request) { + query := r.URL.Query().Get("q") + if query == "" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode([]interface{}{}) + return + } + + res, err := h.svc.Search(query, 1) + if err != nil { + log.Printf("quick search error: %v", err) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + return + } + + // Limit to 10 results + results := res.Animes + if len(results) > 10 { + results = results[:10] + } + + type SearchResult struct { + ID int `json:"id"` + Title string `json:"title"` + Type string `json:"type"` + Image string `json:"image"` + } + + output := make([]SearchResult, len(results)) + for i, anime := range results { + output[i] = SearchResult{ + ID: anime.MalID, + Title: anime.DisplayTitle(), + Type: anime.Type, + Image: anime.ImageURL(), + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(output) +} diff --git a/internal/server/routes.go b/internal/server/routes.go index 9718bff..2382118 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -36,6 +36,7 @@ func NewRouter(cfg Config) http.Handler { mux.HandleFunc("/", animeHandler.HandleCatalog) mux.HandleFunc("/search", animeHandler.HandleSearch) mux.HandleFunc("/api/search", animeHandler.HandleAPISearch) + mux.HandleFunc("/api/search-quick", animeHandler.HandleQuickSearch) mux.HandleFunc("/api/catalog", animeHandler.HandleAPICatalog) mux.HandleFunc("/anime/", animeHandler.HandleAnimeDetails) mux.HandleFunc("/api/anime/", animeHandler.HandleAPIAnimeRelations) diff --git a/internal/templates/layout.templ b/internal/templates/layout.templ index 7355e9f..e247768 100644 --- a/internal/templates/layout.templ +++ b/internal/templates/layout.templ @@ -20,15 +20,20 @@ templ Layout(title string) { watchlist -
+