mirror of
https://github.com/ngthanhvu/kr-phim.git
synced 2026-08-10 17:07:46 +00:00
- Implemented AuthModal.vue for user login and registration with form handling. - Created useAuth composable for managing user state and authentication requests. - Added admin route middleware to protect admin routes. - Developed admin login page with authentication checks. - Implemented API endpoints for user management (login, logout, fetch user, register). - Created database migrations for users table and updated movies table. - Added utility functions for password hashing and JWT token management.
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { movies } from '../../database/schema'
|
|
import { desc, eq, like, sql } from 'drizzle-orm'
|
|
|
|
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 : ''
|
|
|
|
let whereClause = undefined
|
|
if (keyword && status === 'active') {
|
|
whereClause = sql`${like(movies.name, `%${keyword}%`)} AND ${movies.active} = true`
|
|
} else if (keyword) {
|
|
whereClause = like(movies.name, `%${keyword}%`)
|
|
} else if (status === 'active') {
|
|
whereClause = eq(movies.active, true)
|
|
} else if (status === 'inactive') {
|
|
whereClause = eq(movies.active, false)
|
|
}
|
|
|
|
const [items, countResult] = await Promise.all([
|
|
db
|
|
.select()
|
|
.from(movies)
|
|
.where(whereClause)
|
|
.orderBy(desc(movies.apiUpdatedAt), desc(movies.year), desc(movies.syncedAt))
|
|
.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),
|
|
}
|
|
})
|