diff --git a/routes/(apps)/notes/api/ues/[idUE].ts b/routes/(apps)/notes/api/ues/[idUE].ts index 9fb70fa..c92e118 100644 --- a/routes/(apps)/notes/api/ues/[idUE].ts +++ b/routes/(apps)/notes/api/ues/[idUE].ts @@ -34,4 +34,70 @@ export const handler: Handlers = { return new Response("Failed to fetch data", { status: 500 }); } }, -}; \ No newline at end of file + + // #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 }); + } + }, +};