feat: filter participating

This commit is contained in:
Leon 2026-01-05 15:28:01 +01:00
parent 105637000d
commit a421d21d47
2 changed files with 61 additions and 15 deletions

View File

@ -4,6 +4,8 @@ namespace App\Http\Controllers;
use App\Models\Hunts;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class HuntsController extends Controller
{
@ -15,36 +17,80 @@ class HuntsController extends Controller
try {
$query = Hunts::with('participants:id,avatar_uri');
if ($request->boolean('participating')) {
if ($request->has('participating')) {
$participating = filter_var(
$request->query('participating'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
);
$userId = auth('sanctum')->id();
if ($userId) {
Log::info('Participating filter resolved', [
'participating' => $participating,
'user_id' => $userId,
]);
if (!$userId) {
// Non connecté → aucune hunt "participating"
if ($participating === true) {
$query->whereRaw('1 = 0');
}
} else {
if ($participating === true) {
// Hunts où l'utilisateur participe
$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');
// Hunts où l'utilisateur NE participe PAS
$query->whereDoesntHave('participants', function ($q) use ($userId) {
$q->where('user_id', $userId);
});
}
}
}
if ($request->boolean('active')) {
// Filtre active/inactive uniquement si le param est présent
if ($request->has('active')) {
$now = now();
$active = filter_var($request->query('active'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($active === true) {
// Actives: start_at <= now AND (end_at is null OR end_at >= now)
$query->where('start_at', '<=', $now)
->where('end_at', '>=', $now);
->where(function ($q) use ($now) {
$q->whereNull('end_at')
->orWhere('end_at', '>=', $now);
});
} else {
// Inactives: NOT(active)
$query->where(function ($q) use ($now) {
$q->whereNull('start_at')
->orWhere('start_at', '>', $now)
->orWhere(function ($q2) use ($now) {
$q2->whereNotNull('end_at')
->where('end_at', '<', $now);
});
});
}
}
$hunts = $query->paginate(10);
return response()->json($hunts);
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error('Error fetching hunts: ' . $e->getMessage());
Log::error('Error fetching hunts: ' . $e->getMessage());
return response()->json([
'error' => 'Server Error',
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString()
], 500);
}
}
/**
* Store a newly created resource in storage.
*/

View File

@ -15,13 +15,11 @@ Route::prefix('auth')->group(function (): void {
Route::post('register', [AuthController::class, 'register']);
Route::post('login', [AuthController::class, 'login']);
// Route::get('{provider}/redirect', [ProviderRedirectController::class, 'redirect']);
// Route::get('{provider}/callback', [ProviderCallbackController::class, 'callback']);
// Route::get('{provider}/redirect', [ProviderRedirectController::class, 'redirect']);
// Route::get('{provider}/callback', [ProviderCallbackController::class, 'callback']);
});
// List hunts
Route::get('hunts', [HuntsController::class, 'index']);
Route::get('hunts/{hunt}', [HuntsController::class, 'show']);
// Ensure is Orga
Route::middleware(['auth:sanctum', EnsureUserIsOrga::class])->group(function () {
@ -33,6 +31,8 @@ Route::middleware(['auth:sanctum', EnsureUserIsOrga::class])->group(function ()
// Auth middleware
Route::middleware('auth:sanctum')->group(function (): void {
Route::get('hunts', [HuntsController::class, 'index']);
Route::get('hunts/{hunt}', [HuntsController::class, 'show']);
// User
Route::get('users/leaderboard', [UsersController::class, 'leaderboard']);
Route::get('users/{user}', [UsersController::class, 'show']);