mirror of
https://gitlab.com/foxixus/neomovies.git
synced 2025-10-28 01:48:50 +05:00
Fix search
This commit is contained in:
@@ -4,6 +4,7 @@ import { searchAPI } from '@/lib/neoApi';
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = searchParams.get('query');
|
||||
const page = searchParams.get('page') || '1';
|
||||
|
||||
if (!query) {
|
||||
return NextResponse.json(
|
||||
@@ -13,16 +14,18 @@ export async function GET(request: Request) {
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await searchAPI.multiSearch(query);
|
||||
// Используем обновленный multiSearch, который теперь запрашивает и фильмы, и сериалы параллельно
|
||||
const response = await searchAPI.multiSearch(query, parseInt(page));
|
||||
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
console.error('Error searching:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to search',
|
||||
details: error.message
|
||||
details: error.message || 'Unknown error'
|
||||
},
|
||||
{ status: 500 }
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ export interface Movie {
|
||||
genre_ids: number[];
|
||||
runtime?: number;
|
||||
genres?: Genre[];
|
||||
popularity?: number;
|
||||
media_type?: string;
|
||||
}
|
||||
|
||||
export interface MovieResponse {
|
||||
@@ -79,15 +81,72 @@ export interface MovieResponse {
|
||||
}
|
||||
|
||||
export const searchAPI = {
|
||||
// Мультипоиск (фильмы и сериалы)
|
||||
multiSearch(query: string, page = 1) {
|
||||
return neoApi.get<MovieResponse>('/search/multi', {
|
||||
// Поиск фильмов
|
||||
searchMovies(query: string, page = 1) {
|
||||
return neoApi.get<MovieResponse>('/movies/search', {
|
||||
params: {
|
||||
query,
|
||||
page
|
||||
},
|
||||
timeout: 30000 // Увеличиваем таймаут до 30 секунд
|
||||
timeout: 30000
|
||||
});
|
||||
},
|
||||
|
||||
// Поиск сериалов
|
||||
searchTV(query: string, page = 1) {
|
||||
return neoApi.get<MovieResponse>('/tv/search', {
|
||||
params: {
|
||||
query,
|
||||
page
|
||||
},
|
||||
timeout: 30000
|
||||
});
|
||||
},
|
||||
|
||||
// Мультипоиск (фильмы и сериалы)
|
||||
async multiSearch(query: string, page = 1) {
|
||||
// Запускаем параллельные запросы к фильмам и сериалам
|
||||
try {
|
||||
const [moviesResponse, tvResponse] = await Promise.all([
|
||||
this.searchMovies(query, page),
|
||||
this.searchTV(query, page)
|
||||
]);
|
||||
|
||||
// Объединяем результаты
|
||||
const moviesData = moviesResponse.data;
|
||||
const tvData = tvResponse.data;
|
||||
|
||||
// Метаданные для пагинации
|
||||
const totalResults = (moviesData.total_results || 0) + (tvData.total_results || 0);
|
||||
const totalPages = Math.max(moviesData.total_pages || 0, tvData.total_pages || 0);
|
||||
|
||||
// Добавляем информацию о типе контента
|
||||
const moviesWithType = (moviesData.results || []).map(movie => ({
|
||||
...movie,
|
||||
media_type: 'movie'
|
||||
}));
|
||||
|
||||
const tvWithType = (tvData.results || []).map(show => ({
|
||||
...show,
|
||||
media_type: 'tv'
|
||||
}));
|
||||
|
||||
// Объединяем и сортируем по популярности
|
||||
const combinedResults = [...moviesWithType, ...tvWithType]
|
||||
.sort((a, b) => (b.popularity || 0) - (a.popularity || 0));
|
||||
|
||||
return {
|
||||
data: {
|
||||
page: parseInt(String(page)),
|
||||
results: combinedResults,
|
||||
total_pages: totalPages,
|
||||
total_results: totalResults
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error in multiSearch:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user