diff --git a/server/storage.ts b/server/storage.ts index fe325d9..664fd8d 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -158,13 +158,29 @@ class BunnyStorage implements IStorage { async getVideos(limit = 20, offset = 0, search?: string, category?: string): Promise { try { - const page = Math.floor(offset / limit) + 1; - const { videos } = await this.bunnyService.getVideos(page, limit, search); + // For search/filtering, we need to get more videos than the requested limit + // because we'll filter client-side and then slice to the requested amount + const fetchLimit = (search || category) ? 100 : limit; + const page = Math.floor(offset / fetchLimit) + 1; + + // Get videos from Bunny API + const { videos } = await this.bunnyService.getVideos(page, fetchLimit); + + // Apply client-side filtering + let filteredVideos = videos; + + // Filter by search + if (search) { + const searchLower = search.toLowerCase(); + filteredVideos = filteredVideos.filter(video => + video.title.toLowerCase().includes(searchLower) || + (video.description && video.description.toLowerCase().includes(searchLower)) + ); + } // Filter by category if specified - let filteredVideos = videos; if (category && category !== "All Categories") { - filteredVideos = videos.filter(video => video.category === category); + filteredVideos = filteredVideos.filter(video => video.category === category); } // Apply cached view counts @@ -174,7 +190,8 @@ class BunnyStorage implements IStorage { } }); - return filteredVideos; + // Apply pagination after filtering + return filteredVideos.slice(offset, offset + limit); } catch (error) { console.error('Error fetching videos from Bunny:', error); // Fallback to empty array on error @@ -213,8 +230,9 @@ class BunnyStorage implements IStorage { async getVideoCount(search?: string, category?: string): Promise { try { - const { total } = await this.bunnyService.getVideos(1, 1, search); - return total; + // For accurate count with client-side filtering, we need to get all videos and filter them + const allVideos = await this.getVideos(1000, 0, search, category); + return allVideos.length; } catch (error) { console.error('Error getting video count from Bunny:', error); return 0;