feat: requests

This commit is contained in:
Leon 2025-12-19 14:11:30 +01:00
parent a0c55c435b
commit aa92b2e838
5 changed files with 184 additions and 50 deletions

View File

@ -26,26 +26,13 @@ class HuntStepsController extends Controller
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
*/ */
/** public function store(\App\Http\Requests\StoreHuntStepRequest $request, \App\Models\Hunts $hunt)
* Store a newly created resource in storage.
*/
public function store(Request $request, \App\Models\Hunts $hunt)
{ {
if ($hunt->creator_id !== auth()->id()) { if ($hunt->creator_id !== auth()->id()) {
return response()->json(['message' => 'Unauthorized'], 403); return response()->json(['message' => 'Unauthorized'], 403);
} }
$validated = $request->validate([ $validated = $request->validated();
'step_number' => 'required|integer',
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'hint_text' => 'required|string',
'hint_media_url' => 'nullable|string',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'radius_m' => 'integer|min:1',
'xp' => 'integer|min:0',
]);
$step = $hunt->steps()->create($validated); $step = $hunt->steps()->create($validated);

View File

@ -28,24 +28,9 @@ class HuntsController extends Controller
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
*/ */
public function store(Request $request) public function store(\App\Http\Requests\StoreHuntRequest $request)
{ {
$validated = $request->validate([ $validated = $request->validated();
'partner_id' => 'nullable|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',
]);
$validated['creator_id'] = auth()->id(); $validated['creator_id'] = auth()->id();
@ -126,7 +111,7 @@ class HuntsController extends Controller
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
*/ */
public function update(Request $request, $id) public function update(\App\Http\Requests\UpdateHuntRequest $request, $id)
{ {
$hunt = Hunts::find($id); $hunt = Hunts::find($id);
@ -134,23 +119,7 @@ class HuntsController extends Controller
return response()->json(['message' => 'Hunt not found'], 404); return response()->json(['message' => 'Hunt not found'], 404);
} }
$validated = $request->validate([ $validated = $request->validated();
'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); $hunt->update($validated);

View File

@ -0,0 +1,59 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class StoreHuntRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true; // Authorized by default, or check permission if needed
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'partner_id' => 'nullable|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',
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'message' => 'Validation errors',
'errors' => $validator->errors()
], 422));
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class StoreHuntStepRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'step_number' => 'required|integer',
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'hint_text' => 'required|string',
'hint_media_url' => 'nullable|string',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'radius_m' => 'integer|min:1',
'xp' => 'integer|min:0',
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'message' => 'Validation errors',
'errors' => $validator->errors()
], 422));
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rule;
class UpdateHuntRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'partner_id' => 'sometimes|string',
'title' => 'sometimes|string',
'slug' => [
'sometimes',
'string',
Rule::unique('hunts', 'slug')->ignore($this->route('hunt')),
],
'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',
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'message' => 'Validation errors',
'errors' => $validator->errors()
], 422));
}
}