Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 934a160889 | |||
| 2185143b8d | |||
| 1557c9752c |
@@ -1,63 +0,0 @@
|
|||||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
||||||
import { db } from "$root/databases/db.ts";
|
|
||||||
import { modules } from "$root/databases/schema.ts";
|
|
||||||
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
|
||||||
import { eq } from "npm:drizzle-orm";
|
|
||||||
|
|
||||||
export const handler: Handlers<null, AuthenticatedState> = {
|
|
||||||
// #23 GET /modules
|
|
||||||
async GET(
|
|
||||||
_request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
||||||
return new Response(JSON.stringify([]), {
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const rows = await db.select().from(modules);
|
|
||||||
return new Response(JSON.stringify(rows), {
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// #24 POST /modules
|
|
||||||
async POST(
|
|
||||||
request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
||||||
return new Response(null, { status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const body: { id: string; nom: string } = await request.json();
|
|
||||||
|
|
||||||
if (!body.id || !body.nom) {
|
|
||||||
return new Response(null, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const existing = await db
|
|
||||||
.select()
|
|
||||||
.from(modules)
|
|
||||||
.where(eq(modules.id, body.id))
|
|
||||||
.then((rows) => rows[0] ?? null);
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({ error: "Un module avec cet identifiant existe déjà" }),
|
|
||||||
{ status: 409, headers: { "content-type": "application/json" } },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [created] = await db
|
|
||||||
.insert(modules)
|
|
||||||
.values({ id: body.id, nom: body.nom })
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(created), {
|
|
||||||
status: 201,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
|
||||||
import { db } from "$root/databases/db.ts";
|
|
||||||
import { modules } 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" } },
|
|
||||||
);
|
|
||||||
|
|
||||||
export const handler: Handlers<null, AuthenticatedState> = {
|
|
||||||
// #25 GET /modules/{idModule}
|
|
||||||
async GET(
|
|
||||||
_request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
const module = await db
|
|
||||||
.select()
|
|
||||||
.from(modules)
|
|
||||||
.where(eq(modules.id, context.params.idModule))
|
|
||||||
.then((rows) => rows[0] ?? null);
|
|
||||||
|
|
||||||
if (!module) return NOT_FOUND;
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(module), {
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// #26 PUT /modules/{idModule}
|
|
||||||
async PUT(
|
|
||||||
request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
const body: { nom: string } = await request.json();
|
|
||||||
|
|
||||||
const [updated] = await db
|
|
||||||
.update(modules)
|
|
||||||
.set({ nom: body.nom })
|
|
||||||
.where(eq(modules.id, context.params.idModule))
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
if (!updated) return NOT_FOUND;
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(updated), {
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// #27 DELETE /modules/{idModule}
|
|
||||||
async DELETE(
|
|
||||||
_request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
const [deleted] = await db
|
|
||||||
.delete(modules)
|
|
||||||
.where(eq(modules.id, context.params.idModule))
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
if (!deleted) return NOT_FOUND;
|
|
||||||
|
|
||||||
return new Response(null, { status: 204 });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
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";
|
|
||||||
|
|
||||||
export const handler: Handlers<null, AuthenticatedState> = {
|
|
||||||
// #48 GET /ajustements
|
|
||||||
async GET(request) {
|
|
||||||
try {
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const numEtudParam = url.searchParams.get("numEtud");
|
|
||||||
const idUEParam = url.searchParams.get("idUE");
|
|
||||||
|
|
||||||
let query = db.select().from(ajustements).$dynamic();
|
|
||||||
|
|
||||||
if (numEtudParam) {
|
|
||||||
const numEtud = parseInt(numEtudParam);
|
|
||||||
if (isNaN(numEtud)) {
|
|
||||||
return new Response("Paramètre numEtud invalide", { status: 400 });
|
|
||||||
}
|
|
||||||
query = query.where(eq(ajustements.numEtud, numEtud));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (idUEParam) {
|
|
||||||
const idUE = parseInt(idUEParam);
|
|
||||||
if (isNaN(idUE)) {
|
|
||||||
return new Response("Paramètre idUE invalide", { status: 400 });
|
|
||||||
}
|
|
||||||
query = query.where(eq(ajustements.idUE, idUE));
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await query;
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(result), {
|
|
||||||
status: 200,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching ajustements:", error);
|
|
||||||
return new Response("Failed to fetch data", { status: 500 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// #49 POST /ajustements
|
|
||||||
async POST(
|
|
||||||
request: Request,
|
|
||||||
context: FreshContext<AuthenticatedState>,
|
|
||||||
): Promise<Response> {
|
|
||||||
if (context.state.session.eduPersonPrimaryAffiliation !== "employee") {
|
|
||||||
return new Response(null, { status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const body: { numEtud: number; idUE: number; valeur: number } =
|
|
||||||
await request.json();
|
|
||||||
|
|
||||||
if (!body.numEtud || !body.idUE || body.valeur === undefined) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({ error: "Champs requis: numEtud, idUE, valeur" }),
|
|
||||||
{ status: 400, headers: { "content-type": "application/json" } },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const [created] = await db
|
|
||||||
.insert(ajustements)
|
|
||||||
.values({
|
|
||||||
numEtud: body.numEtud,
|
|
||||||
idUE: body.idUE,
|
|
||||||
valeur: body.valeur,
|
|
||||||
})
|
|
||||||
.returning();
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(created), {
|
|
||||||
status: 201,
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error creating ajustement:", error);
|
|
||||||
return new Response("Failed to create ajustement", { status: 500 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -36,26 +36,4 @@ export const handler: Handlers = {
|
|||||||
return new Response("Failed to fetch data", { status: 500 });
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// #43 POST /notes
|
|
||||||
async POST(request) {
|
|
||||||
try {
|
|
||||||
const body = await request.json();
|
|
||||||
const { note, numEtud, idModule } = body;
|
|
||||||
|
|
||||||
if (note === undefined || !numEtud || !idModule) {
|
|
||||||
return new Response("Champs 'note', 'numEtud' et 'idModule' requis", { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await db.insert(notes).values({ note, numEtud, idModule }).returning();
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(result[0]), {
|
|
||||||
status: 201,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error creating note:", error);
|
|
||||||
return new Response("Failed to create note", { status: 500 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
|
||||||
import { db } from "../../../../databases/db.ts";
|
|
||||||
import { ueModules } from "../../../../databases/schema.ts";
|
|
||||||
import { and, eq } from "npm:drizzle-orm";
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
|
||||||
// #37 GET /ue-modules
|
|
||||||
async GET(request) {
|
|
||||||
try {
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const idPromo = url.searchParams.get("idPromo");
|
|
||||||
const idUEParam = url.searchParams.get("idUE");
|
|
||||||
|
|
||||||
const idUE = idUEParam ? parseInt(idUEParam) : null;
|
|
||||||
|
|
||||||
if (idUEParam && isNaN(idUE!)) {
|
|
||||||
return new Response("Paramètre idUE invalide", { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await db.select().from(ueModules).where(
|
|
||||||
and(
|
|
||||||
idPromo ? eq(ueModules.idPromo, idPromo) : undefined,
|
|
||||||
idUE ? eq(ueModules.idUE, idUE) : undefined,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return new Response(JSON.stringify(result), {
|
|
||||||
status: 200,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching UE-modules:", error);
|
|
||||||
return new Response("Failed to fetch data", { status: 500 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
|
||||||
import { db } from "../../../../../databases/db.ts";
|
|
||||||
import { ues } from "../../../../../databases/schema.ts";
|
|
||||||
import { eq } from "npm:drizzle-orm";
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
|
||||||
// # 34 GET /ues/:idUE
|
|
||||||
async GET(_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.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" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching UE:", error);
|
|
||||||
return new Response("Failed to fetch data", { status: 500 });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user