Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4eaea48ebd | |||
| f959cf0d3a |
@@ -0,0 +1,83 @@
|
||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||
import { db } from "$root/databases/db.ts";
|
||||
import { students } from "$root/databases/schema.ts";
|
||||
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||
import { eq } from "npm:drizzle-orm";
|
||||
|
||||
const NOT_FOUND = new Response(
|
||||
JSON.stringify({ error: "Ressource introuvable" }),
|
||||
{ status: 404, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
|
||||
const FORBIDDEN = new Response(null, { status: 403 });
|
||||
|
||||
export const handler: Handlers<null, AuthenticatedState> = {
|
||||
// #10 GET /students/{numEtud}
|
||||
async GET(
|
||||
_request: Request,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return FORBIDDEN;
|
||||
}
|
||||
|
||||
const numEtud = Number(context.params.numEtud);
|
||||
const student = await db
|
||||
.select()
|
||||
.from(students)
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
|
||||
if (!student) return NOT_FOUND;
|
||||
|
||||
return new Response(JSON.stringify(student), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
},
|
||||
|
||||
// #11 PUT /students/{numEtud}
|
||||
async PUT(
|
||||
request: Request,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return FORBIDDEN;
|
||||
}
|
||||
|
||||
const numEtud = Number(context.params.numEtud);
|
||||
const body: { nom: string; prenom: string; idPromo: string } =
|
||||
await request.json();
|
||||
|
||||
const [updated] = await db
|
||||
.update(students)
|
||||
.set({ nom: body.nom, prenom: body.prenom, idPromo: body.idPromo })
|
||||
.where(eq(students.numEtud, numEtud))
|
||||
.returning();
|
||||
|
||||
if (!updated) return NOT_FOUND;
|
||||
|
||||
return new Response(JSON.stringify(updated), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
},
|
||||
|
||||
// #12 DELETE /students/{numEtud}
|
||||
async DELETE(
|
||||
_request: Request,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
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;
|
||||
|
||||
return new Response(null, { status: 204 });
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||
import { db } from "$root/databases/db.ts";
|
||||
import { students } from "$root/databases/schema.ts";
|
||||
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||
|
||||
// #9 POST /students/import-csv
|
||||
export const handler: Handlers<null, AuthenticatedState> = {
|
||||
async POST(
|
||||
request: Request,
|
||||
context: FreshContext<AuthenticatedState>,
|
||||
): Promise<Response> {
|
||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||
return new Response(null, { status: 403 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const file = formData.get("file") as File | null;
|
||||
const idPromo = formData.get("idPromo") as string | null;
|
||||
|
||||
if (!file || !idPromo) {
|
||||
return new Response(null, { status: 400 });
|
||||
}
|
||||
|
||||
const text = await file.text();
|
||||
const lines = text.trim().split("\n");
|
||||
|
||||
let imported = 0;
|
||||
const errors: { line: number; message: string }[] = [];
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const lineNum = i + 1;
|
||||
const cols = lines[i].split(",").map((c) => c.trim());
|
||||
|
||||
const [numEtudStr, nom, prenom] = cols;
|
||||
|
||||
if (!numEtudStr) {
|
||||
errors.push({ line: lineNum, message: "Numéro étudiant manquant" });
|
||||
continue;
|
||||
}
|
||||
|
||||
const numEtud = Number(numEtudStr);
|
||||
if (isNaN(numEtud)) {
|
||||
errors.push({ line: lineNum, message: "Numéro étudiant invalide" });
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!nom || !prenom) {
|
||||
errors.push({ line: lineNum, message: "Nom ou prénom manquant" });
|
||||
continue;
|
||||
}
|
||||
|
||||
await db
|
||||
.insert(students)
|
||||
.values({ nom, prenom, idPromo })
|
||||
.onConflictDoNothing();
|
||||
|
||||
imported++;
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ imported, errors }), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user