From 6f5e39c5beecfe7c75883bdbb1696a34b1fe940d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Thu, 23 Apr 2026 11:21:16 +0200 Subject: [PATCH] =?UTF-8?q?GET=20/notes/{numEtud}/{idModule}=20-=20r=C3=A9?= =?UTF-8?q?cup=C3=A9rer=20le=20d=C3=A9tail=20d'une=20note=20pour=20un=20?= =?UTF-8?q?=C3=A9tudiant=20dans=20un=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notes/api/notes/[numEtud]/[idModule].ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts diff --git a/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts b/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts new file mode 100644 index 0000000..18092f6 --- /dev/null +++ b/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts @@ -0,0 +1,43 @@ +import { Handlers } from "$fresh/server.ts"; +import { db } from "../../../../../../databases/db.ts"; +import { notes } from "../../../../../../databases/schema.ts"; +import { and, eq } from "npm:drizzle-orm"; + +export const handler: Handlers = { + // #45 GET /notes/:numEtud/:idModule + async GET(_request, context) { + try { + const numEtud = parseInt(context.params.numEtud); + const { idModule } = context.params; + + if (isNaN(numEtud)) { + return new Response(JSON.stringify({ error: "Paramètre numEtud invalide" }), { + status: 400, + headers: { "Content-Type": "application/json" }, + }); + } + + const result = await db.select().from(notes).where( + and( + eq(notes.numEtud, numEtud), + eq(notes.idModule, idModule), + ), + ); + + if (result.length === 0) { + return new Response(JSON.stringify({ error: "Ressource introuvable" }), { + status: 404, + headers: { "Content-Type": "application/json" }, + }); + } + + return new Response(JSON.stringify(result[0]), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + } catch (error) { + console.error("Error fetching note:", error); + return new Response("Failed to fetch data", { status: 500 }); + } + }, +}; \ No newline at end of file