9368e68622
refactor: add withRules wrapper to API routes Use withRules to enforce permissions instead of manual checks. Remove FORBIDDEN constant, simplify handlers, import withRules, adjust GET/POST/PUT/DELETE handlers. Centralizes auth logic. refactor: replace manual auth checks with withRules wrapper for routes refactor(student routes): replace manual employee checks with withRules wrapper
91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
import { Handlers } from "$fresh/server.ts";
|
|
import { db } from "../../../../../databases/db.ts";
|
|
import { ues } from "../../../../../databases/schema.ts";
|
|
import { withRules } from "$root/defaults/withRules.ts";
|
|
import { eq } from "npm:drizzle-orm@0.45.2";
|
|
|
|
export const handler: Handlers = {
|
|
// #34 GET /ues/:idUE
|
|
GET: withRules(["note_read"])(async (_request, context) => {
|
|
const idUE = parseInt(context.params.idUE);
|
|
|
|
if (isNaN(idUE)) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Paramètre idUE invalide" }),
|
|
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const result = await db.select().from(ues).where(eq(ues.id, idUE));
|
|
|
|
if (result.length === 0) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Ressource introuvable" }),
|
|
{ status: 404, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
return new Response(JSON.stringify(result[0]), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}),
|
|
|
|
// #35 PUT /ues/:idUE
|
|
PUT: withRules(["note_write"])(async (request, context) => {
|
|
const idUE = parseInt(context.params.idUE);
|
|
|
|
if (isNaN(idUE)) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Paramètre idUE invalide" }),
|
|
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { nom } = body;
|
|
|
|
if (!nom) {
|
|
return new Response("Champ 'nom' manquant", { status: 400 });
|
|
}
|
|
|
|
const result = await db.update(ues).set({ nom }).where(eq(ues.id, idUE))
|
|
.returning();
|
|
|
|
if (result.length === 0) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Ressource introuvable" }),
|
|
{ status: 404, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
return new Response(JSON.stringify(result[0]), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}),
|
|
|
|
// #36 DELETE /ues/:idUE
|
|
DELETE: withRules(["note_write"])(async (_request, context) => {
|
|
const idUE = parseInt(context.params.idUE);
|
|
|
|
if (isNaN(idUE)) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Paramètre idUE invalide" }),
|
|
{ status: 400, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
const result = await db.delete(ues).where(eq(ues.id, idUE)).returning();
|
|
|
|
if (result.length === 0) {
|
|
return new Response(
|
|
JSON.stringify({ error: "Ressource introuvable" }),
|
|
{ status: 404, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
return new Response(null, { status: 204 });
|
|
}),
|
|
};
|