import { Handlers } from "$fresh/server.ts"; import { db } from "$root/databases/db.ts"; import { promotions } from "$root/databases/schema.ts"; import { AuthenticatedState } from "$root/defaults/interfaces.ts"; import { withRules } from "$root/defaults/withRules.ts"; export const handler: Handlers = { // #13 GET /promotions GET: withRules(["student_read"])(async (_request, _context) => { const rows = await db.select().from(promotions); return new Response(JSON.stringify(rows), { headers: { "content-type": "application/json" }, }); }), // #14 POST /promotions POST: withRules(["student_write"])(async (request, _context) => { const body: { idPromo: string; annee: string } = await request.json(); if (!body.idPromo || !body.annee) { return new Response(null, { status: 400 }); } const [created] = await db .insert(promotions) .values({ id: body.idPromo, annee: body.annee }) .returning(); return new Response(JSON.stringify(created), { status: 201, headers: { "content-type": "application/json" }, }); }), };