@@ -3,159 +3,159 @@ import pb from "@/lib/pocketbase";
33const { EMAIL , PASSWORD , COLLECTION = "budgetable" } = process . env ;
44
55async 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+ }
1212}
1313
1414export async function GET (
15- _req : Request ,
16- context : { params : Promise < { id : string } > }
15+ _req : Request ,
16+ context : { params : Promise < { id : string } > } ,
1717) {
18- try {
19- await authenticateSuperuser ( ) ;
18+ try {
19+ await authenticateSuperuser ( ) ;
2020
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+ }
3535
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- }
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+ }
5555}
5656
5757export async function DELETE (
58- _req : Request ,
59- context : { params : Promise < { id : string } > }
58+ _req : Request ,
59+ context : { params : Promise < { id : string } > } ,
6060) {
61- try {
62- await authenticateSuperuser ( ) ;
61+ try {
62+ await authenticateSuperuser ( ) ;
6363
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+ }
7878
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- }
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+ }
103103}
104104
105105export async function PUT (
106- req : Request ,
107- context : { params : Promise < { id : string } > }
106+ req : Request ,
107+ context : { params : Promise < { id : string } > } ,
108108) {
109- try {
110- await authenticateSuperuser ( ) ;
109+ try {
110+ await authenticateSuperuser ( ) ;
111111
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+ }
126126
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+ }
141141
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- }
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+ }
161161}
0 commit comments