From be651ed2ddcb4c8e857b7bdcb1601465e206bdb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Oudelet?= Date: Thu, 23 Apr 2026 11:56:26 +0200 Subject: [PATCH] PMPR-46/47 : PUT et DELETE /notes/{numEtud}/{idModule} --- .../notes/api/notes/[numEtud]/[idModule].ts | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts b/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts index 18092f6..24d8a28 100644 --- a/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts +++ b/routes/(apps)/notes/api/notes/[numEtud]/[idModule].ts @@ -40,4 +40,82 @@ export const handler: Handlers = { return new Response("Failed to fetch data", { status: 500 }); } }, + + // #46 PUT /notes/:numEtud/:idModule + async PUT(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 body = await request.json(); + const { note } = body; + + if (note === undefined) { + return new Response("Champ 'note' manquant", { status: 400 }); + } + + const result = await db.update(notes).set({ note }).where( + and( + eq(notes.numEtud, numEtud), + eq(notes.idModule, idModule), + ), + ).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 note:", error); + return new Response("Failed to update note", { status: 500 }); + } + }, + + // #47 DELETE /notes/:numEtud/:idModule + async DELETE(_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.delete(notes).where( + and( + eq(notes.numEtud, numEtud), + eq(notes.idModule, idModule), + ), + ).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 note:", error); + return new Response("Failed to delete note", { status: 500 }); + } + }, }; \ No newline at end of file