-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚑 add fallback for station search (#3103)
- Loading branch information
1 parent
24c983a
commit 9f07bba
Showing
6 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
app/Http/Controllers/Backend/Transport/BahnWebApiController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Backend\Transport; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Station; | ||
use App\Models\User; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
abstract class BahnWebApiController extends Controller | ||
{ | ||
|
||
public static function searchStation(string $query, int $limit = 10): Collection { | ||
$url = "https://www.bahn.de/web/api/reiseloesung/orte?suchbegriff=" . urlencode($query) . "&typ=ALL&limit=" . $limit; | ||
$response = Http::get($url); | ||
$json = $response->json(); | ||
$extIds = []; | ||
foreach ($json as $rawStation) { | ||
if (!isset($rawStation['extId'])) { | ||
continue; | ||
} | ||
$extIds[] = $rawStation['extId']; | ||
} | ||
$stationCache = Station::whereIn('ibnr', $extIds)->get(); | ||
|
||
$stations = collect(); | ||
foreach ($json as $rawStation) { | ||
if (!isset($rawStation['extId'])) { | ||
continue; | ||
} | ||
$station = $stationCache->where('ibnr', $rawStation['extId'])->first(); | ||
if ($station === null) { | ||
$station = Station::create([ | ||
'name' => $rawStation['name'], | ||
'latitude' => $rawStation['lat'], | ||
'longitude' => $rawStation['lon'], | ||
'ibnr' => $rawStation['extId'], | ||
'source' => 'bahn-web-api', | ||
]); | ||
} | ||
$stations->push($station); | ||
} | ||
|
||
return $stations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
database/migrations/2025_01_08_000000_add_source_to_stations.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
|
||
public function up(): void { | ||
Schema::table('train_stations', function(Blueprint $table) { | ||
$table->string('source')->nullable()->after('longitude'); | ||
}); | ||
} | ||
|
||
public function down(): void { | ||
Schema::table('train_stations', function(Blueprint $table) { | ||
$table->dropColumn('source'); | ||
}); | ||
} | ||
}; |