|
1 | 1 | import pb from "@/lib/pocketbase"; |
2 | 2 |
|
3 | | -const { EMAIL, PASSWORD } = process.env; |
| 3 | +const { EMAIL, PASSWORD, COLLECTION = "budgetable" } = process.env; |
4 | 4 |
|
5 | 5 | async function authenticateSuperuser() { |
6 | | - if (!EMAIL || !PASSWORD) { |
7 | | - throw new Error("Environment variables EMAIL and PASSWORD must be set"); |
8 | | - } |
9 | | - if (!pb.authStore.isValid) { |
10 | | - await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD); |
11 | | - } |
| 6 | + if (!EMAIL || !PASSWORD) { |
| 7 | + throw new Error("Environment variables EMAIL and PASSWORD must be set"); |
| 8 | + } |
| 9 | + if (!pb.authStore.isValid) { |
| 10 | + await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD); |
| 11 | + } |
12 | 12 | } |
13 | 13 |
|
14 | 14 | export async function GET( |
15 | | - _req: Request, |
16 | | - context: { params: Promise<{ id: string }> }, |
| 15 | + _req: Request, |
| 16 | + context: { params: Promise<{ id: string }> } |
17 | 17 | ) { |
18 | | - try { |
19 | | - await authenticateSuperuser(); |
| 18 | + try { |
| 19 | + await authenticateSuperuser(); |
20 | 20 |
|
21 | | - const id = (await context.params)?.id; |
22 | | - if (!id) { |
23 | | - return Response.json( |
24 | | - { |
25 | | - error: { |
26 | | - message: "Missing ID in request", |
27 | | - }, |
28 | | - }, |
29 | | - { |
30 | | - status: 400, |
31 | | - headers: { "Content-Type": "application/json" }, |
32 | | - }, |
33 | | - ); |
34 | | - } |
| 21 | + const id = (await context.params)?.id; |
| 22 | + if (!id) { |
| 23 | + return Response.json( |
| 24 | + { |
| 25 | + error: { |
| 26 | + message: "Missing ID in request", |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + status: 400, |
| 31 | + headers: { "Content-Type": "application/json" }, |
| 32 | + } |
| 33 | + ); |
| 34 | + } |
35 | 35 |
|
36 | | - const record = await pb.collection("budgetable").getOne(id); |
37 | | - return Response.json(record, { |
38 | | - status: 200, |
39 | | - headers: { "Content-Type": "application/json" }, |
40 | | - }); |
41 | | - } catch (error) { |
42 | | - console.error("Error fetching data:", error); |
43 | | - return Response.json( |
44 | | - { |
45 | | - error: { |
46 | | - message: "Failed to fetch data", |
47 | | - }, |
48 | | - }, |
49 | | - { |
50 | | - status: 500, |
51 | | - headers: { "Content-Type": "application/json" }, |
52 | | - }, |
53 | | - ); |
54 | | - } |
| 36 | + const record = await pb.collection(COLLECTION).getOne(id); |
| 37 | + return Response.json(record, { |
| 38 | + status: 200, |
| 39 | + headers: { "Content-Type": "application/json" }, |
| 40 | + }); |
| 41 | + } catch (error) { |
| 42 | + console.error("Error fetching data:", error); |
| 43 | + return Response.json( |
| 44 | + { |
| 45 | + error: { |
| 46 | + message: "Failed to fetch data", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + status: 500, |
| 51 | + headers: { "Content-Type": "application/json" }, |
| 52 | + } |
| 53 | + ); |
| 54 | + } |
55 | 55 | } |
56 | 56 |
|
57 | 57 | export async function DELETE( |
58 | | - _req: Request, |
59 | | - context: { params: Promise<{ id: string }> }, |
| 58 | + _req: Request, |
| 59 | + context: { params: Promise<{ id: string }> } |
60 | 60 | ) { |
61 | | - try { |
62 | | - await authenticateSuperuser(); |
| 61 | + try { |
| 62 | + await authenticateSuperuser(); |
63 | 63 |
|
64 | | - const id = (await context.params)?.id; |
65 | | - if (!id) { |
66 | | - return Response.json( |
67 | | - { |
68 | | - error: { |
69 | | - message: "Missing ID in request", |
70 | | - }, |
71 | | - }, |
72 | | - { |
73 | | - status: 400, |
74 | | - headers: { "Content-Type": "application/json" }, |
75 | | - }, |
76 | | - ); |
77 | | - } |
| 64 | + const id = (await context.params)?.id; |
| 65 | + if (!id) { |
| 66 | + return Response.json( |
| 67 | + { |
| 68 | + error: { |
| 69 | + message: "Missing ID in request", |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + status: 400, |
| 74 | + headers: { "Content-Type": "application/json" }, |
| 75 | + } |
| 76 | + ); |
| 77 | + } |
78 | 78 |
|
79 | | - await pb.collection("budgetable").delete(id); |
80 | | - return Response.json( |
81 | | - { |
82 | | - success: true, |
83 | | - }, |
84 | | - { |
85 | | - status: 200, |
86 | | - headers: { "Content-Type": "application/json" }, |
87 | | - }, |
88 | | - ); |
89 | | - } catch (error) { |
90 | | - console.error("Error deleting data:", error); |
91 | | - return Response.json( |
92 | | - { |
93 | | - error: { |
94 | | - message: "Failed to delete data", |
95 | | - }, |
96 | | - }, |
97 | | - { |
98 | | - status: 500, |
99 | | - headers: { "Content-Type": "application/json" }, |
100 | | - }, |
101 | | - ); |
102 | | - } |
| 79 | + await pb.collection(COLLECTION).delete(id); |
| 80 | + return Response.json( |
| 81 | + { |
| 82 | + success: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + status: 200, |
| 86 | + headers: { "Content-Type": "application/json" }, |
| 87 | + } |
| 88 | + ); |
| 89 | + } catch (error) { |
| 90 | + console.error("Error deleting data:", error); |
| 91 | + return Response.json( |
| 92 | + { |
| 93 | + error: { |
| 94 | + message: "Failed to delete data", |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + status: 500, |
| 99 | + headers: { "Content-Type": "application/json" }, |
| 100 | + } |
| 101 | + ); |
| 102 | + } |
103 | 103 | } |
104 | 104 |
|
105 | 105 | export async function PUT( |
106 | | - req: Request, |
107 | | - context: { params: Promise<{ id: string }> }, |
| 106 | + req: Request, |
| 107 | + context: { params: Promise<{ id: string }> } |
108 | 108 | ) { |
109 | | - try { |
110 | | - await authenticateSuperuser(); |
| 109 | + try { |
| 110 | + await authenticateSuperuser(); |
111 | 111 |
|
112 | | - const id = (await context.params)?.id; |
113 | | - if (!id) { |
114 | | - return Response.json( |
115 | | - { |
116 | | - error: { |
117 | | - message: "Missing ID in request", |
118 | | - }, |
119 | | - }, |
120 | | - { |
121 | | - status: 400, |
122 | | - headers: { "Content-Type": "application/json" }, |
123 | | - }, |
124 | | - ); |
125 | | - } |
| 112 | + const id = (await context.params)?.id; |
| 113 | + if (!id) { |
| 114 | + return Response.json( |
| 115 | + { |
| 116 | + error: { |
| 117 | + message: "Missing ID in request", |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + status: 400, |
| 122 | + headers: { "Content-Type": "application/json" }, |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
126 | 126 |
|
127 | | - const body = await req.json(); |
128 | | - if (!body.title || typeof body.price !== "number") { |
129 | | - return Response.json( |
130 | | - { |
131 | | - error: { |
132 | | - message: "Invalid data provided", |
133 | | - }, |
134 | | - }, |
135 | | - { |
136 | | - status: 400, |
137 | | - headers: { "Content-Type": "application/json" }, |
138 | | - }, |
139 | | - ); |
140 | | - } |
| 127 | + const body = await req.json(); |
| 128 | + if (!body.title || typeof body.price !== "number") { |
| 129 | + return Response.json( |
| 130 | + { |
| 131 | + error: { |
| 132 | + message: "Invalid data provided", |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + status: 400, |
| 137 | + headers: { "Content-Type": "application/json" }, |
| 138 | + } |
| 139 | + ); |
| 140 | + } |
141 | 141 |
|
142 | | - const updatedRecord = await pb.collection("budgetable").update(id, body); |
143 | | - return Response.json(updatedRecord, { |
144 | | - status: 200, |
145 | | - headers: { "Content-Type": "application/json" }, |
146 | | - }); |
147 | | - } catch (error) { |
148 | | - console.error("Error updating data:", error); |
149 | | - return Response.json( |
150 | | - { |
151 | | - error: { |
152 | | - message: "Failed to update data", |
153 | | - }, |
154 | | - }, |
155 | | - { |
156 | | - status: 500, |
157 | | - headers: { "Content-Type": "application/json" }, |
158 | | - }, |
159 | | - ); |
160 | | - } |
| 142 | + const updatedRecord = await pb.collection(COLLECTION).update(id, body); |
| 143 | + return Response.json(updatedRecord, { |
| 144 | + status: 200, |
| 145 | + headers: { "Content-Type": "application/json" }, |
| 146 | + }); |
| 147 | + } catch (error) { |
| 148 | + console.error("Error updating data:", error); |
| 149 | + return Response.json( |
| 150 | + { |
| 151 | + error: { |
| 152 | + message: "Failed to update data", |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + status: 500, |
| 157 | + headers: { "Content-Type": "application/json" }, |
| 158 | + } |
| 159 | + ); |
| 160 | + } |
161 | 161 | } |
0 commit comments