File size: 1,202 Bytes
98b0aa6
 
 
b34e9b1
 
98b0aa6
 
 
b34e9b1
 
 
 
 
 
 
 
98b0aa6
 
 
 
b34e9b1
 
 
 
 
98b0aa6
 
 
 
b34e9b1
 
 
 
 
 
 
 
 
 
98b0aa6
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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,
  })
}