mirror of
https://github.com/skidoodle/budgetable.git
synced 2025-02-15 03:39:14 +01:00
Refactor: Break down single table component into multiple modular components
This commit is contained in:
parent
8c9688017b
commit
95380493cb
33 changed files with 1562 additions and 6642 deletions
|
@ -1,94 +1,104 @@
|
|||
import pb from "@/app/lib/pocketbase";
|
||||
import pb from "@/lib/pocketbase";
|
||||
|
||||
const { EMAIL, PASSWORD } = process.env;
|
||||
|
||||
async function authenticateSuperuser() {
|
||||
if (!pb.authStore.isValid) {
|
||||
await pb.collection("_superusers").authWithPassword(EMAIL!, PASSWORD!);
|
||||
}
|
||||
if (!EMAIL || !PASSWORD) {
|
||||
throw new Error("Environment variables EMAIL and PASSWORD must be set");
|
||||
}
|
||||
if (!pb.authStore.isValid) {
|
||||
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
export async function GET(
|
||||
req: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
|
||||
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" } }
|
||||
);
|
||||
}
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
|
||||
const record = await pb.collection("budgetable").getOne(id);
|
||||
return new Response(JSON.stringify(record), {
|
||||
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" } }
|
||||
);
|
||||
}
|
||||
const record = await pb.collection("budgetable").getOne(id);
|
||||
return new Response(JSON.stringify(record), {
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
export async function DELETE(
|
||||
req: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
|
||||
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" } }
|
||||
);
|
||||
}
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
|
||||
await pb.collection("budgetable").delete(id);
|
||||
return new Response(
|
||||
JSON.stringify({ success: true }),
|
||||
{ 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" } }
|
||||
);
|
||||
}
|
||||
await pb.collection("budgetable").delete(id);
|
||||
return new Response(JSON.stringify({ success: true }), {
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
export async function PUT(
|
||||
req: Request,
|
||||
context: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
await authenticateSuperuser();
|
||||
|
||||
const id = (await context.params)?.id; // Use `context.params?.id`
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: { message: "Missing ID in request" } }),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
const id = (await context.params)?.id; // Use `context.params?.id`
|
||||
if (!id) {
|
||||
return new Response(
|
||||
JSON.stringify({ 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" } }
|
||||
);
|
||||
}
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
|
||||
const updatedRecord = await pb.collection("budgetable").update(id, body);
|
||||
return new Response(
|
||||
JSON.stringify(updatedRecord),
|
||||
{ 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" } }
|
||||
);
|
||||
}
|
||||
const updatedRecord = await pb.collection("budgetable").update(id, body);
|
||||
return new Response(JSON.stringify(updatedRecord), {
|
||||
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" } },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue