boredgame/oldApis/games/search/+server.ts

95 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-09-04 23:04:41 +00:00
import { games } from '$db/schema'
import { FilterSchema, PaginationSchema, SearchSchema, SortSchema } from '$lib/validations/zod-schemas'
import { error, json } from '@sveltejs/kit'
import { asc, desc, eq, ilike, or } from 'drizzle-orm'
import kebabCase from 'just-kebab-case'
import db from '../../../../db'
// Search a user's collection
export const GET = async ({ url, locals }) => {
2024-09-04 23:04:41 +00:00
const searchParams = Object.fromEntries(url.searchParams)
2024-09-04 23:04:41 +00:00
const searchGames = PaginationSchema.merge(FilterSchema).merge(SortSchema).merge(SearchSchema).parse(searchParams)
if (searchGames.status !== 'success') {
2024-09-04 23:04:41 +00:00
error(400, 'Invalid request')
}
2024-09-04 23:04:41 +00:00
const q = searchParams?.q?.trim() || ''
const limit = parseInt(searchParams?.limit) || 10
const skip = parseInt(searchParams?.skip) || 0
const order: OrderDirection = searchParams?.order === 'desc' ? 'desc' : 'asc'
const exact = searchParams?.exact === 'true'
let orderBy = searchParams?.orderBy || 'slug'
if (orderBy === 'name') {
2024-09-04 23:04:41 +00:00
orderBy = 'slug'
}
2024-09-04 23:04:41 +00:00
console.log(`q: ${q}, limit: ${limit}, skip: ${skip}, order: ${order}, exact: ${exact}, orderBy: ${orderBy}`)
console.log(exact)
if (exact) {
2024-09-04 23:04:41 +00:00
console.log('Exact Search API')
const game = await db.query.games.findFirst({
where: eq(games.name, q),
columns: {
id: true,
name: true,
slug: true,
thumb_url: true,
},
2024-09-04 23:04:41 +00:00
})
if (!game) {
2024-09-04 23:04:41 +00:00
error(404, { message: 'No gamesTable found' })
}
2024-09-04 23:04:41 +00:00
const foundGames = [game]
console.log('Games found in Exact Search API', JSON.stringify(foundGames, null, 2))
return json(foundGames)
} else {
const foundGames =
(await db
.select({
id: games.id,
name: games.name,
slug: games.slug,
thumb_url: games.thumb_url,
})
.from(games)
.where(or(ilike(games.name, `%${q}%`), ilike(games.slug, `%${kebabCase(q)}%`)))
.orderBy(getOrderDirection(order)(getOrderBy(orderBy)))
.offset(skip)
2024-09-04 23:04:41 +00:00
.limit(limit)) || []
// const foundGames = await db.select({
2024-09-04 23:04:41 +00:00
// id: gamesTable.id,
// name: gamesTable.name,
// slug: gamesTable.slug,
// thumb_url: gamesTable.thumb_url
// })
2024-09-04 23:04:41 +00:00
// .from(gamesTable)
// .where(sql`to_tsvector('simple', ${gamesTable.name}) || to_tsvector('simple', ${gamesTable.slug}) @@ to_tsquery('simple', ${q})`)
// .orderBy(sql`${orderBy} ${order}`).offset(skip).limit(limit) || [];
if (foundGames.length === 0) {
2024-09-04 23:04:41 +00:00
error(404, { message: 'No gamesTable found' })
}
2024-09-04 23:04:41 +00:00
console.log('Games found in Search API', JSON.stringify(foundGames, null, 2))
return json(foundGames)
}
2024-09-04 23:04:41 +00:00
}
2024-09-04 23:04:41 +00:00
type OrderDirection = 'asc' | 'desc'
const getOrderDirection = (direction: OrderDirection) => {
2024-09-04 23:04:41 +00:00
return direction === 'asc' ? asc : desc
}
const getOrderBy = (orderBy: string) => {
switch (orderBy) {
case 'name':
2024-09-04 23:04:41 +00:00
return games.name
case 'slug':
2024-09-04 23:04:41 +00:00
return games.slug
default:
2024-09-04 23:04:41 +00:00
return games.slug
}
2024-09-04 23:04:41 +00:00
}