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@0.45.2"; 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 }); }, };