mirror of
https://github.com/skidoodle/budgetable.git
synced 2025-02-15 03:39:14 +01:00
feat: get collection from env
This commit is contained in:
parent
fb7740dae0
commit
eda85a3292
2 changed files with 187 additions and 187 deletions
|
@ -1,161 +1,161 @@
|
||||||
import pb from "@/lib/pocketbase";
|
import pb from "@/lib/pocketbase";
|
||||||
|
|
||||||
const { EMAIL, PASSWORD } = process.env;
|
const { EMAIL, PASSWORD, COLLECTION = "budgetable" } = process.env;
|
||||||
|
|
||||||
async function authenticateSuperuser() {
|
async function authenticateSuperuser() {
|
||||||
if (!EMAIL || !PASSWORD) {
|
if (!EMAIL || !PASSWORD) {
|
||||||
throw new Error("Environment variables EMAIL and PASSWORD must be set");
|
throw new Error("Environment variables EMAIL and PASSWORD must be set");
|
||||||
}
|
}
|
||||||
if (!pb.authStore.isValid) {
|
if (!pb.authStore.isValid) {
|
||||||
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
|
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(
|
||||||
_req: Request,
|
_req: Request,
|
||||||
context: { params: Promise<{ id: string }> },
|
context: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await authenticateSuperuser();
|
await authenticateSuperuser();
|
||||||
|
|
||||||
const id = (await context.params)?.id;
|
const id = (await context.params)?.id;
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Missing ID in request",
|
message: "Missing ID in request",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const record = await pb.collection("budgetable").getOne(id);
|
const record = await pb.collection(COLLECTION).getOne(id);
|
||||||
return Response.json(record, {
|
return Response.json(record, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error("Error fetching data:", error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Failed to fetch data",
|
message: "Failed to fetch data",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(
|
export async function DELETE(
|
||||||
_req: Request,
|
_req: Request,
|
||||||
context: { params: Promise<{ id: string }> },
|
context: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await authenticateSuperuser();
|
await authenticateSuperuser();
|
||||||
|
|
||||||
const id = (await context.params)?.id;
|
const id = (await context.params)?.id;
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Missing ID in request",
|
message: "Missing ID in request",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await pb.collection("budgetable").delete(id);
|
await pb.collection(COLLECTION).delete(id);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error deleting data:", error);
|
console.error("Error deleting data:", error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Failed to delete data",
|
message: "Failed to delete data",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function PUT(
|
export async function PUT(
|
||||||
req: Request,
|
req: Request,
|
||||||
context: { params: Promise<{ id: string }> },
|
context: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await authenticateSuperuser();
|
await authenticateSuperuser();
|
||||||
|
|
||||||
const id = (await context.params)?.id;
|
const id = (await context.params)?.id;
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Missing ID in request",
|
message: "Missing ID in request",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
if (!body.title || typeof body.price !== "number") {
|
if (!body.title || typeof body.price !== "number") {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Invalid data provided",
|
message: "Invalid data provided",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 400,
|
status: 400,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedRecord = await pb.collection("budgetable").update(id, body);
|
const updatedRecord = await pb.collection(COLLECTION).update(id, body);
|
||||||
return Response.json(updatedRecord, {
|
return Response.json(updatedRecord, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error updating data:", error);
|
console.error("Error updating data:", error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Failed to update data",
|
message: "Failed to update data",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,61 +1,61 @@
|
||||||
import pb from "@/lib/pocketbase";
|
import pb from "@/lib/pocketbase";
|
||||||
|
|
||||||
const { EMAIL, PASSWORD } = process.env;
|
const { EMAIL, PASSWORD, COLLECTION = "budgetable" } = process.env;
|
||||||
|
|
||||||
async function authenticateSuperuser() {
|
async function authenticateSuperuser() {
|
||||||
if (!EMAIL || !PASSWORD) {
|
if (!EMAIL || !PASSWORD) {
|
||||||
throw new Error("Environment variables EMAIL and PASSWORD must be set");
|
throw new Error("Environment variables EMAIL and PASSWORD must be set");
|
||||||
}
|
}
|
||||||
if (!pb.authStore.isValid) {
|
if (!pb.authStore.isValid) {
|
||||||
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
|
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
await authenticateSuperuser();
|
await authenticateSuperuser();
|
||||||
const records = await pb.collection("budgetable").getFullList();
|
const records = await pb.collection(COLLECTION).getFullList();
|
||||||
return Response.json(records, {
|
return Response.json(records, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching data:", error);
|
console.error("Error fetching data:", error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Failed to fetch data",
|
message: "Failed to fetch data",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
await authenticateSuperuser();
|
await authenticateSuperuser();
|
||||||
const data = await req.json();
|
const data = await req.json();
|
||||||
const record = await pb.collection("budgetable").create(data);
|
const record = await pb.collection(COLLECTION).create(data);
|
||||||
return Response.json(record, {
|
return Response.json(record, {
|
||||||
status: 201,
|
status: 201,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error adding data:", error);
|
console.error("Error adding data:", error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
error: {
|
error: {
|
||||||
message: "Failed to add data",
|
message: "Failed to add data",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue