From 3f0c8d079f149ff09b86549c5b025462219b9681 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Wed, 22 Apr 2026 14:00:05 +0200 Subject: [PATCH] feat(students): add promotions API for employees --- routes/(apps)/students/api/promotions.ts | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 routes/(apps)/students/api/promotions.ts 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" }, + }); + }, +}; -- 2.52.0