25 lines
720 B
TypeScript
25 lines
720 B
TypeScript
import type { Chapter } from '~/types'
|
|
|
|
export default defineEventHandler(async (event): Promise<Chapter[]> => {
|
|
const novelId = getRouterParam(event, 'novelId')
|
|
|
|
const chapters = await queryCollection('content').all() as Chapter[]
|
|
|
|
const novelChapters = chapters
|
|
.filter(ch => ch._path?.includes(`/novels/${novelId}/ch-`))
|
|
.sort((a, b) => {
|
|
const aNum = parseInt(a._path?.match(/ch-(\d+)/)?.[1] || '0') || 0
|
|
const bNum = parseInt(b._path?.match(/ch-(\d+)/)?.[1] || '0') || 0
|
|
return aNum - bNum
|
|
}) as Chapter[]
|
|
|
|
if (!novelChapters.length) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'No chapters found for this novel'
|
|
})
|
|
}
|
|
|
|
return novelChapters
|
|
})
|