refactor(notes): improve error handling and formatting
docs: update CLAUDE.md formatting
This commit is contained in:
@@ -34,28 +34,30 @@ export const handler: Handlers = {
|
||||
}),
|
||||
|
||||
// #43 POST /notes
|
||||
POST: withRules(["note_write", "own_teaching_note"])(async (request, _context) => {
|
||||
const body = await request.json();
|
||||
const { note, numEtud, idModule } = body;
|
||||
POST: withRules(["note_write", "own_teaching_note"])(
|
||||
async (request, _context) => {
|
||||
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,
|
||||
if (note === undefined || !numEtud || !idModule) {
|
||||
return new Response("Champs 'note', 'numEtud' et 'idModule' requis", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof note !== "number" || note < 0 || note > 20) {
|
||||
return new Response("Champ 'note' doit être un nombre entre 0 et 20", {
|
||||
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" },
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof note !== "number" || note < 0 || note > 20) {
|
||||
return new Response("Champ 'note' doit être un nombre entre 0 et 20", {
|
||||
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" },
|
||||
});
|
||||
}),
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
@@ -6,101 +6,101 @@ import { and, eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
export const handler: Handlers = {
|
||||
// #45 GET /notes/:numEtud/:idModule
|
||||
GET: withRules(["note_read", "own_note", "own_teaching_note"])(async (_request, context) => {
|
||||
try {
|
||||
const numEtud = parseInt(context.params.numEtud);
|
||||
const { idModule } = context.params;
|
||||
GET: withRules(["note_read", "own_note", "own_teaching_note"])(
|
||||
async (_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" },
|
||||
},
|
||||
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 });
|
||||
}
|
||||
|
||||
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
|
||||
PUT: withRules(["note_write", "own_teaching_note"])(async (request, context) => {
|
||||
try {
|
||||
const numEtud = parseInt(context.params.numEtud);
|
||||
const { idModule } = context.params;
|
||||
PUT: withRules(["note_write", "own_teaching_note"])(
|
||||
async (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" },
|
||||
},
|
||||
);
|
||||
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 });
|
||||
}
|
||||
|
||||
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
|
||||
DELETE: withRules(["note_write"])(async (_request, context) => {
|
||||
|
||||
Reference in New Issue
Block a user