mirror of
https://github.com/skidoodle/budgetable.git
synced 2025-02-15 03:39:14 +01:00
i forgor
This commit is contained in:
parent
bcfa0f92a6
commit
dedb1c0628
16 changed files with 316 additions and 239 deletions
|
@ -12,7 +12,7 @@ async function authenticateSuperuser() {
|
|||
}
|
||||
|
||||
export async function GET(
|
||||
req: Request,
|
||||
_req: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
|
@ -20,27 +20,36 @@ export async function GET(
|
|||
|
||||
const id = (await context.params)?.id;
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Missing ID in request" } }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Missing ID in request",
|
||||
},
|
||||
}, {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const record = await pb.collection("budgetable").getOne(id);
|
||||
return new Response(JSON.stringify(record), {
|
||||
return Response.json(record, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Failed to fetch data" } }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Failed to fetch data",
|
||||
},
|
||||
}, {
|
||||
status: 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
req: Request,
|
||||
_req: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
|
@ -48,22 +57,33 @@ export async function DELETE(
|
|||
|
||||
const id = (await context.params)?.id;
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Missing ID in request" } }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Missing ID in request",
|
||||
},
|
||||
}, {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
await pb.collection("budgetable").delete(id);
|
||||
return new Response(JSON.stringify({ success: true }), {
|
||||
return Response.json({
|
||||
success: true,
|
||||
}, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error deleting data:", error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Failed to delete data" } }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Failed to delete data",
|
||||
},
|
||||
}, {
|
||||
status: 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,31 +94,44 @@ export async function PUT(
|
|||
try {
|
||||
await authenticateSuperuser();
|
||||
|
||||
const id = (await context.params)?.id; // Use `context.params?.id`
|
||||
const id = (await context.params)?.id;
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Missing ID in request" } }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Missing ID in request",
|
||||
},
|
||||
}, {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const body = await req.json();
|
||||
if (!body.title || typeof body.price !== "number") {
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Invalid data provided" } }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Invalid data provided",
|
||||
},
|
||||
}, {
|
||||
status: 400,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const updatedRecord = await pb.collection("budgetable").update(id, body);
|
||||
return new Response(JSON.stringify(updatedRecord), {
|
||||
return Response.json(updatedRecord, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error updating data:", error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Failed to update data" } }),
|
||||
{ status: 500, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
return Response.json({
|
||||
error: {
|
||||
message: "Failed to update data",
|
||||
},
|
||||
}, {
|
||||
status: 500,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue