145 lines
4.5 KiB
PHP
145 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Hunts;
|
|
use Illuminate\Http\Request;
|
|
|
|
class HuntsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
try {
|
|
$hunts = Hunts::with('participants')->paginate(10);
|
|
return response()->json($hunts);
|
|
} catch (\Exception $e) {
|
|
\Illuminate\Support\Facades\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.
|
|
*/
|
|
public function store(\App\Http\Requests\StoreHuntRequest $request)
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
$validated['creator_id'] = auth()->id();
|
|
|
|
$hunt = Hunts::create($validated);
|
|
|
|
return response()->json($hunt, 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Request $request, $id)
|
|
{
|
|
$hunt = Hunts::with(['steps', 'participants'])->find($id);
|
|
|
|
if (!$hunt) {
|
|
return response()->json(['message' => 'Hunt not found'], 404);
|
|
}
|
|
|
|
$user = $request->user('sanctum');
|
|
|
|
if ($user) {
|
|
$participation = $user->participations()->where('hunt_id', $hunt->id)->first();
|
|
|
|
if ($participation) {
|
|
// User is a participant
|
|
$currentStepNumber = $participation->pivot->current_step_number;
|
|
|
|
// Filter steps
|
|
$hunt->setRelation('steps', $hunt->steps->filter(function ($step) use ($currentStepNumber) {
|
|
if ($step->step_number < $currentStepNumber) {
|
|
return true; // Show past steps fully
|
|
}
|
|
if ($step->step_number == $currentStepNumber) {
|
|
// Hide hints for current step
|
|
$step->makeHidden(['hint_text', 'hint_media_url']);
|
|
return true;
|
|
}
|
|
return false; // Hide future steps
|
|
})->values());
|
|
|
|
$hunt->participation_status = $participation->pivot;
|
|
} elseif ($hunt->creator_id !== $user->id) {
|
|
// User is logged in but not participant and not creator
|
|
// Show only first step or nothing specific (keep as is for now, maybe hide all steps or just show basic info)
|
|
// For now, let's just hide hints of all steps if not participating?
|
|
// Or maybe just show nothing of steps if not participating?
|
|
// Requirement: "fetch the hunt in question it retrieves me the hunt steps already passed and the next one" implying context of participation.
|
|
// If not participating, maybe just return basic hunt info without steps?
|
|
$hunt->unsetRelation('steps');
|
|
}
|
|
// If creator, show everything (default)
|
|
} else {
|
|
// Guest
|
|
$hunt->unsetRelation('steps');
|
|
}
|
|
|
|
return response()->json($hunt);
|
|
}
|
|
|
|
public function participate(Request $request, $id)
|
|
{
|
|
$hunt = Hunts::findOrFail($id);
|
|
$user = $request->user();
|
|
|
|
if ($user->participations()->where('hunt_id', $hunt->id)->exists()) {
|
|
return response()->json(['message' => 'Already participating'], 409);
|
|
}
|
|
|
|
$hunt->participants()->attach($user->id, ['current_step_number' => 1, 'status' => 'started']);
|
|
|
|
return response()->json(['message' => 'Participation started'], 201);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(\App\Http\Requests\UpdateHuntRequest $request, $id)
|
|
{
|
|
$hunt = Hunts::find($id);
|
|
|
|
if (!$hunt) {
|
|
return response()->json(['message' => 'Hunt not found'], 404);
|
|
}
|
|
|
|
$validated = $request->validated();
|
|
|
|
$hunt->update($validated);
|
|
|
|
return response()->json($hunt);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$hunt = Hunts::find($id);
|
|
|
|
if (!$hunt) {
|
|
return response()->json(['message' => 'Hunt not found'], 404);
|
|
}
|
|
|
|
$hunt->delete();
|
|
|
|
return response()->json(['message' => 'Hunt deleted successfully']);
|
|
}
|
|
}
|