From 6a12f115d7ee40686ab3be0db99c67739f2fe5b6 Mon Sep 17 00:00:00 2001 From: Giulio Troccoli-Allard Date: Tue, 3 Sep 2024 18:22:05 +0100 Subject: [PATCH] Added Team CRUD --- app/Livewire/Forms/TeamForm.php | 52 ++++++++++++++++ app/Livewire/Teams/Create.php | 37 +++++++++++ app/Livewire/Teams/Edit.php | 37 +++++++++++ app/Livewire/Teams/Index.php | 61 +++++++++++++++++++ app/Livewire/Teams/Show.php | 25 ++++++++ .../views/components/navigation.blade.php | 2 +- .../views/livewire/team/create.blade.php | 12 ++++ resources/views/livewire/team/edit.blade.php | 13 ++++ resources/views/livewire/team/form.blade.php | 35 +++++++++++ resources/views/livewire/team/index.blade.php | 27 ++++++++ resources/views/livewire/team/show.blade.php | 11 ++++ routes/web.php | 6 +- 12 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 app/Livewire/Forms/TeamForm.php create mode 100644 app/Livewire/Teams/Create.php create mode 100644 app/Livewire/Teams/Edit.php create mode 100644 app/Livewire/Teams/Index.php create mode 100644 app/Livewire/Teams/Show.php create mode 100644 resources/views/livewire/team/create.blade.php create mode 100644 resources/views/livewire/team/edit.blade.php create mode 100644 resources/views/livewire/team/form.blade.php create mode 100644 resources/views/livewire/team/index.blade.php create mode 100644 resources/views/livewire/team/show.blade.php diff --git a/app/Livewire/Forms/TeamForm.php b/app/Livewire/Forms/TeamForm.php new file mode 100644 index 00000000..465e439c --- /dev/null +++ b/app/Livewire/Forms/TeamForm.php @@ -0,0 +1,52 @@ + 'required|uuid|exists:clubs,id', + 'name' => 'required|string', + 'venue_id' => 'string|uuid|exists:venues,id', + ]; + } + + public function setTeamModel(Team $teamModel): void + { + $this->teamModel = $teamModel; + + $this->club_id = $this->teamModel->club_id; + $this->clubName = $this->teamModel->club?->name; + $this->name = $this->teamModel->name; + $this->venue_id = $this->teamModel->venue_id; + } + + public function store(): void + { + $this->teamModel->create($this->validate()); + + $this->reset(); + } + + public function update(): void + { + $this->teamModel->update($this->validate()); + + $this->reset(); + } +} diff --git a/app/Livewire/Teams/Create.php b/app/Livewire/Teams/Create.php new file mode 100644 index 00000000..b8ee67aa --- /dev/null +++ b/app/Livewire/Teams/Create.php @@ -0,0 +1,37 @@ +form->setTeamModel($team); + } + + public function save(): void + { + $this->form->store(); + + $this->redirectRoute('teams.index', navigate: true); + } + + #[Layout('layouts.app')] + public function render(): View + { + return view('livewire.team.create', [ + 'clubs' => Club::query()->orderBy('name')->get(), + 'venues' => Venue::query()->orderBy('name')->get(), + ]); + } +} diff --git a/app/Livewire/Teams/Edit.php b/app/Livewire/Teams/Edit.php new file mode 100644 index 00000000..cfd66458 --- /dev/null +++ b/app/Livewire/Teams/Edit.php @@ -0,0 +1,37 @@ +form->setTeamModel($team); + } + + public function save(): void + { + $this->form->update(); + + $this->redirectRoute('teams.index', navigate: true); + } + + #[Layout('layouts.app')] + public function render(): View + { + return view('livewire.team.edit', [ + 'clubs' => Club::query()->orderBy('name')->get(), + 'venues' => Venue::query()->orderBy('name')->get(), + ]); + } +} diff --git a/app/Livewire/Teams/Index.php b/app/Livewire/Teams/Index.php new file mode 100644 index 00000000..4db99925 --- /dev/null +++ b/app/Livewire/Teams/Index.php @@ -0,0 +1,61 @@ +orderBy('name')->get(); + $this->clubId = $clubs->first()->getKey(); + + $this->filters = [ + 'clubs' => [ + 'label' => 'Clubs', + 'options' => $clubs, + 'currentOption' => $this->clubId, + 'event' => 'club-selected', + ], + ]; + } + + #[Layout('layouts.app')] + public function render(): View + { + $teams = Team::query() + ->where('club_id', $this->clubId) + ->with(['club', 'venue']) + ->orderBy('name') + ->simplePaginate(10); + + return view('livewire.team.index', compact('teams')) + ->with('i', $this->getPage() * $teams->perPage()); + } + + public function delete(Team $team): void + { + $team->delete(); + + $this->redirectRoute('teams.index', navigate: true); + } + + #[On('club-selected')] + public function setCurrentClub($clubId): void + { + $this->clubId = $clubId; + } +} diff --git a/app/Livewire/Teams/Show.php b/app/Livewire/Teams/Show.php new file mode 100644 index 00000000..a8645330 --- /dev/null +++ b/app/Livewire/Teams/Show.php @@ -0,0 +1,25 @@ +form->setTeamModel($team); + } + + #[Layout('layouts.app')] + public function render(): View + { + return view('livewire.team.show', ['team' => $this->form->teamModel]); + } +} diff --git a/resources/views/components/navigation.blade.php b/resources/views/components/navigation.blade.php index 198f9e01..21080810 100644 --- a/resources/views/components/navigation.blade.php +++ b/resources/views/components/navigation.blade.php @@ -15,7 +15,7 @@ Clubs - + Teams diff --git a/resources/views/livewire/team/create.blade.php b/resources/views/livewire/team/create.blade.php new file mode 100644 index 00000000..ced66cfd --- /dev/null +++ b/resources/views/livewire/team/create.blade.php @@ -0,0 +1,12 @@ +Teams + +
+ Create a new team + + +
+ @csrf + @include('livewire.team.form') +
+
+
diff --git a/resources/views/livewire/team/edit.blade.php b/resources/views/livewire/team/edit.blade.php new file mode 100644 index 00000000..56c146d6 --- /dev/null +++ b/resources/views/livewire/team/edit.blade.php @@ -0,0 +1,13 @@ +Teams + +
+ Update the {{ $this->form->teamModel->name }} team + + +
+ {{ method_field('PATCH') }} + @csrf + @include('livewire.team.form') +
+
+
diff --git a/resources/views/livewire/team/form.blade.php b/resources/views/livewire/team/form.blade.php new file mode 100644 index 00000000..bd28772d --- /dev/null +++ b/resources/views/livewire/team/form.blade.php @@ -0,0 +1,35 @@ +
+
+ + @empty($form->club_id) + + @else + + @endempty + @error('form.club_id') + + @enderror +
+
+ + + @error('form.name') + + @enderror +
+
+ + + @error('form.venue_id') + + @enderror +
+ +
+ Save +
+
diff --git a/resources/views/livewire/team/index.blade.php b/resources/views/livewire/team/index.blade.php new file mode 100644 index 00000000..8159110f --- /dev/null +++ b/resources/views/livewire/team/index.blade.php @@ -0,0 +1,27 @@ +Teams + +
+ A list of all the teams in the system + + + + @foreach ($teams as $team) + + {{ $team->name }} + {{ $team->venue->name }} + + + + + Are you sure you want to delete team {{ $team->name }}?" + + + + @endforeach + + +
+ {!! $teams->withQueryString()->links() !!} +
+
+
diff --git a/resources/views/livewire/team/show.blade.php b/resources/views/livewire/team/show.blade.php new file mode 100644 index 00000000..fffebb71 --- /dev/null +++ b/resources/views/livewire/team/show.blade.php @@ -0,0 +1,11 @@ +Teams + +
+ Details of the {{ $team->name }} team + + + {{ $team->club->name }} + {{ $team->name }} + + +
diff --git a/routes/web.php b/routes/web.php index 72b36619..290dcbcb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -32,7 +32,11 @@ Route::get('/clubs/show/{club}', \App\Livewire\Clubs\Show::class)->name('clubs.show'); Route::get('/clubs/update/{club}', \App\Livewire\Clubs\Edit::class)->name('clubs.edit'); - Route::view('teams', 'teams')->name('teams'); + Route::get('/teams', \App\Livewire\Teams\Index::class)->name('teams.index'); + Route::get('/teams/create', \App\Livewire\Teams\Create::class)->name('teams.create'); + Route::get('/teams/show/{team}', \App\Livewire\Teams\Show::class)->name('teams.show'); + Route::get('/teams/update/{team}', \App\Livewire\Teams\Edit::class)->name('teams.edit'); + Route::view('venues', 'venues')->name('venues'); });