From 83f92dd7a1bf1e4f19b2f7ed9fe2227aaa131f23 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 14 Jan 2026 12:25:28 +0100 Subject: [PATCH] feat: search params --- app/Http/Controllers/HuntsController.php | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/app/Http/Controllers/HuntsController.php b/app/Http/Controllers/HuntsController.php index 7202570..06a2d80 100644 --- a/app/Http/Controllers/HuntsController.php +++ b/app/Http/Controllers/HuntsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Enums\HuntDifficulty; use App\Http\Requests\StoreHuntRequest; use App\Http\Requests\UpdateHuntRequest; use App\Models\Hunts; @@ -105,6 +106,54 @@ class HuntsController extends Controller } } + if ($request->filled('city')) { + $city = $request->query('city'); + + if (is_string($city)) { + $query->where('city', 'like', '%'.$city.'%'); + } + } + + if ($request->filled('difficulty')) { + $difficulties = $request->query('difficulty'); + + if (is_string($difficulties)) { + $difficulties = explode(',', $difficulties); + } + + if (is_array($difficulties)) { + $validDifficulties = array_values(array_intersect( + HuntDifficulty::values(), + array_map('strtolower', $difficulties) + )); + + if (! empty($validDifficulties)) { + $query->whereIn('difficulty', $validDifficulties); + } + } + } + + $startFrom = $this->parseDateFilter($request->query('start_from')); + $startTo = $this->parseDateFilter($request->query('start_to')); + $endFrom = $this->parseDateFilter($request->query('end_from')); + $endTo = $this->parseDateFilter($request->query('end_to')); + + if ($startFrom) { + $query->where('start_at', '>=', $startFrom); + } + + if ($startTo) { + $query->where('start_at', '<=', $startTo); + } + + if ($endFrom) { + $query->where('end_at', '>=', $endFrom); + } + + if ($endTo) { + $query->where('end_at', '<=', $endTo); + } + return $query; } @@ -121,6 +170,19 @@ class HuntsController extends Controller return $searchTerm !== '' ? $searchTerm : null; } + private function parseDateFilter(mixed $value): ?Carbon + { + if (! is_string($value) || trim($value) === '') { + return null; + } + + try { + return Carbon::parse($value); + } catch (\Throwable $e) { + return null; + } + } + /** * Store a newly created resource in storage. */