Skip to content

Commit

Permalink
✨ Better sorting of the stations in the trip creation interface (#2910)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin authored Sep 12, 2024
1 parent 4298b96 commit 07fb984
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/Http/Controllers/Backend/Transport/StationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function index(string $search, string $lang): AnonymousResourceCollection
$stations->merge($this->stationRepository->getStationByName($search, $lang, true))->unique();
}

return StationResource::collection($stations);
$stations->map(function($station) use ($search) {
similar_text($station->name, $search, $percent);
$station->similarity = $percent;

return $station;
});

return StationResource::collection($stations->sortByDesc('similarity'));
}
}
9 changes: 7 additions & 2 deletions app/Repositories/StationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ class StationRepository
public function getStationByName(string $name, string $lang, bool $invertLanguage = false): Collection {
$invertLanguage = $invertLanguage ? '!=' : '=';
return Station::leftJoin('station_names', 'station_names.station_id', '=', 'train_stations.id')
->where('station_names.name', 'LIKE', "%$name%")
->where('station_names.name', 'LIKE', "$name%")
->where('station_names.language', $invertLanguage, $lang)
->orWhere('train_stations.name', 'LIKE', "%$name%")
->orWhere('train_stations.name', 'LIKE', "$name%")
->orWhere(function($query) use ($name, $invertLanguage, $lang) {
$query->where('station_names.name', 'LIKE', "%$name%")
->where('station_names.language', $invertLanguage, $lang)
->orWhere('train_stations.name', 'LIKE', "%$name%");
})
->select('train_stations.*')
->distinct()
->limit(20)
Expand Down

0 comments on commit 07fb984

Please sign in to comment.