api/app/Http/Controllers/HuntsController.php

120 lines
3.3 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::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(Request $request)
{
$validated = $request->validate([
'partner_id' => 'required|string',
'title' => 'required|string',
'slug' => 'required|string|unique:hunts,slug',
'description' => 'nullable|string',
'status' => 'required|string',
'difficulty' => 'required|string',
'city' => 'required|string',
'latitude' => 'nullable|numeric',
'longitude' => 'nullable|numeric',
'is_public' => 'boolean',
'estimated_duration_min' => 'nullable|integer',
'start_at' => 'nullable|date',
'end_at' => 'nullable|date',
'image' => 'nullable|string',
'creator_id' => 'nullable|exists:users,id',
]);
$hunt = Hunts::create($validated);
return response()->json($hunt, 201);
}
/**
* Display the specified resource.
*/
public function show($id)
{
$hunt = Hunts::find($id);
if (!$hunt) {
return response()->json(['message' => 'Hunt not found'], 404);
}
return response()->json($hunt);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
$hunt = Hunts::find($id);
if (!$hunt) {
return response()->json(['message' => 'Hunt not found'], 404);
}
$validated = $request->validate([
'partner_id' => 'sometimes|string',
'title' => 'sometimes|string',
'slug' => 'sometimes|string|unique:hunts,slug,' . $id,
'description' => 'nullable|string',
'status' => 'sometimes|string',
'difficulty' => 'sometimes|string',
'city' => 'sometimes|string',
'latitude' => 'nullable|numeric',
'longitude' => 'nullable|numeric',
'is_public' => 'boolean',
'estimated_duration_min' => 'nullable|integer',
'start_at' => 'nullable|date',
'end_at' => 'nullable|date',
'image' => 'nullable|string',
'creator_id' => 'nullable|exists:users,id',
]);
$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']);
}
}