feat(api): implement UE-Module association get endpoint
- GET /ue-modules/{idModule}/{idUE}/{idPromo}: recover the detail of an
ue-module association by its composite key
- requires employee role
This commit was merged in pull request #133.
This commit is contained in:
@@ -0,0 +1,53 @@
|
|||||||
|
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||||
|
import { db } from "$root/databases/db.ts";
|
||||||
|
import { ueModules } 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: "Association UE-Module introuvable" }),
|
||||||
|
{ status: 404, headers: { "content-type": "application/json" } },
|
||||||
|
);
|
||||||
|
|
||||||
|
const FORBIDDEN = new Response(null, { status: 403 });
|
||||||
|
|
||||||
|
const BAD_REQUEST = new Response(
|
||||||
|
JSON.stringify({ error: "Paramètres invalides" }),
|
||||||
|
{ status: 400, headers: { "content-type": "application/json" } },
|
||||||
|
);
|
||||||
|
|
||||||
|
export const handler: Handlers<null, AuthenticatedState> = {
|
||||||
|
// #39 GET /ue-modules/{idModule}/{idUE}/{idPromo}
|
||||||
|
async GET(
|
||||||
|
_request: Request,
|
||||||
|
context: FreshContext<AuthenticatedState>,
|
||||||
|
): Promise<Response> {
|
||||||
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
return FORBIDDEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idModule = context.params.idModule;
|
||||||
|
const idUE = Number(context.params.idUE);
|
||||||
|
const idPromo = context.params.idPromo;
|
||||||
|
|
||||||
|
if (isNaN(idUE)) {
|
||||||
|
return BAD_REQUEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ueModuleAssociation = await db
|
||||||
|
.select()
|
||||||
|
.from(ueModules)
|
||||||
|
.where(
|
||||||
|
eq(ueModules.idModule, idModule),
|
||||||
|
eq(ueModules.idUE, idUE),
|
||||||
|
eq(ueModules.idPromo, idPromo),
|
||||||
|
)
|
||||||
|
.then((rows) => rows[0] ?? null);
|
||||||
|
|
||||||
|
if (!ueModuleAssociation) return NOT_FOUND;
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(ueModuleAssociation), {
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user