From 3998f79056dee74eb1e5e6665e7e319efc99e31d Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 5 Jan 2026 14:11:17 +0100 Subject: [PATCH] feat: participating hunts --- app/Http/Controllers/HuntsController.php | 12 +++++-- tests/Feature/HuntParticipationTest.php | 41 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/HuntParticipationTest.php diff --git a/app/Http/Controllers/HuntsController.php b/app/Http/Controllers/HuntsController.php index ecd4d1c..929c24f 100644 --- a/app/Http/Controllers/HuntsController.php +++ b/app/Http/Controllers/HuntsController.php @@ -16,9 +16,15 @@ class HuntsController extends Controller $query = Hunts::with('participants:id,avatar_uri'); if ($request->boolean('participating')) { - $query->whereHas('participants', function ($q) { - $q->where('user_id', auth()->id()); - }); + $userId = auth('sanctum')->id(); + if ($userId) { + $query->whereHas('participants', function ($q) use ($userId) { + $q->where('user_id', $userId); + }); + } else { + // User wants participating hunts but is not logged in -> return nothing + $query->whereRaw('1 = 0'); + } } if ($request->boolean('active')) { diff --git a/tests/Feature/HuntParticipationTest.php b/tests/Feature/HuntParticipationTest.php new file mode 100644 index 0000000..10ca954 --- /dev/null +++ b/tests/Feature/HuntParticipationTest.php @@ -0,0 +1,41 @@ +create(); + + $hunt = Hunts::create([ + 'title' => 'Test Hunt', + 'slug' => 'test-hunt-' . uniqid(), + 'status' => HuntStatus::PUBLISHED, + 'difficulty' => HuntDifficulty::EASY, + 'city' => 'Paris', + 'creator_id' => $user->id, + 'latitude' => 0, + 'longitude' => 0, + 'is_public' => true + ]); + + // Participate + $hunt->participants()->attach($user->id, ['status' => 'started', 'current_step_number' => 1]); + + $response = $this->actingAs($user)->getJson('/api/hunts?participating=true'); + + $response->assertStatus(200) + ->assertJsonCount(1, 'data') + ->assertJsonFragment(['id' => $hunt->id]); +}); + +test('guest receiving empty list when filtering participating hunts', function () { + $response = $this->getJson('/api/hunts?participating=true'); + + $response->assertStatus(200) + ->assertJsonCount(0, 'data'); +});