X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FEventController.php;h=c9a1a4190fb0e2c2c9b713879e98d826ec81d000;hb=147c5f43c5d41fa18e82edb6651fe5a37c789353;hp=eb742399dab1d1ba9dfd1e8c172ed8e483bda502;hpb=071885a30f24b980699b337d9cdb65952f8c6c42;p=alttp.git diff --git a/app/Http/Controllers/EventController.php b/app/Http/Controllers/EventController.php index eb74239..c9a1a41 100644 --- a/app/Http/Controllers/EventController.php +++ b/app/Http/Controllers/EventController.php @@ -3,18 +3,55 @@ namespace App\Http\Controllers; use App\Models\Event; +use Carbon\Carbon; use Illuminate\Http\Request; class EventController extends Controller { public function search(Request $request) { + $validatedData = $request->validate([ + 'after' => 'nullable|date', + 'before' => 'nullable|date', + 'order' => 'nullable|string', + 'with' => 'nullable|array', + 'with.*' => 'string', + ]); $events = Event::where('visible', '=', true); + if (isset($validatedData['before'])) { + $events = $events->where(function ($query) use ($validatedData) { + $query->whereNull('start'); + $query->orWhere('start', '<', $validatedData['before']); + }); + } + if (isset($validatedData['after'])) { + $events = $events->where(function ($query) use ($validatedData) { + $query->whereNull('end'); + $query->orWhere('end', '>', $validatedData['after']); + }); + } + if (isset($validatedData['order'])) { + switch ($validatedData['order']) { + case 'recency': + $events->orderByRaw('start IS NOT NULL'); + $events->orderByRaw('end IS NOT NULL'); + $events->orderBy('end', 'DESC'); + $events->orderBy('start', 'DESC'); + $events->orderBy('name', 'ASC'); + break; + } + } + if (isset($validatedData['with'])) { + if (in_array('description', $validatedData['with'])) { + $events->with('description'); + } + } return $events->get()->toJson(); } public function single(Request $request, Event $event) { $this->authorize('view', $event); + $event->load('description'); return $event->toJson(); }