From b124053506ea1e5447504c3d4e8501714dcadfc7 Mon Sep 17 00:00:00 2001 From: Nuwan Jaliyagoda Date: Tue, 22 Oct 2024 22:23:45 +0530 Subject: [PATCH] [API] events apis filter by endpoint #169 (#199) --- .../Controllers/API/EventApiController.php | 24 +++++++++++++++---- app/Http/Livewire/Backend/CourseTable.php | 4 ++-- app/Http/Livewire/Backend/EventsTable.php | 12 +++++++++- app/Http/Resources/EventResource.php | 7 ++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/API/EventApiController.php b/app/Http/Controllers/API/EventApiController.php index 80e9c10..25d9dd6 100644 --- a/app/Http/Controllers/API/EventApiController.php +++ b/app/Http/Controllers/API/EventApiController.php @@ -5,19 +5,33 @@ use App\Domains\Event\Models\Event; use App\Http\Controllers\Controller; use App\Http\Resources\EventResource; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class EventApiController extends Controller { - public function index() + public function index(Request $request) { try { - $perPage = 20; - $event = Event::where('enabled', 1)->orderBy('start_at', 'desc')->paginate($perPage); + $query = Event::where('enabled', 1)->orderBy('start_at', 'desc'); + + if ($request->has('event_type')) { + + if (in_array($request->event_type, Event::eventTypeMap())) { + $eventTypeId = array_search($request->event_type, Event::eventTypeMap()); + + // Note: This is not the best way, but easiest way to filter JSON content + $query = $query->where('event_type', 'LIKE', "%\"$eventTypeId\"%"); + } else { + return EventResource::collection([]); + } + } + + $events = $query->paginate(20); - return EventResource::collection($event); + return EventResource::collection($events); } catch (\Exception $e) { - Log::error('Error in EventApiController@index', ['error' => $e->getMessage()]); + Log::error('Error in EventApiController@index', $e); return response()->json(['message' => 'An error occurred while fetching events'], 500); } } diff --git a/app/Http/Livewire/Backend/CourseTable.php b/app/Http/Livewire/Backend/CourseTable.php index 2188eff..57477ff 100644 --- a/app/Http/Livewire/Backend/CourseTable.php +++ b/app/Http/Livewire/Backend/CourseTable.php @@ -46,7 +46,7 @@ public function query(): Builder return Course::query() ->when($this->getFilter('academic_program'), fn($query, $type) => $query->where('academic_program', $type)) ->when($this->getFilter('semester_id'), fn($query, $type) => $query->where('semester_id', $type)) - ->when($this->getFilter('version'), fn($query, $version) => $query->where('version', $version));; + ->when($this->getFilter('version'), fn($query, $version) => $query->where('version', $version)); } public function filters(): array @@ -80,4 +80,4 @@ public function rowView(): string { return 'backend.courses.index-table-row'; } -} \ No newline at end of file +} diff --git a/app/Http/Livewire/Backend/EventsTable.php b/app/Http/Livewire/Backend/EventsTable.php index de81950..762492f 100644 --- a/app/Http/Livewire/Backend/EventsTable.php +++ b/app/Http/Livewire/Backend/EventsTable.php @@ -54,7 +54,15 @@ public function query(): Builder } elseif ($enabled === 0) { $query->where('enabled', false); } - })->orderBy('published_at', 'desc'); + }) + ->when( + $this->getFilter('event_type') !== null, + function ($query) { + $eventType = $this->getFilter('event_type'); + $query->where('event_type', 'LIKE', "%\"$eventType\"%"); + } + ) + ->orderBy('published_at', 'desc'); } public function toggleEnable($eventId) { @@ -78,6 +86,8 @@ public function filters(): array 1 => 'Upcoming', 0 => 'Past', ]), + 'event_type' => Filter::make("Event Type") + ->select(array_merge(['' => 'Any'], Event::eventTypeMap())) ]; } diff --git a/app/Http/Resources/EventResource.php b/app/Http/Resources/EventResource.php index 1d2dbd1..950fa0a 100644 --- a/app/Http/Resources/EventResource.php +++ b/app/Http/Resources/EventResource.php @@ -3,6 +3,7 @@ namespace App\Http\Resources; use App\Domains\Auth\Models\User; +use App\Domains\Event\Models\Event; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\URL; @@ -16,6 +17,11 @@ class EventResource extends JsonResource */ public function toArray($request) { + $eventTypeList = Event::eventTypeMap(); + $eventTypes = array_map(function ($id) use ($eventTypeList) { + return isset($eventTypeList[$id]) ? $eventTypeList[$id] : null; + }, $this->event_type); + return [ 'id' => $this->id, 'title' => $this->title, @@ -24,6 +30,7 @@ public function toArray($request) 'image' => URL::to($this->thumbURL()), 'start_at' => $this->start_at, 'end_at' => $this->end_at, + 'event_type' => $eventTypes, 'location' => $this->location, 'link_url' => $this->link_url, 'link_caption' => $this->link_caption,