Skip to content

Commit b615e15

Browse files
committed
Fix place update API with proper request handling
1 parent e3746bf commit b615e15

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

src/routes/api/places/update/index.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import type { RequestHandler } from "@builder.io/qwik-city";
22
import { UpdatePlace } from "~/api/Query";
33

4-
export const onPost: RequestHandler = async ({ request, json, event }) => {
4+
export const onPost: RequestHandler = async ({ request, json }) => {
55
try {
6-
const body = await request.parseBody();
6+
const body = await request.json();
77

88
// Validate required fields
99
if (!body.placeId) {
10-
return json(400, {
10+
json(400, {
1111
success: false,
1212
message: "Missing required field: placeId",
1313
});
14+
return;
1415
}
1516

1617
const placeId = parseInt(body.placeId as string);
1718
if (isNaN(placeId)) {
18-
return json(400, {
19+
json(400, {
1920
success: false,
2021
message: "Invalid placeId format",
2122
});
23+
return;
2224
}
2325

2426
// Parse and validate placeData
@@ -29,10 +31,11 @@ export const onPost: RequestHandler = async ({ request, json, event }) => {
2931
? JSON.parse(body.placeData)
3032
: body.placeData;
3133
} catch (error) {
32-
return json(400, {
34+
json(400, {
3335
success: false,
3436
message: "Invalid placeData format. Must be valid JSON.",
3537
});
38+
return;
3639
}
3740

3841
// Validate specific fields
@@ -53,67 +56,72 @@ export const onPost: RequestHandler = async ({ request, json, event }) => {
5356
);
5457

5558
if (invalidFields.length > 0) {
56-
return json(400, {
59+
json(400, {
5760
success: false,
5861
message: `Invalid fields: ${invalidFields.join(
5962
", "
6063
)}. Allowed fields: ${allowedFields.join(", ")}`,
6164
});
65+
return;
6266
}
6367

6468
// Field-specific validations
6569
if (placeData.rating !== undefined) {
6670
if (typeof placeData.rating !== "number" || placeData.rating < 1 || placeData.rating > 5) {
67-
return json(400, {
71+
json(400, {
6872
success: false,
6973
message: "Rating must be a number between 1 and 5",
7074
});
75+
return;
7176
}
7277
}
7378

7479
if (placeData.wifiSpeed !== undefined) {
7580
if (typeof placeData.wifiSpeed !== "number" || placeData.wifiSpeed < 0) {
76-
return json(400, {
81+
json(400, {
7782
success: false,
7883
message: "WifiSpeed must be a non-negative number",
7984
});
85+
return;
8086
}
8187
}
8288

8389
if (placeData.lat !== undefined && (typeof placeData.lat !== "number" || placeData.lat < -90 || placeData.lat > 90)) {
84-
return json(400, {
90+
json(400, {
8591
success: false,
8692
message: "Latitude must be a number between -90 and 90",
8793
});
94+
return;
8895
}
8996

9097
if (placeData.lng !== undefined && (typeof placeData.lng !== "number" || placeData.lng < -180 || placeData.lng > 180)) {
91-
return json(400, {
98+
json(400, {
9299
success: false,
93100
message: "Longitude must be a number between -180 and 180",
94101
});
102+
return;
95103
}
96104

97-
// Call the secure update function with built-in ownership validation
105+
// Call secure update function with built-in ownership validation
98106
const result = await UpdatePlace({
99-
event,
107+
event: request,
100108
placeId,
101109
placeData,
102110
});
103111

104112
if (result.success) {
105-
return json(200, result);
113+
json(200, result);
106114
} else {
107115
const statusCode = result.message.includes("Unauthorized")
108116
? 403
109117
: result.message.includes("not found")
110118
? 404
111119
: 400;
112-
return json(statusCode, result);
120+
json(statusCode, result);
113121
}
114122
} catch (error) {
115123
console.error("API Error in place update:", error);
116-
return json(500, {
124+
json(500, {
117125
success: false,
118126
message: "Internal server error",
119127
});
@@ -122,7 +130,7 @@ export const onPost: RequestHandler = async ({ request, json, event }) => {
122130

123131
// Keep GET endpoint for backward compatibility or debugging
124132
export const onGet: RequestHandler = async ({ json }) => {
125-
return json(405, {
133+
json(405, {
126134
success: false,
127135
message: "Method not allowed. Use POST to update place.",
128136
});

0 commit comments

Comments
 (0)