enzostvs's picture
enzostvs HF Staff
models filters + search
b34e9b1
raw
history blame
1.2 kB
import { error, json, type RequestEvent } from '@sveltejs/kit';
// import { env } from '$env/dynamic/private'
import jsonData from "$lib/utils/loras.json";
import type { ModelCard } from '$lib/type';
/** @type {import('./$types').RequestHandler} */
const Dict: Record<
string,
(a: ModelCard, b: ModelCard) => number
> = {
hotest: (a: ModelCard, b: ModelCard) => (b.downloads ?? 0) - (a.downloads ?? 0),
likes: (a: ModelCard, b: ModelCard) => b.likes - a.likes,
}
export async function GET(request : RequestEvent) {
const hasError = false
const page = parseInt(request.url.searchParams.get('page') || '0')
const filter = request.url.searchParams.get('filter') || 'hotest'
const search = request.url.searchParams.get('search') || ''
console.log({ search })
if (hasError) {
return error(500, 'Internal Server Error')
}
let cards: ModelCard[] = jsonData
if (search) {
cards = cards.filter((card) => card.title.toLowerCase().includes(search.toLowerCase()))
}
const sortFunc = Dict[filter] || Dict['hotest']
cards = cards.sort(sortFunc)
cards = cards.slice(page * 25, page * 25 + 25)
return json({
cards,
total_items: jsonData.length,
})
}