first commit
This commit is contained in:
60
app/Http/Controllers/MerchantController.php
Normal file
60
app/Http/Controllers/MerchantController.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class MerchantController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/merchants
|
||||
* Get all merchants with optional filters
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
// Read from merchants.json file (like the original Express.js backend)
|
||||
$jsonPath = base_path('merchants.json');
|
||||
|
||||
if (!file_exists($jsonPath)) {
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
$merchants = json_decode(file_get_contents($jsonPath), true);
|
||||
|
||||
// Filter by bbox if provided
|
||||
if ($request->has('bbox') && !empty($request->bbox)) {
|
||||
$bbox = explode(',', $request->bbox);
|
||||
if (count($bbox) === 4) {
|
||||
$minLng = (float) $bbox[0];
|
||||
$minLat = (float) $bbox[1];
|
||||
$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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by categories if provided
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user