feat : fix a lot of stuff
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||
import { db } from "$root/databases/db.ts";
|
||||
import { students } from "$root/databases/schema.ts";
|
||||
import {
|
||||
ajustements,
|
||||
mobility,
|
||||
notes,
|
||||
students,
|
||||
} 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: "Ressource introuvable" }),
|
||||
{ status: 404, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
const NOT_FOUND = () =>
|
||||
new Response(
|
||||
JSON.stringify({ error: "Ressource introuvable" }),
|
||||
{ status: 404, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
|
||||
const FORBIDDEN = new Response(null, { status: 403 });
|
||||
const FORBIDDEN = () => new Response(null, { status: 403 });
|
||||
|
||||
export const handler: Handlers<null, AuthenticatedState> = {
|
||||
// #10 GET /students/{numEtud}
|
||||
@@ -18,7 +24,7 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return FORBIDDEN;
|
||||
return FORBIDDEN();
|
||||
}
|
||||
|
||||
const numEtud = Number(context.params.numEtud);
|
||||
@@ -28,7 +34,7 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
|
||||
if (!student) return NOT_FOUND;
|
||||
if (!student) return NOT_FOUND();
|
||||
|
||||
return new Response(JSON.stringify(student), {
|
||||
headers: { "content-type": "application/json" },
|
||||
@@ -41,20 +47,32 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return FORBIDDEN;
|
||||
return FORBIDDEN();
|
||||
}
|
||||
|
||||
const numEtud = Number(context.params.numEtud);
|
||||
const body: { nom: string; prenom: string; idPromo: string } = await request
|
||||
.json();
|
||||
const body: { nom?: string; prenom?: string; idPromo?: string } =
|
||||
await request.json();
|
||||
|
||||
const set: { nom?: string; prenom?: string; idPromo?: string } = {};
|
||||
if (body.nom !== undefined) set.nom = body.nom;
|
||||
if (body.prenom !== undefined) set.prenom = body.prenom;
|
||||
if (body.idPromo !== undefined) set.idPromo = body.idPromo;
|
||||
|
||||
if (Object.keys(set).length === 0) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: "Au moins un champ requis" }),
|
||||
{ status: 400, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
}
|
||||
|
||||
const [updated] = await db
|
||||
.update(students)
|
||||
.set({ nom: body.nom, prenom: body.prenom, idPromo: body.idPromo })
|
||||
.set(set)
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.returning();
|
||||
|
||||
if (!updated) return NOT_FOUND;
|
||||
if (!updated) return NOT_FOUND();
|
||||
|
||||
return new Response(JSON.stringify(updated), {
|
||||
headers: { "content-type": "application/json" },
|
||||
@@ -62,21 +80,31 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
},
|
||||
|
||||
// #12 DELETE /students/{numEtud}
|
||||
// Cascade: deletes notes, ajustements, mobility for this student.
|
||||
async DELETE(
|
||||
_request: Request,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return FORBIDDEN;
|
||||
return FORBIDDEN();
|
||||
}
|
||||
|
||||
const numEtud = Number(context.params.numEtud);
|
||||
const [deleted] = await db
|
||||
.delete(students)
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.returning();
|
||||
|
||||
if (!deleted) return NOT_FOUND;
|
||||
const student = await db
|
||||
.select()
|
||||
.from(students)
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.then((r) => r[0] ?? null);
|
||||
|
||||
if (!student) return NOT_FOUND();
|
||||
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.delete(notes).where(eq(notes.numEtud, numEtud));
|
||||
await tx.delete(ajustements).where(eq(ajustements.numEtud, numEtud));
|
||||
await tx.delete(mobility).where(eq(mobility.studentId, numEtud));
|
||||
await tx.delete(students).where(eq(students.numEtud, numEtud));
|
||||
});
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user