diff --git a/app/Http/Controllers/HuntStepsController.php b/app/Http/Controllers/HuntStepsController.php index 550e70e..5b3bb11 100644 --- a/app/Http/Controllers/HuntStepsController.php +++ b/app/Http/Controllers/HuntStepsController.php @@ -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(Request $request, \App\Models\Hunts $hunt) + public function store(\App\Http\Requests\StoreHuntStepRequest $request, \App\Models\Hunts $hunt) { if ($hunt->creator_id !== auth()->id()) { return response()->json(['message' => 'Unauthorized'], 403); } - $validated = $request->validate([ - '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', - ]); + $validated = $request->validated(); $step = $hunt->steps()->create($validated); diff --git a/app/Http/Controllers/HuntsController.php b/app/Http/Controllers/HuntsController.php index 1268126..bc0c558 100644 --- a/app/Http/Controllers/HuntsController.php +++ b/app/Http/Controllers/HuntsController.php @@ -28,24 +28,9 @@ class HuntsController extends Controller /** * Store a newly created resource in storage. */ - public function store(Request $request) + public function store(\App\Http\Requests\StoreHuntRequest $request) { - $validated = $request->validate([ - '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 = $request->validated(); $validated['creator_id'] = auth()->id(); @@ -126,7 +111,7 @@ class HuntsController extends Controller /** * 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); @@ -134,23 +119,7 @@ class HuntsController extends Controller 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', - ]); + $validated = $request->validated(); $hunt->update($validated); diff --git a/app/Http/Requests/StoreHuntRequest.php b/app/Http/Requests/StoreHuntRequest.php new file mode 100644 index 0000000..c400d87 --- /dev/null +++ b/app/Http/Requests/StoreHuntRequest.php @@ -0,0 +1,59 @@ +|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)); + } +} diff --git a/app/Http/Requests/StoreHuntStepRequest.php b/app/Http/Requests/StoreHuntStepRequest.php new file mode 100644 index 0000000..c017692 --- /dev/null +++ b/app/Http/Requests/StoreHuntStepRequest.php @@ -0,0 +1,54 @@ +|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)); + } +} diff --git a/app/Http/Requests/UpdateHuntRequest.php b/app/Http/Requests/UpdateHuntRequest.php new file mode 100644 index 0000000..530ed75 --- /dev/null +++ b/app/Http/Requests/UpdateHuntRequest.php @@ -0,0 +1,65 @@ +|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)); + } +}