mirror of
https://github.com/ngthanhvu/kr-phim.git
synced 2026-08-10 16:47:46 +00:00
feat: add movie management API and database schema
- Implemented movie CRUD operations with endpoints for creating, retrieving, updating, and deleting movies. - Added pagination and filtering capabilities for movie retrieval. - Integrated Redis caching for improved performance on movie data retrieval. - Created database schema for movies with necessary fields and types. - Added migration files for initial movie table creation and subsequent updates. - Configured database connection using Drizzle ORM with MySQL. - Established Redis connection utility for caching.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { movies } from '../../database/schema'
|
||||
import { desc, 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.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),
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user