Refactor script

This commit is contained in:
furen81
2026-02-07 04:52:11 +07:00
parent 6e681c4ad3
commit c1ef2df512
20 changed files with 1608 additions and 118 deletions

View File

@ -9,20 +9,14 @@ class MerchantController extends Controller
{
/**
* GET /api/merchants
* Get all merchants with optional filters
* Get all customers (aliased as merchants for frontend compatibility)
*/
public function index(Request $request): JsonResponse
{
// Read from merchants.json file (like the original Express.js backend)
$jsonPath = base_path('merchants.json');
// Switch to Customer model
$query = \App\Models\Customer::query();
if (!file_exists($jsonPath)) {
return response()->json([]);
}
$merchants = json_decode(file_get_contents($jsonPath), true);
// Filter by bbox if provided
// Filter by bbox
if ($request->has('bbox') && !empty($request->bbox)) {
$bbox = explode(',', $request->bbox);
if (count($bbox) === 4) {
@ -31,30 +25,34 @@ class MerchantController extends Controller
$maxLng = (float) $bbox[2];
$maxLat = (float) $bbox[3];
$merchants = array_filter($merchants, function ($m) use ($minLat, $maxLat, $minLng, $maxLng) {
return $m['latitude'] >= $minLat &&
$m['latitude'] <= $maxLat &&
$m['longitude'] >= $minLng &&
$m['longitude'] <= $maxLng;
});
$query->where('latitude', '>=', $minLat)
->where('latitude', '<=', $maxLat)
->where('longitude', '>=', $minLng)
->where('longitude', '<=', $maxLng);
}
}
// Filter by categories if provided
// Filter by categories
if ($request->has('categories') && !empty($request->categories)) {
$categories = explode(',', $request->categories);
$merchants = array_filter($merchants, function ($m) use ($categories) {
return in_array($m['category'], $categories);
});
$query->whereIn('category', $categories);
}
$customers = $query->get();
// return formatted data
$data = $customers->map(function ($m) {
return [
'id' => $m->id,
'name' => $m->name,
'category' => $m->category ?? 'other',
'latitude' => (float) $m->latitude,
'longitude' => (float) $m->longitude,
'city' => $m->city ?? '',
'address' => $m->address ?? ''
];
});
// Return plain values
return response()->json(array_map(function ($m) {
$m['latitude'] = (float) $m['latitude'];
$m['longitude'] = (float) $m['longitude'];
return $m;
}, array_values($merchants)));
return response()->json($data);
}
}