From 78cbb5820b26deed41babee9b029bf0e9c49477b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Wed, 22 Apr 2026 18:39:34 +0200 Subject: [PATCH] PMPR-36 : DELETE /ues/{idUE} - supprimer une UE --- routes/(apps)/notes/api/ues/[idUE].ts | 103 ++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 routes/(apps)/notes/api/ues/[idUE].ts diff --git a/routes/(apps)/notes/api/ues/[idUE].ts b/routes/(apps)/notes/api/ues/[idUE].ts new file mode 100644 index 0000000..eb7fdb5 --- /dev/null +++ b/routes/(apps)/notes/api/ues/[idUE].ts @@ -0,0 +1,103 @@ +import { Handlers } from "$fresh/server.ts"; +import { db } from "../../../../../databases/db.ts"; +import { ues } from "../../../../../databases/schema.ts"; +import { eq } from "npm:drizzle-orm"; + +export const handler: Handlers = { + // #34 GET /ues/:idUE + async GET(_request, context) { + try { + const idUE = parseInt(context.params.idUE); + + if (isNaN(idUE)) { + return new Response(JSON.stringify({ error: "Paramètre idUE invalide" }), { + status: 400, + headers: { "Content-Type": "application/json" }, + }); + } + + const result = await db.select().from(ues).where(eq(ues.id, idUE)); + + 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 UE:", error); + return new Response("Failed to fetch data", { status: 500 }); + } + }, + + // #35 PUT /ues/:idUE + async PUT(request, context) { + try { + const idUE = parseInt(context.params.idUE); + + if (isNaN(idUE)) { + return new Response(JSON.stringify({ error: "Paramètre idUE invalide" }), { + status: 400, + headers: { "Content-Type": "application/json" }, + }); + } + + const body = await request.json(); + const { nom } = body; + + if (!nom) { + return new Response("Champ 'nom' manquant", { status: 400 }); + } + + const result = await db.update(ues).set({ nom }).where(eq(ues.id, idUE)).returning(); + + 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 updating UE:", error); + return new Response("Failed to update UE", { status: 500 }); + } + }, + + // #36 DELETE /ues/:idUE + async DELETE(_request, context) { + try { + const idUE = parseInt(context.params.idUE); + + if (isNaN(idUE)) { + return new Response(JSON.stringify({ error: "Paramètre idUE invalide" }), { + status: 400, + headers: { "Content-Type": "application/json" }, + }); + } + + const result = await db.delete(ues).where(eq(ues.id, idUE)).returning(); + + if (result.length === 0) { + return new Response(JSON.stringify({ error: "Ressource introuvable" }), { + status: 404, + headers: { "Content-Type": "application/json" }, + }); + } + + return new Response(null, { status: 204 }); + } catch (error) { + console.error("Error deleting UE:", error); + return new Response("Failed to delete UE", { status: 500 }); + } + }, +}; \ No newline at end of file