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 });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user