Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 66183c2ad8 | |||
| 9976b9e2b4 | |||
| 457b008ba3 | |||
| 22750ba07e | |||
| 49876339bf | |||
| eeb087ea76 | |||
| 7ad70c4525 | |||
| 79669d60cf | |||
| d3f1f433e1 |
@@ -0,0 +1,107 @@
|
|||||||
|
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||||
|
import { db } from "$root/databases/db.ts";
|
||||||
|
import { ajustements } 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: "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 (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
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(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 (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
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 } = await request.json();
|
||||||
|
|
||||||
|
if (body.valeur === undefined) {
|
||||||
|
return new Response(JSON.stringify({ error: "Champ requis: valeur" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [updated] = await db
|
||||||
|
.update(ajustements)
|
||||||
|
.set({ valeur: body.valeur })
|
||||||
|
.where(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 (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
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(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!deleted) return NOT_FOUND;
|
||||||
|
|
||||||
|
return new Response(null, { status: 204 });
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import { db } from "../../../../../../databases/db.ts";
|
||||||
|
import { notes } from "../../../../../../databases/schema.ts";
|
||||||
|
import { and, eq } from "npm:drizzle-orm";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
// #45 GET /notes/:numEtud/:idModule
|
||||||
|
async GET(_request, context) {
|
||||||
|
try {
|
||||||
|
const numEtud = parseInt(context.params.numEtud);
|
||||||
|
const { idModule } = context.params;
|
||||||
|
|
||||||
|
if (isNaN(numEtud)) {
|
||||||
|
return new Response(JSON.stringify({ error: "Paramètre numEtud invalide" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await db.select().from(notes).where(
|
||||||
|
and(
|
||||||
|
eq(notes.numEtud, numEtud),
|
||||||
|
eq(notes.idModule, idModule),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
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" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching note:", error);
|
||||||
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// #46 PUT /notes/:numEtud/:idModule
|
||||||
|
async PUT(request, context) {
|
||||||
|
try {
|
||||||
|
const numEtud = parseInt(context.params.numEtud);
|
||||||
|
const { idModule } = context.params;
|
||||||
|
|
||||||
|
if (isNaN(numEtud)) {
|
||||||
|
return new Response(JSON.stringify({ error: "Paramètre numEtud invalide" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
const { note } = body;
|
||||||
|
|
||||||
|
if (note === undefined) {
|
||||||
|
return new Response("Champ 'note' manquant", { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await db.update(notes).set({ note }).where(
|
||||||
|
and(
|
||||||
|
eq(notes.numEtud, numEtud),
|
||||||
|
eq(notes.idModule, idModule),
|
||||||
|
),
|
||||||
|
).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" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating note:", error);
|
||||||
|
return new Response("Failed to update note", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// #47 DELETE /notes/:numEtud/:idModule
|
||||||
|
async DELETE(_request, context) {
|
||||||
|
try {
|
||||||
|
const numEtud = parseInt(context.params.numEtud);
|
||||||
|
const { idModule } = context.params;
|
||||||
|
|
||||||
|
if (isNaN(numEtud)) {
|
||||||
|
return new Response(JSON.stringify({ error: "Paramètre numEtud invalide" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await db.delete(notes).where(
|
||||||
|
and(
|
||||||
|
eq(notes.numEtud, numEtud),
|
||||||
|
eq(notes.idModule, idModule),
|
||||||
|
),
|
||||||
|
).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 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error deleting note:", error);
|
||||||
|
return new Response("Failed to delete note", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -33,4 +33,26 @@ export const handler: Handlers = {
|
|||||||
return new Response("Failed to fetch data", { status: 500 });
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// #38 POST /ue-modules
|
||||||
|
async POST(request) {
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { idModule, idUE, idPromo, coeff } = body;
|
||||||
|
|
||||||
|
if (!idModule || !idUE || !idPromo || coeff === undefined) {
|
||||||
|
return new Response("Champs 'idModule', 'idUE', 'idPromo' et 'coeff' requis", { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await db.insert(ueModules).values({ idModule, idUE, idPromo, coeff }).returning();
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(result[0]), {
|
||||||
|
status: 201,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error creating UE-module:", error);
|
||||||
|
return new Response("Failed to create UE-module", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||||
|
import { db } from "$root/databases/db.ts";
|
||||||
|
import { ueModules } from "$root/databases/schema.ts";
|
||||||
|
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||||
|
import { and, eq } from "npm:drizzle-orm@0.45.2";
|
||||||
|
|
||||||
|
const NOT_FOUND = new Response(
|
||||||
|
JSON.stringify({ error: "Association UE-Module introuvable" }),
|
||||||
|
{ status: 404, headers: { "content-type": "application/json" } },
|
||||||
|
);
|
||||||
|
|
||||||
|
const FORBIDDEN = new Response(null, { status: 403 });
|
||||||
|
|
||||||
|
const BAD_REQUEST = new Response(
|
||||||
|
JSON.stringify({ error: "Paramètres invalides" }),
|
||||||
|
{ status: 400, headers: { "content-type": "application/json" } },
|
||||||
|
);
|
||||||
|
|
||||||
|
export const handler: Handlers<null, AuthenticatedState> = {
|
||||||
|
// #39 GET /ue-modules/{idModule}/{idUE}/{idPromo}
|
||||||
|
async GET(
|
||||||
|
_request: Request,
|
||||||
|
context: FreshContext<AuthenticatedState>,
|
||||||
|
): Promise<Response> {
|
||||||
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
return FORBIDDEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idModule = context.params.idModule;
|
||||||
|
const idUE = Number(context.params.idUE);
|
||||||
|
const idPromo = context.params.idPromo;
|
||||||
|
|
||||||
|
if (isNaN(idUE)) {
|
||||||
|
return BAD_REQUEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ueModuleAssociation = await db
|
||||||
|
.select()
|
||||||
|
.from(ueModules)
|
||||||
|
.where(
|
||||||
|
eq(ueModules.idModule, idModule),
|
||||||
|
eq(ueModules.idUE, idUE),
|
||||||
|
eq(ueModules.idPromo, idPromo),
|
||||||
|
)
|
||||||
|
.then((rows) => rows[0] ?? null);
|
||||||
|
|
||||||
|
if (!ueModuleAssociation) return NOT_FOUND;
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(ueModuleAssociation), {
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// #40 PUT /ue-modules/{idModule}/{idUE}/{idPromo}
|
||||||
|
async PUT(
|
||||||
|
request: Request,
|
||||||
|
context: FreshContext<AuthenticatedState>,
|
||||||
|
): Promise<Response> {
|
||||||
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
return FORBIDDEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idModule = context.params.idModule;
|
||||||
|
const idUE = Number(context.params.idUE);
|
||||||
|
const idPromo = context.params.idPromo;
|
||||||
|
|
||||||
|
if (isNaN(idUE)) {
|
||||||
|
return BAD_REQUEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
const body: { coeff: number } = await request.json();
|
||||||
|
|
||||||
|
if (typeof body.coeff !== "number") {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({ error: "Le champ 'coeff' doit être un nombre" }),
|
||||||
|
{ status: 400, headers: { "content-type": "application/json" } },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [updated] = await db
|
||||||
|
.update(ueModules)
|
||||||
|
.set({ coeff: body.coeff })
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(ueModules.idModule, idModule),
|
||||||
|
eq(ueModules.idUE, idUE),
|
||||||
|
eq(ueModules.idPromo, idPromo),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!updated) return NOT_FOUND;
|
||||||
|
|
||||||
|
return new Response(JSON.stringify({
|
||||||
|
idModule: updated.idModule,
|
||||||
|
idUE: updated.idUE,
|
||||||
|
idPromo: updated.idPromo,
|
||||||
|
coeff: updated.coeff,
|
||||||
|
}), {
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// #41 DELETE /ue-modules/{idModule}/{idUE}/{idPromo}
|
||||||
|
async DELETE(
|
||||||
|
_request: Request,
|
||||||
|
context: FreshContext<AuthenticatedState>,
|
||||||
|
): Promise<Response> {
|
||||||
|
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
||||||
|
return FORBIDDEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
const idModule = context.params.idModule;
|
||||||
|
const idUE = Number(context.params.idUE);
|
||||||
|
const idPromo = context.params.idPromo;
|
||||||
|
|
||||||
|
if (isNaN(idUE)) {
|
||||||
|
return BAD_REQUEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [deleted] = await db
|
||||||
|
.delete(ueModules)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(ueModules.idModule, idModule),
|
||||||
|
eq(ueModules.idUE, idUE),
|
||||||
|
eq(ueModules.idPromo, idPromo),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!deleted) return NOT_FOUND;
|
||||||
|
|
||||||
|
return new Response(null, { status: 204 });
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -34,4 +34,70 @@ export const handler: Handlers = {
|
|||||||
return new Response("Failed to fetch data", { status: 500 });
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// #35 PUT /ues/:idUE
|
||||||
|
async PUT(request, context) {
|
||||||
|
try {
|
||||||
|
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" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating UE:", error);
|
||||||
|
return new Response("Failed to update UE", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// #36 DELETE /ues/:idUE
|
||||||
|
async DELETE(_request, context) {
|
||||||
|
try {
|
||||||
|
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 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error deleting UE:", error);
|
||||||
|
return new Response("Failed to delete UE", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user