import { Handlers } from "$fresh/server.ts";
import { createSupabaseClient } from "../../../plugins/auth.ts";
export const handler: Handlers = {
async GET(req, _ctx) {
const headers = new Headers();
const resp = new Response();
try {
const url = new URL(req.url); // Parse the URL
const pathId = url.searchParams.get("pathId");
console.log(pathId);
// Validate the header
if (!pathId) {
return new Response(
JSON.stringify({ success: false, message: "Path id header missing." }),
{ status: 400, headers }
);
}
const supabaseClient = createSupabaseClient(req, resp);
// Fetch modules for the given path slug
const { data: path, error: pathError } = await supabaseClient
.from("paths")
.select("id")
.eq("id", pathId)
.single();
if (pathError || !path) {
return new Response(
JSON.stringify({ success: false, message: "Path not found." }),
{ status: 404, headers }
);
}
const { data: modules, error: modulesError } = await supabaseClient
.from("modules")
.select("*")
.eq("path_id", path.id);
if (modulesError) {
console.error("Error fetching modules:", modulesError.message);
return new Response(
JSON.stringify({ success: false, message: "Failed to fetch modules." }),
{ status: 500, headers }
);
}
return new Response(
JSON.stringify({ success: true, modules }),
{ status: 200, headers }
);
}
.....
}
}