21 lines
718 B
TypeScript
21 lines
718 B
TypeScript
import { db } from "../../../db/index";
|
|
import { topics, quizQuestions } from "../../../db/schema";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const id = getRouterParam(event, "id")!;
|
|
|
|
const topic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
|
if (!topic) throw createError({ statusCode: 404, message: "Topic not found" });
|
|
|
|
const questions = await db.query.quizQuestions.findMany({
|
|
where: eq(quizQuestions.topicId, id),
|
|
});
|
|
|
|
if (questions.length === 0) throw createError({ statusCode: 404, message: "Quiz not yet generated" });
|
|
|
|
return questions.map((q) => ({
|
|
...q,
|
|
options: q.options ? JSON.parse(q.options) : null,
|
|
}));
|
|
});
|