124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
import { db } from "$root/databases/db.ts";
|
|
import { ajustements } from "$root/databases/schema.ts";
|
|
import { AuthenticatedState, isEmployee } from "$root/defaults/interfaces.ts";
|
|
import { and, eq } from "npm:drizzle-orm@0.45.2";
|
|
|
|
const NOT_FOUND = () =>
|
|
new Response(
|
|
JSON.stringify({ error: "Ajustement introuvable" }),
|
|
{ status: 404, headers: { "content-type": "application/json" } },
|
|
);
|
|
|
|
const FORBIDDEN = () => new Response(null, { status: 403 });
|
|
|
|
export const handler: Handlers<null, AuthenticatedState> = {
|
|
// #50 GET /ajustements/{numEtud}/{idUE}
|
|
async GET(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (!isEmployee(context.state.session)) {
|
|
return FORBIDDEN();
|
|
}
|
|
|
|
const numEtud = Number(context.params.numEtud);
|
|
const idUE = Number(context.params.idUE);
|
|
|
|
if (isNaN(numEtud) || isNaN(idUE)) {
|
|
return new Response("Paramètres invalides", { status: 400 });
|
|
}
|
|
|
|
const ajustement = await db
|
|
.select()
|
|
.from(ajustements)
|
|
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!ajustement) return NOT_FOUND();
|
|
|
|
return new Response(JSON.stringify(ajustement), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #51 PUT /ajustements/{numEtud}/{idUE}
|
|
async PUT(
|
|
request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (!isEmployee(context.state.session)) {
|
|
return FORBIDDEN();
|
|
}
|
|
|
|
const numEtud = Number(context.params.numEtud);
|
|
const idUE = Number(context.params.idUE);
|
|
|
|
if (isNaN(numEtud) || isNaN(idUE)) {
|
|
return new Response("Paramètres invalides", { status: 400 });
|
|
}
|
|
|
|
const body: { valeur: number; malus?: number } = await request.json();
|
|
|
|
if (body.valeur === undefined) {
|
|
return new Response(JSON.stringify({ error: "Champ requis: valeur" }), {
|
|
status: 400,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
if (
|
|
body.malus !== undefined &&
|
|
(!Number.isInteger(body.malus) || body.malus < 0)
|
|
) {
|
|
return new Response(
|
|
JSON.stringify({ error: "malus doit être un entier >= 0" }),
|
|
{ status: 400, headers: { "content-type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const set: { valeur: number; malus?: number } = { valeur: body.valeur };
|
|
if (body.malus !== undefined) {
|
|
set.malus = body.malus;
|
|
}
|
|
|
|
const [updated] = await db
|
|
.update(ajustements)
|
|
.set(set)
|
|
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
|
.returning();
|
|
|
|
if (!updated) return NOT_FOUND();
|
|
|
|
return new Response(JSON.stringify(updated), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #52 DELETE /ajustements/{numEtud}/{idUE}
|
|
async DELETE(
|
|
_request: Request,
|
|
context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
if (!isEmployee(context.state.session)) {
|
|
return FORBIDDEN();
|
|
}
|
|
|
|
const numEtud = Number(context.params.numEtud);
|
|
const idUE = Number(context.params.idUE);
|
|
|
|
if (isNaN(numEtud) || isNaN(idUE)) {
|
|
return new Response("Paramètres invalides", { status: 400 });
|
|
}
|
|
|
|
const [deleted] = await db
|
|
.delete(ajustements)
|
|
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
|
.returning();
|
|
|
|
if (!deleted) return NOT_FOUND();
|
|
|
|
return new Response(null, { status: 204 });
|
|
},
|
|
};
|