mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 18:52:10 +00:00
101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Player;
|
|
use App\Models\Room;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class JoinRoomRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'join_code' => strtoupper(trim((string) $this->input('join_code'))),
|
|
'player_name' => trim((string) $this->input('player_name')),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$roomId = Room::query()
|
|
->where('join_code', $this->input('join_code'))
|
|
->value('id');
|
|
|
|
$existingPlayerId = null;
|
|
|
|
if ($roomId !== null && $this->user() !== null) {
|
|
$existingPlayerId = Player::query()
|
|
->where('room_id', $roomId)
|
|
->where('user_id', $this->user()->id)
|
|
->value('id');
|
|
} elseif ($roomId !== null) {
|
|
$sessionPlayerId = $this->session()->get("room_players.{$roomId}");
|
|
$existingPlayerId = is_numeric($sessionPlayerId)
|
|
? Player::query()
|
|
->where('room_id', $roomId)
|
|
->whereKey((int) $sessionPlayerId)
|
|
->value('id')
|
|
: null;
|
|
}
|
|
|
|
$playerNameRules = ['required', 'string', 'max:50'];
|
|
|
|
if ($existingPlayerId === null) {
|
|
$playerNameRules[] = Rule::unique(Player::class, 'name')->where('room_id', $roomId);
|
|
}
|
|
|
|
return [
|
|
'join_code' => [
|
|
'bail',
|
|
'string',
|
|
'required',
|
|
'size:6',
|
|
Rule::exists(Room::class, 'join_code')->whereNull('deleted_at'),
|
|
],
|
|
'player_name' => $playerNameRules,
|
|
];
|
|
}
|
|
|
|
/** @return array<callable(Validator): void> */
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
$room = Room::query()
|
|
->where('join_code', $this->input('join_code'))
|
|
->first();
|
|
|
|
if ($room?->isClosed()) {
|
|
$validator->errors()->add('join_code', 'Dieser Raum ist geschlossen.');
|
|
}
|
|
},
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'join_code.exists' => 'Dieser Raumcode existiert nicht.',
|
|
'join_code.size' => 'Der Raumcode muss aus sechs Zeichen bestehen.',
|
|
'player_name.unique' => 'Dieser Name wird in dem Raum bereits verwendet.',
|
|
];
|
|
}
|
|
}
|