From fea48a957147d881765c41eb2cd9f6234e352803 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Wed, 12 Nov 2025 04:54:31 +0000 Subject: [PATCH] Create public Foundation courses endpoint cgen-0ccbb65c4a594d9c98858d0c6bcfc5c3 --- api/foundation/courses.ts | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 api/foundation/courses.ts diff --git a/api/foundation/courses.ts b/api/foundation/courses.ts new file mode 100644 index 00000000..a18d5856 --- /dev/null +++ b/api/foundation/courses.ts @@ -0,0 +1,76 @@ +import { createClient } from "@supabase/supabase-js"; + +const supabase = createClient( + process.env.VITE_SUPABASE_URL!, + process.env.VITE_SUPABASE_ANON_KEY!, +); + +export async function getCourses(req: Request) { + try { + const url = new URL(req.url); + const category = url.searchParams.get("category"); + const difficulty = url.searchParams.get("difficulty"); + + let query = supabase.from("foundation_courses").select( + ` + id, + slug, + title, + description, + category, + difficulty, + instructor_id, + cover_image_url, + estimated_hours, + is_published, + user_profiles!foundation_courses_instructor_id_fkey ( + id, + full_name, + avatar_url + ) + `, + ); + + // Only show published courses + query = query.eq("is_published", true); + + if (category) { + query = query.eq("category", category); + } + + if (difficulty) { + query = query.eq("difficulty", difficulty); + } + + const { data: courses, error } = await query.order("created_at", { + ascending: false, + }); + + if (error) throw error; + + const formattedCourses = (courses || []).map((c: any) => ({ + id: c.id, + slug: c.slug, + title: c.title, + description: c.description, + category: c.category, + difficulty: c.difficulty, + instructor_id: c.instructor_id, + instructor_name: c.user_profiles?.full_name, + instructor_avatar: c.user_profiles?.avatar_url, + cover_image_url: c.cover_image_url, + estimated_hours: c.estimated_hours, + })); + + return new Response(JSON.stringify(formattedCourses), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + } catch (error: any) { + console.error("Error fetching courses:", error); + return new Response( + JSON.stringify({ error: "Failed to fetch courses" }), + { status: 500, headers: { "Content-Type": "application/json" } }, + ); + } +}