| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | package handlers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"neomovies-api/pkg/models" | 
					
						
							|  |  |  | 	"neomovies-api/pkg/services" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SearchHandler struct { | 
					
						
							|  |  |  | 	tmdbService *services.TMDBService | 
					
						
							| 
									
										
										
										
											2025-10-18 20:21:13 +00:00
										 |  |  | 	kpService   *services.KinopoiskService | 
					
						
							| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 20:21:13 +00:00
										 |  |  | func NewSearchHandler(tmdbService *services.TMDBService, kpService *services.KinopoiskService) *SearchHandler { | 
					
						
							| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | 	return &SearchHandler{ | 
					
						
							|  |  |  | 		tmdbService: tmdbService, | 
					
						
							| 
									
										
										
										
											2025-10-18 20:21:13 +00:00
										 |  |  | 		kpService:   kpService, | 
					
						
							| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (h *SearchHandler) MultiSearch(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	query := r.URL.Query().Get("query") | 
					
						
							|  |  |  | 	if query == "" { | 
					
						
							|  |  |  | 		http.Error(w, "Query parameter is required", http.StatusBadRequest) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	page := getIntQuery(r, "page", 1) | 
					
						
							| 
									
										
										
										
											2025-10-05 15:46:06 +00:00
										 |  |  | 	language := GetLanguage(r) | 
					
						
							| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-19 07:47:59 +00:00
										 |  |  |     if services.ShouldUseKinopoisk(language) { | 
					
						
							|  |  |  |         if h.kpService == nil { | 
					
						
							|  |  |  |             http.Error(w, "Kinopoisk service is not configured", http.StatusBadGateway) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-10-18 20:21:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-19 07:47:59 +00:00
										 |  |  |         kpSearch, err := h.kpService.SearchFilms(query, page) | 
					
						
							|  |  |  |         if err != nil { | 
					
						
							|  |  |  |             http.Error(w, err.Error(), http.StatusBadGateway) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         tmdbResp := services.MapKPSearchToTMDBResponse(kpSearch) | 
					
						
							|  |  |  |         multiResults := make([]models.MultiSearchResult, 0) | 
					
						
							|  |  |  |         for _, movie := range tmdbResp.Results { | 
					
						
							|  |  |  |             multiResults = append(multiResults, models.MultiSearchResult{ | 
					
						
							|  |  |  |                 ID:               movie.ID, | 
					
						
							|  |  |  |                 MediaType:        "movie", | 
					
						
							|  |  |  |                 Title:            movie.Title, | 
					
						
							|  |  |  |                 OriginalTitle:    movie.OriginalTitle, | 
					
						
							|  |  |  |                 Overview:         movie.Overview, | 
					
						
							|  |  |  |                 PosterPath:       movie.PosterPath, | 
					
						
							|  |  |  |                 BackdropPath:     movie.BackdropPath, | 
					
						
							|  |  |  |                 ReleaseDate:      movie.ReleaseDate, | 
					
						
							|  |  |  |                 VoteAverage:      movie.VoteAverage, | 
					
						
							|  |  |  |                 VoteCount:        movie.VoteCount, | 
					
						
							|  |  |  |                 Popularity:       movie.Popularity, | 
					
						
							|  |  |  |                 Adult:            movie.Adult, | 
					
						
							|  |  |  |                 OriginalLanguage: movie.OriginalLanguage, | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         w.Header().Set("Content-Type", "application/json") | 
					
						
							|  |  |  |         json.NewEncoder(w).Encode(models.APIResponse{ | 
					
						
							|  |  |  |             Success: true, | 
					
						
							|  |  |  |             Data: models.MultiSearchResponse{ | 
					
						
							|  |  |  |                 Page:         page, | 
					
						
							|  |  |  |                 Results:      multiResults, | 
					
						
							|  |  |  |                 TotalPages:   tmdbResp.TotalPages, | 
					
						
							|  |  |  |                 TotalResults: tmdbResp.TotalResults, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // EN/прочие языки — TMDB | 
					
						
							|  |  |  |     results, err := h.tmdbService.SearchMulti(query, page, language) | 
					
						
							|  |  |  |     if err != nil { | 
					
						
							|  |  |  |         http.Error(w, err.Error(), http.StatusInternalServerError) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-08-07 13:47:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-19 07:47:59 +00:00
										 |  |  |     w.Header().Set("Content-Type", "application/json") | 
					
						
							|  |  |  |     json.NewEncoder(w).Encode(models.APIResponse{ | 
					
						
							|  |  |  |         Success: true, | 
					
						
							|  |  |  |         Data:    results, | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2025-09-28 11:46:20 +00:00
										 |  |  | } |