From 58dc46ef4bb6e3ff301cbe346b95f105ac618a5c Mon Sep 17 00:00:00 2001 From: sebastjanartic <45803536-sebastjanartic@users.noreply.replit.com> Date: Mon, 4 Aug 2025 20:14:30 +0000 Subject: [PATCH] Improve video search and filtering to provide more accurate results Implements client-side filtering and adjusts pagination in `BunnyStorage.getVideos` and retrieves all videos for `getVideoCount` to enhance search accuracy. Replit-Commit-Author: Agent Replit-Commit-Session-Id: aa92e7e2-ec62-4c92-b21b-02ef78a664c2 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/aa92e7e2-ec62-4c92-b21b-02ef78a664c2/0lh52lE --- server/storage.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) 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;