b0930b8da2
- ajustements [numEtud]/[idUE]: fix .where() missing and() — PUT/DELETE were applying only numEtud condition, modifying all rows for a student - modules/users/enseignements POST: add try/catch, return 500 on invalid JSON - modules/[idModule] PUT: add try/catch + type check on nom (string required) - modules POST: add .trim() check to reject whitespace-only id/nom - users POST: add .trim() check to reject whitespace-only id/nom/prenom - ues POST: add .trim() check to reject whitespace-only nom - notes POST: add type check (typeof number) and bounds check (0 ≤ note ≤ 20) - ue-modules POST: add coeff >= 0 validation Update robustness tests to reflect fixed behavior (remove [BUG] labels, replace assertRejects with status code assertions).
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
import { db } from "$root/databases/db.ts";
|
|
import { users } from "$root/databases/schema.ts";
|
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
|
import { eq } from "npm:drizzle-orm@0.45.2";
|
|
|
|
export const handler: Handlers<null, AuthenticatedState> = {
|
|
// #60 GET /users
|
|
async GET(
|
|
request: Request,
|
|
_context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
const url = new URL(request.url);
|
|
const idRole = url.searchParams.get("idRole");
|
|
|
|
const rows = idRole
|
|
? await db.select().from(users).where(eq(users.idRole, Number(idRole)))
|
|
: await db.select().from(users);
|
|
|
|
return new Response(JSON.stringify(rows), {
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
|
|
// #61 POST /users
|
|
async POST(
|
|
request: Request,
|
|
_context: FreshContext<AuthenticatedState>,
|
|
): Promise<Response> {
|
|
let body: { id: string; nom: string; prenom: string; idRole: number };
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return new Response(null, { status: 500 });
|
|
}
|
|
|
|
if (
|
|
!body.id || !body.id.trim() || !body.nom || !body.nom.trim() ||
|
|
!body.prenom || !body.prenom.trim()
|
|
) {
|
|
return new Response(null, { status: 400 });
|
|
}
|
|
|
|
const existing = await db
|
|
.select()
|
|
.from(users)
|
|
.where(eq(users.id, body.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (existing) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Un utilisateur avec cet identifiant existe déjà",
|
|
}),
|
|
{ status: 409, headers: { "content-type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const [created] = await db
|
|
.insert(users)
|
|
.values({
|
|
id: body.id,
|
|
nom: body.nom,
|
|
prenom: body.prenom,
|
|
idRole: body.idRole,
|
|
})
|
|
.returning();
|
|
|
|
return new Response(JSON.stringify(created), {
|
|
status: 201,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
},
|
|
};
|