diff --git a/routes/(apps)/students/api/promotions.ts b/routes/(apps)/students/api/promotions.ts new file mode 100644 index 0000000..83b318c --- /dev/null +++ b/routes/(apps)/students/api/promotions.ts @@ -0,0 +1,50 @@ +import { FreshContext, 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 { eq } from "npm:drizzle-orm"; + +export const handler: Handlers = { + // #13 GET /promotions + async GET( + _request: Request, + context: FreshContext, + ): Promise { + if (context.state.session.eduPersonPrimaryAffiliation !== "employee") { + 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 (context.state.session.eduPersonPrimaryAffiliation !== "employee") { + 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" }, + }); + }, +};