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
This commit is contained in:
sebastjanartic 2025-08-04 20:14:30 +00:00
parent 018f559617
commit 58dc46ef4b

View File

@ -158,13 +158,29 @@ class BunnyStorage implements IStorage {
async getVideos(limit = 20, offset = 0, search?: string, category?: string): Promise<Video[]> { async getVideos(limit = 20, offset = 0, search?: string, category?: string): Promise<Video[]> {
try { try {
const page = Math.floor(offset / limit) + 1; // For search/filtering, we need to get more videos than the requested limit
const { videos } = await this.bunnyService.getVideos(page, limit, search); // 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 // Filter by category if specified
let filteredVideos = videos;
if (category && category !== "All Categories") { if (category && category !== "All Categories") {
filteredVideos = videos.filter(video => video.category === category); filteredVideos = filteredVideos.filter(video => video.category === category);
} }
// Apply cached view counts // 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) { } catch (error) {
console.error('Error fetching videos from Bunny:', error); console.error('Error fetching videos from Bunny:', error);
// Fallback to empty array on error // Fallback to empty array on error
@ -213,8 +230,9 @@ class BunnyStorage implements IStorage {
async getVideoCount(search?: string, category?: string): Promise<number> { async getVideoCount(search?: string, category?: string): Promise<number> {
try { try {
const { total } = await this.bunnyService.getVideos(1, 1, search); // For accurate count with client-side filtering, we need to get all videos and filter them
return total; const allVideos = await this.getVideos(1000, 0, search, category);
return allVideos.length;
} catch (error) { } catch (error) {
console.error('Error getting video count from Bunny:', error); console.error('Error getting video count from Bunny:', error);
return 0; return 0;