feat: participating hunts
This commit is contained in:
parent
b6c756c30a
commit
3998f79056
|
|
@ -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')) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Hunts;
|
||||
use App\Enums\HuntStatus;
|
||||
use App\Enums\HuntDifficulty;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('authenticated user can filter participating hunts', function () {
|
||||
$user = User::factory()->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');
|
||||
});
|
||||
Loading…
Reference in New Issue