Skip to content

Commit

Permalink
🚑 add fallback for station search (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jan 8, 2025
1 parent 24c983a commit 9f07bba
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
47 changes: 47 additions & 0 deletions app/Http/Controllers/Backend/Transport/BahnWebApiController.php
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;
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/FrontendStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Controllers\Backend\Transport\BahnWebApiController;
use App\Http\Controllers\Backend\Transport\StationController;
use App\Http\Controllers\Backend\User\DashboardController;
use App\Http\Controllers\Backend\User\ProfilePictureController;
Expand All @@ -19,6 +20,9 @@
class FrontendStatusController extends Controller
{
public function getDashboard(): Renderable|RedirectResponse {

BahnWebApiController::searchStation('Karlsruhe Hbf');

$statuses = DashboardController::getPrivateDashboard(auth()->user());

return view('dashboard', [
Expand Down
12 changes: 11 additions & 1 deletion app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\DataProviders\DataProviderBuilder;
use App\DataProviders\DataProviderInterface;
use App\Exceptions\HafasException;
use App\Http\Controllers\API\v1\ExperimentalController;
use App\Http\Controllers\Backend\Transport\BahnWebApiController;
use App\Http\Resources\StationResource;
use App\Models\Checkin;
use App\Models\PolyLine;
Expand Down Expand Up @@ -42,7 +44,15 @@ public function getTrainStationAutocomplete(string $query): Collection {
} elseif (preg_match('/^Q\d+$/', $query)) {
$stations = self::getStationsByWikidataId($query);
} elseif (!isset($stations) || $stations[0] === null) {
$stations = $this->dataProvider->getStations($query);
$stations = null;
try {
$stations = $this->dataProvider->getStations($query);
} catch (HafasException) {

}
if ($stations === null || $stations->isEmpty()) {
$stations = BahnWebApiController::searchStation($query);
}
}
return $stations->map(function(Station $station) {
return new StationResource($station);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Station.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Station extends Model
protected $fillable = [
'ibnr', 'wikidata_id', 'rilIdentifier',
'ifopt_a', 'ifopt_b', 'ifopt_c', 'ifopt_d', 'ifopt_e',
'name', 'latitude', 'longitude', 'time_offset', 'shift_time'
'name', 'latitude', 'longitude', 'source', 'time_offset', 'shift_time'
];
protected $hidden = ['created_at', 'updated_at', 'time_offset', 'shift_time'];
protected $casts = [
Expand Down
2 changes: 1 addition & 1 deletion config/trwl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# DB_REST
'db_rest' => env('DB_REST', 'https://v5.db.transport.rest/'),
'db_rest_timeout' => env('DB_REST_TIMEOUT', 10),
'db_rest_timeout' => env('DB_REST_TIMEOUT', 3),

# Points
'base_points' => [
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/2025_01_08_000000_add_source_to_stations.php
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');
});
}
};

0 comments on commit 9f07bba

Please sign in to comment.