mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 14:52:09 +00:00
111 lines
3.3 KiB
PHP
111 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Player;
|
|
use App\Models\Room;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
use Illuminate\Validation\Rule;
|
|
use Inertia\Inertia;
|
|
|
|
class ShowEmbeddedRoomRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user() !== null;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'join_code' => $this->string('join_code')->trim()->upper()->toString(),
|
|
'player_name' => $this->string('player_name')->trim()->toString(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'join_code' => [
|
|
'bail',
|
|
'required',
|
|
'string',
|
|
'size:6',
|
|
Rule::exists(Room::class, 'join_code')->whereNull('deleted_at'),
|
|
],
|
|
'player_name' => ['required', 'string', 'max:50'],
|
|
];
|
|
}
|
|
|
|
/** @return array<callable(Validator): void> */
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
if ($validator->errors()->has('join_code')) {
|
|
return;
|
|
}
|
|
|
|
$room = Room::query()
|
|
->where('join_code', $this->input('join_code'))
|
|
->first();
|
|
|
|
if ($room?->isClosed()) {
|
|
$validator->errors()->add('join_code', 'Dieser Raum ist geschlossen.');
|
|
|
|
return;
|
|
}
|
|
|
|
if ($room === null || $this->user() === null) {
|
|
return;
|
|
}
|
|
|
|
$nameIsTaken = Player::query()
|
|
->where('room_id', $room->id)
|
|
->where('name', $this->input('player_name'))
|
|
->where(function ($query): void {
|
|
$query->whereNull('user_id')
|
|
->orWhere('user_id', '!=', $this->user()->id);
|
|
})
|
|
->exists();
|
|
|
|
if ($nameIsTaken) {
|
|
$validator->errors()->add(
|
|
'player_name',
|
|
'Dieser Anzeigename ist in dem Raum bereits vorhanden.',
|
|
);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'join_code.exists' => 'Dieser Raumcode existiert nicht.',
|
|
'join_code.size' => 'Der Raumcode muss aus sechs Zeichen bestehen.',
|
|
'player_name.required' => 'Bitte hinterlege in der Hub-App einen Anzeigenamen.',
|
|
];
|
|
}
|
|
|
|
protected function failedValidation(Validator $validator): never
|
|
{
|
|
throw new HttpResponseException(
|
|
Inertia::render('embedded/Error', [
|
|
'message' => $validator->errors()->first(),
|
|
])->toResponse($this)->setStatusCode(422),
|
|
);
|
|
}
|
|
}
|