From 92182b952f8ce4fdee49f9e2c1b965b848706d55 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Wed, 22 Apr 2026 14:03:22 +0200 Subject: [PATCH] feat(modules): add CRUD endpoints for module resource Implement GET, PUT, DELETE for /modules/{idModule} with 404 handling. --- routes/(apps)/admin/api/modules/[idModule].ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 routes/(apps)/admin/api/modules/[idModule].ts diff --git a/routes/(apps)/admin/api/modules/[idModule].ts b/routes/(apps)/admin/api/modules/[idModule].ts new file mode 100644 index 0000000..3062772 --- /dev/null +++ b/routes/(apps)/admin/api/modules/[idModule].ts @@ -0,0 +1,65 @@ +import { FreshContext, Handlers } from "$fresh/server.ts"; +import { db } from "$root/databases/db.ts"; +import { modules } from "$root/databases/schema.ts"; +import { AuthenticatedState } from "$root/defaults/interfaces.ts"; +import { eq } from "npm:drizzle-orm"; + +const NOT_FOUND = new Response( + JSON.stringify({ error: "Ressource introuvable" }), + { status: 404, headers: { "content-type": "application/json" } }, +); + +export const handler: Handlers = { + // #25 GET /modules/{idModule} + async GET( + _request: Request, + context: FreshContext, + ): Promise { + const module = await db + .select() + .from(modules) + .where(eq(modules.id, context.params.idModule)) + .then((rows) => rows[0] ?? null); + + if (!module) return NOT_FOUND; + + return new Response(JSON.stringify(module), { + headers: { "content-type": "application/json" }, + }); + }, + + // #26 PUT /modules/{idModule} + async PUT( + request: Request, + context: FreshContext, + ): Promise { + const body: { nom: string } = await request.json(); + + const [updated] = await db + .update(modules) + .set({ nom: body.nom }) + .where(eq(modules.id, context.params.idModule)) + .returning(); + + if (!updated) return NOT_FOUND; + + return new Response(JSON.stringify(updated), { + headers: { "content-type": "application/json" }, + }); + }, + + // #27 DELETE /modules/{idModule} + async DELETE( + _request: Request, + context: FreshContext, + ): Promise { + const [deleted] = await db + .delete(modules) + .where(eq(modules.id, context.params.idModule)) + .returning(); + + if (!deleted) return NOT_FOUND; + + return new Response(null, { status: 204 }); + }, +}; -- 2.52.0