Skip to content

Commit

Permalink
[API] events apis filter by endpoint #169 (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ authored Oct 22, 2024
1 parent 8d018ea commit b124053
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
24 changes: 19 additions & 5 deletions app/Http/Controllers/API/EventApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/Backend/CourseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,4 +80,4 @@ public function rowView(): string
{
return 'backend.courses.index-table-row';
}
}
}
12 changes: 11 additions & 1 deletion app/Http/Livewire/Backend/EventsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -78,6 +86,8 @@ public function filters(): array
1 => 'Upcoming',
0 => 'Past',
]),
'event_type' => Filter::make("Event Type")
->select(array_merge(['' => 'Any'], Event::eventTypeMap()))
];
}

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Resources/EventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit b124053

Please sign in to comment.