feat: get collection from env

This commit is contained in:
hasitotabla 2025-01-02 22:35:47 +01:00
parent fb7740dae0
commit eda85a3292
2 changed files with 187 additions and 187 deletions

View file

@ -1,6 +1,6 @@
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) {
@ -13,7 +13,7 @@ async function authenticateSuperuser() {
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();
@ -29,11 +29,11 @@ export async function GET(
{ {
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" },
@ -49,14 +49,14 @@ export async function GET(
{ {
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();
@ -72,11 +72,11 @@ export async function DELETE(
{ {
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,
@ -84,7 +84,7 @@ export async function DELETE(
{ {
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);
@ -97,14 +97,14 @@ export async function DELETE(
{ {
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();
@ -120,7 +120,7 @@ export async function PUT(
{ {
status: 400, status: 400,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}, }
); );
} }
@ -135,11 +135,11 @@ export async function PUT(
{ {
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" },
@ -155,7 +155,7 @@ export async function PUT(
{ {
status: 500, status: 500,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}, }
); );
} }
} }

View file

@ -1,6 +1,6 @@
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) {
@ -14,7 +14,7 @@ async function authenticateSuperuser() {
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" },
@ -30,7 +30,7 @@ export async function GET() {
{ {
status: 500, status: 500,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}, }
); );
} }
} }
@ -39,7 +39,7 @@ 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" },
@ -55,7 +55,7 @@ export async function POST(req: Request) {
{ {
status: 500, status: 500,
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
}, }
); );
} }
} }