import { FreshContext, Handlers } from "$fresh/server.ts"; import { db } from "$root/databases/db.ts"; import { promotions } from "$root/databases/schema.ts"; import { AuthenticatedState, isEmployee } from "$root/defaults/interfaces.ts"; export const handler: Handlers = { // #13 GET /promotions async GET( _request: Request, context: FreshContext, ): Promise { if (!isEmployee(context.state.session)) { return new Response(JSON.stringify([]), { headers: { "content-type": "application/json" }, }); } const rows = await db.select().from(promotions); return new Response(JSON.stringify(rows), { headers: { "content-type": "application/json" }, }); }, // #14 POST /promotions async POST( request: Request, context: FreshContext, ): Promise { if (!isEmployee(context.state.session)) { return new Response(null, { status: 403 }); } 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" }, }); }, };