import { db } from "../../../db/index"; import { courses, topics, lessons } from "../../../db/schema"; import { eq, inArray } from "drizzle-orm"; export default defineEventHandler(async (event) => { const id = getRouterParam(event, "id")!; const course = await db.query.courses.findFirst({ where: eq(courses.id, id) }); if (!course) throw createError({ statusCode: 404, message: "Course not found" }); const topicRows = await db.query.topics.findMany({ where: eq(topics.courseId, id), orderBy: (t, { asc }) => asc(t.order), }); let lessonTopicIds: Set = new Set(); if (topicRows.length > 0) { const topicIds = topicRows.map((t) => t.id); const lessonRows = await db.query.lessons.findMany({ where: inArray(lessons.topicId, topicIds), }); lessonTopicIds = new Set(lessonRows.map((l) => l.topicId)); } return { status: course.status, stage: course.stage, topics: topicRows.map((t) => ({ id: t.id, status: t.status, hasLesson: lessonTopicIds.has(t.id), })), }; });