2026-07-22 00:31:10 -04:00
|
|
|
import { movies } from '../../database/schema'
|
2026-07-23 02:56:24 -04:00
|
|
|
import { desc, eq, like, and, sql } from 'drizzle-orm'
|
2026-07-22 00:31:10 -04:00
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
|
const db = useDb()
|
|
|
|
|
const query = getQuery(event)
|
|
|
|
|
const page = Math.max(Number(query.page) || 1, 1)
|
|
|
|
|
const limit = Math.min(Math.max(Number(query.limit) || 20, 1), 100)
|
|
|
|
|
const offset = (page - 1) * limit
|
|
|
|
|
const keyword = typeof query.keyword === 'string' ? query.keyword.trim() : ''
|
|
|
|
|
const status = typeof query.status === 'string' ? query.status : ''
|
2026-07-23 02:56:24 -04:00
|
|
|
const source = typeof query.source === 'string' ? query.source : ''
|
|
|
|
|
const type = typeof query.type === 'string' ? query.type : ''
|
2026-07-22 00:31:10 -04:00
|
|
|
|
2026-07-23 02:56:24 -04:00
|
|
|
const conditions: any[] = []
|
|
|
|
|
|
|
|
|
|
if (keyword) conditions.push(like(movies.name, `%${keyword}%`))
|
|
|
|
|
if (status === 'active') conditions.push(eq(movies.active, true))
|
|
|
|
|
else if (status === 'inactive') conditions.push(eq(movies.active, false))
|
|
|
|
|
if (source) conditions.push(eq(movies.source, source))
|
|
|
|
|
if (type) conditions.push(eq(movies.type, type))
|
|
|
|
|
|
|
|
|
|
const whereClause = conditions.length > 0 ? and(...conditions) : undefined
|
2026-07-22 00:31:10 -04:00
|
|
|
|
|
|
|
|
const [items, countResult] = await Promise.all([
|
|
|
|
|
db
|
|
|
|
|
.select()
|
|
|
|
|
.from(movies)
|
|
|
|
|
.where(whereClause)
|
2026-07-22 04:26:58 -04:00
|
|
|
.orderBy(desc(movies.apiUpdatedAt), desc(movies.year), desc(movies.syncedAt))
|
2026-07-22 00:31:10 -04:00
|
|
|
.limit(limit)
|
|
|
|
|
.offset(offset),
|
|
|
|
|
db
|
|
|
|
|
.select({ count: sql<number>`count(*)` })
|
|
|
|
|
.from(movies)
|
|
|
|
|
.where(whereClause),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const total = countResult[0]?.count ?? 0
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
items,
|
|
|
|
|
page,
|
|
|
|
|
limit,
|
|
|
|
|
total,
|
|
|
|
|
totalPages: Math.ceil(total / limit),
|
|
|
|
|
}
|
|
|
|
|
})
|