32 lines
1,008 B
TypeScript
32 lines
1,008 B
TypeScript
import { db } from "../../db/index";
|
|
import { courses, topics } from "../../db/schema";
|
|
import { inArray, eq } from "drizzle-orm";
|
|
|
|
export default defineEventHandler(async () => {
|
|
const allCourses = await db.query.courses.findMany();
|
|
|
|
if (allCourses.length === 0) return [];
|
|
|
|
const courseIds = allCourses.map((c) => c.id);
|
|
const allTopics = await db.query.topics.findMany({
|
|
where: inArray(topics.courseId, courseIds),
|
|
});
|
|
|
|
const topicCountByCourse = new Map<string, number>();
|
|
const readyCountByCourse = new Map<string, number>();
|
|
|
|
for (const t of allTopics) {
|
|
topicCountByCourse.set(t.courseId, (topicCountByCourse.get(t.courseId) ?? 0) + 1);
|
|
if (t.status === "ready") {
|
|
readyCountByCourse.set(t.courseId, (readyCountByCourse.get(t.courseId) ?? 0) + 1);
|
|
}
|
|
}
|
|
|
|
return allCourses.map((c) => ({
|
|
id: c.id,
|
|
status: c.status,
|
|
stage: c.stage,
|
|
topicCount: topicCountByCourse.get(c.id) ?? 0,
|
|
readyTopicCount: readyCountByCourse.get(c.id) ?? 0,
|
|
}));
|
|
});
|