Character: add ability to edit a character.
This commit is contained in:
parent
d8895901bf
commit
b6f1f8b887
8 changed files with 217 additions and 70 deletions
|
|
@ -28,6 +28,15 @@ class CharacterController extends Controller
|
|||
return view('character.create');
|
||||
}
|
||||
|
||||
public function edit(Character $character)
|
||||
{
|
||||
$this->authorize('update', $character);
|
||||
|
||||
return view('character.edit', [
|
||||
'character' => $character
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Character $character)
|
||||
{
|
||||
$this->authorize('delete', $character);
|
||||
|
|
|
|||
|
|
@ -8,41 +8,22 @@ use App\Warcraft\Races;
|
|||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CreateCharacterForm extends Component
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
/**
|
||||
* Character's name
|
||||
*/
|
||||
public string $name = '';
|
||||
|
||||
/**
|
||||
* Character's level
|
||||
*/
|
||||
public string $level = '70';
|
||||
|
||||
/**
|
||||
* All possible classes
|
||||
*/
|
||||
public $classes;
|
||||
|
||||
/**
|
||||
* Character's class
|
||||
*/
|
||||
public string $class = '';
|
||||
|
||||
/**
|
||||
* All possible races
|
||||
*/
|
||||
public $races;
|
||||
|
||||
/**
|
||||
* Character's race
|
||||
*/
|
||||
public string $race = '';
|
||||
|
||||
/**
|
||||
* All posible genders
|
||||
*/
|
||||
|
|
@ -52,28 +33,39 @@ class CreateCharacterForm extends Component
|
|||
];
|
||||
|
||||
/**
|
||||
* Character gender
|
||||
* Character model
|
||||
*/
|
||||
public $gender = 'M';
|
||||
public Character $character;
|
||||
|
||||
/**
|
||||
* Validation rules
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected $rules = [
|
||||
'name' => 'required|alpha|min:2|max:12|unique:characters,name',
|
||||
'level' => 'required|integer|min:1|max:70',
|
||||
'gender' => 'required|in:M,F',
|
||||
'race' => 'required',
|
||||
'class' => 'required'
|
||||
];
|
||||
|
||||
public function mount()
|
||||
public function rules()
|
||||
{
|
||||
$this->races = (new Races)->alliance();
|
||||
$rules['race'] = 'required|in:' . $this->races->keys()->join(',');
|
||||
return [
|
||||
'character.name' => [ 'required', 'min:4', Rule::unique('characters', 'name')->ignore($this->character) ],
|
||||
'character.level' => [ 'required', 'integer', 'min:1', 'max:70' ],
|
||||
'character.gender' => [ 'required', 'in:M,F' ],
|
||||
'character.race' => [ 'required', 'in:' . $this->races->keys()->join(',') ],
|
||||
'character.class' => [ 'required', 'in:' . $this->classes->keys()->join(',') ]
|
||||
];
|
||||
}
|
||||
|
||||
$this->race = $this->races->keys()->first();
|
||||
$this->updatedRace($this->race);
|
||||
public function mount(Character $character)
|
||||
{
|
||||
$this->character = $character;
|
||||
$this->races = (new Races)->alliance();
|
||||
|
||||
// Set some default values.
|
||||
if (!$this->character->exists) {
|
||||
$this->character->race = $this->races->keys()->first();
|
||||
$this->character->gender = 'M';
|
||||
$this->character->level = 70;
|
||||
}
|
||||
|
||||
$this->updatedCharacterRace($this->character->race);
|
||||
}
|
||||
|
||||
public function updated($propertyName)
|
||||
|
|
@ -81,18 +73,15 @@ class CreateCharacterForm extends Component
|
|||
$this->validateOnly($propertyName);
|
||||
}
|
||||
|
||||
public function updatedRace($race)
|
||||
public function updatedCharacterRace($race)
|
||||
{
|
||||
// Update classes list for this race.
|
||||
$this->classes = (new Classes)->race($this->race);
|
||||
|
||||
// Update validation rules
|
||||
$rules['class'] = 'required|in:' . $this->classes->keys()->join(',');
|
||||
$this->classes = (new Classes)->race($race);
|
||||
|
||||
// if this race can not be the selected class.
|
||||
// select the first one.
|
||||
if (!$this->classes->has($this->class)) {
|
||||
$this->class = $this->classes->keys()->first();
|
||||
if (!$this->classes->has($this->character->class)) {
|
||||
$this->character->class = $this->classes->keys()->first();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,16 +90,25 @@ class CreateCharacterForm extends Component
|
|||
*/
|
||||
public function save()
|
||||
{
|
||||
$this->authorize('create', Character::class);
|
||||
if ($this->character->exists) {
|
||||
$this->authorize('update', $this->character);
|
||||
$action = "updated";
|
||||
} else {
|
||||
$this->authorize('create', Character::class);
|
||||
$action = "created";
|
||||
}
|
||||
|
||||
$data = $this->validate();
|
||||
$this->validate();
|
||||
|
||||
$user = auth()->user();
|
||||
$character = $user->characters()->create($data);
|
||||
if ($action === 'created') {
|
||||
$this->character->user()
|
||||
->associate(auth()->user());
|
||||
}
|
||||
$this->character->save();
|
||||
|
||||
// Livewire redirect() does not have "with" method.
|
||||
// so we call session()->flash() directly instead.
|
||||
session()->flash('success', "<strong>{$character->name}</strong> was created!");
|
||||
session()->flash('success', "<strong>{$this->character->name}</strong> was {$action}!");
|
||||
return redirect()->route('user.index');
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue