2026-07-22 00:31:10 -04:00
|
|
|
import { movies } from '../../database/schema'
|
|
|
|
|
import { eq, and } from 'drizzle-orm'
|
2026-05-23 15:08:57 +07:00
|
|
|
import { getMovieDetailGroup, parseSourceRefs } from '../../utils/movies'
|
2026-05-17 20:30:19 +07:00
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
|
const slug = getRouterParam(event, 'slug')
|
2026-05-23 15:08:57 +07:00
|
|
|
const query = getQuery(event)
|
2026-05-17 20:30:19 +07:00
|
|
|
|
|
|
|
|
if (!slug) {
|
|
|
|
|
throw createError({ statusCode: 400, statusMessage: 'Thiếu slug phim' })
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 00:31:10 -04:00
|
|
|
const source = typeof query.source === 'string' ? query.source : 'nguonc'
|
|
|
|
|
const db = useDb()
|
|
|
|
|
|
|
|
|
|
const existing = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(movies)
|
|
|
|
|
.where(and(eq(movies.slug, slug), eq(movies.source, source), eq(movies.active, true)))
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
2026-07-23 02:56:24 -04:00
|
|
|
let recordToUpdate = existing.length ? existing[0] : null
|
|
|
|
|
|
|
|
|
|
if (!recordToUpdate) {
|
2026-07-22 00:31:10 -04:00
|
|
|
const fallback = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(movies)
|
|
|
|
|
.where(and(eq(movies.slug, slug), eq(movies.active, true)))
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
|
|
if (!fallback.length) {
|
|
|
|
|
throw createError({ statusCode: 404, statusMessage: 'Phim không tồn tại hoặc đã bị ẩn' })
|
|
|
|
|
}
|
2026-07-23 02:56:24 -04:00
|
|
|
recordToUpdate = fallback[0]
|
2026-07-22 00:31:10 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-24 22:35:13 +07:00
|
|
|
const refs = parseSourceRefs(query.sources || query.srcs, query.source, slug)
|
2026-05-21 23:07:59 +07:00
|
|
|
|
2026-07-23 02:56:24 -04:00
|
|
|
const detail = await getMovieDetailGroup(refs)
|
|
|
|
|
|
|
|
|
|
if (detail.episodeTotal && recordToUpdate) {
|
|
|
|
|
try {
|
|
|
|
|
await db
|
|
|
|
|
.update(movies)
|
|
|
|
|
.set({ episodeTotal: detail.episodeTotal })
|
|
|
|
|
.where(eq(movies.id, recordToUpdate.id))
|
|
|
|
|
} catch {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return detail
|
2026-05-17 20:30:19 +07:00
|
|
|
})
|