import { asc, like, or, sql } from 'drizzle-orm' import { useDB, schema } from '../../utils/db.js' import { requireAdmin } from '../../utils/auth.js' import { parsePagination } from '../../utils/pagination.js' export default defineEventHandler(async (event) => { requireAdmin(event) const db = useDB() const { page, limit, offset, search } = parsePagination(event) let whereClause = sql`1=1` if (search) { whereClause = or( like(schema.galleryItems.title, `%${search}%`), like(schema.galleryItems.category, `%${search}%`), like(schema.galleryItems.location, `%${search}%`) ) } const [{ count: total }] = db.select({ count: sql`count(*)` }).from(schema.galleryItems).where(whereClause).all() const items = db.select().from(schema.galleryItems).where(whereClause).orderBy(asc(schema.galleryItems.sortOrder)).limit(limit).offset(offset).all() return { items, total, page, limit, totalPages: Math.ceil(total / limit) } })