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 App\Models\Hunts;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class HuntsController extends Controller class HuntsController extends Controller
{ {
@ -15,36 +17,80 @@ class HuntsController extends Controller
try { try {
$query = Hunts::with('participants:id,avatar_uri'); $query = Hunts::with('participants:id,avatar_uri');
if ($request->boolean('participating')) { if ($request->has('participating')) {
$userId = auth('sanctum')->id(); $participating = filter_var(
if ($userId) { $request->query('participating'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
);
$userId = auth('sanctum')->id();
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) { $query->whereHas('participants', function ($q) use ($userId) {
$q->where('user_id', $userId); $q->where('user_id', $userId);
}); });
} else { } else {
// User wants participating hunts but is not logged in -> return nothing // Hunts où l'utilisateur NE participe PAS
$query->whereRaw('1 = 0'); $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(); $now = now();
$query->where('start_at', '<=', $now) $active = filter_var($request->query('active'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
->where('end_at', '>=', $now);
if ($active === true) {
// Actives: start_at <= now AND (end_at is null OR end_at >= now)
$query->where('start_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); $hunts = $query->paginate(10);
return response()->json($hunts); return response()->json($hunts);
} catch (\Exception $e) { } catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error('Error fetching hunts: ' . $e->getMessage()); Log::error('Error fetching hunts: ' . $e->getMessage());
return response()->json([ return response()->json([
'error' => 'Server Error', 'error' => 'Server Error',
'message' => $e->getMessage(), 'message' => $e->getMessage(),
'trace' => $e->getTraceAsString()
], 500); ], 500);
} }
} }
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
*/ */

View File

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