Skip to content

Commit

Permalink
Added Club CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
troccoli committed Sep 3, 2024
1 parent 101c1ef commit f5f35aa
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 4 deletions.
35 changes: 35 additions & 0 deletions app/Livewire/Clubs/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Livewire\Clubs;

use App\Livewire\Forms\ClubForm;
use App\Models\Club;
use App\Models\Venue;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Create extends Component
{
public ClubForm $form;

public function mount(Club $club): void
{
$this->form->setClubModel($club);
}

public function save(): void
{
$this->form->store();

$this->redirectRoute('clubs.index', navigate: true);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.club.create', [
'venues' => Venue::query()->orderBy('name')->get(),
]);
}
}
35 changes: 35 additions & 0 deletions app/Livewire/Clubs/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Livewire\Clubs;

use App\Livewire\Forms\ClubForm;
use App\Models\Club;
use App\Models\Venue;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Edit extends Component
{
public ClubForm $form;

public function mount(Club $club): void
{
$this->form->setClubModel($club);
}

public function save(): void
{
$this->form->update();

$this->redirectRoute('clubs.index', navigate: true);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.club.edit', [
'venues' => Venue::query()->orderBy('name')->get(),
]);
}
}
33 changes: 33 additions & 0 deletions app/Livewire/Clubs/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Livewire\Clubs;

use App\Models\Club;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;

class Index extends Component
{
use WithPagination;

#[Layout('layouts.app')]
public function render(): View
{
$clubs = Club::query()
->orderBy('name')
->with('venue')
->simplePaginate(10);

return view('livewire.club.index', compact('clubs'))
->with('i', $this->getPage() * $clubs->perPage());
}

public function delete(Club $club): void
{
$club->delete();

$this->redirectRoute('clubs.index', navigate: true);
}
}
25 changes: 25 additions & 0 deletions app/Livewire/Clubs/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Livewire\Clubs;

use App\Livewire\Forms\ClubForm;
use App\Models\Club;
use Illuminate\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

class Show extends Component
{
public ClubForm $form;

public function mount(Club $club): void
{
$this->form->setClubModel($club);
}

#[Layout('layouts.app')]
public function render(): View
{
return view('livewire.club.show', ['club' => $this->form->clubModel]);
}
}
45 changes: 45 additions & 0 deletions app/Livewire/Forms/ClubForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Livewire\Forms;

use App\Models\Club;
use Livewire\Form;

class ClubForm extends Form
{
public ?Club $clubModel;

public ?string $name;

public ?string $venue_id;

public function rules(): array
{
return [
'name' => 'required|string',
'venue_id' => 'string|uuid|exists:venues,id',
];
}

public function setClubModel(Club $clubModel): void
{
$this->clubModel = $clubModel;

$this->name = $this->clubModel->name;
$this->venue_id = $this->clubModel->venue_id;
}

public function store(): void
{
$this->clubModel->create($this->validate());

$this->reset();
}

public function update(): void
{
$this->clubModel->update($this->validate());

$this->reset();
}
}
8 changes: 7 additions & 1 deletion app/Models/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Contracts\Selectable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -14,7 +15,7 @@
* @property-read Collection $teams
* @property-read ?Venue $venue
*/
class Club extends Model
class Club extends Model implements Selectable
{
use HasFactory,
HasUuids;
Expand All @@ -34,4 +35,9 @@ public function venue(): BelongsTo
{
return $this->belongsTo(Venue::class);
}

public function getName(): string
{
return $this->name;
}
}
8 changes: 7 additions & 1 deletion app/Models/Venue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Contracts\Selectable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -13,7 +14,7 @@
* @property-read Collection $clubs
* @property-read Collection teams
*/
class Venue extends Model
class Venue extends Model implements Selectable
{
use HasFactory,
HasUuids;
Expand All @@ -31,4 +32,9 @@ public function teams(): HasMany
{
return $this->hasMany(Team::class);
}

public function getName(): string
{
return $this->name;
}
}
2 changes: 1 addition & 1 deletion resources/views/components/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Divisions
</x-responsive-nav-link>
<hr class="hidden md:block"/>
<x-responsive-nav-link :href="route('clubs')" :active="request()->routeIs('clubs')" wire:navigate>
<x-responsive-nav-link :href="route('clubs.index')" :active="request()->routeIs('clubs.*')" wire:navigate>
Clubs
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('teams')" :active="request()->routeIs('teams')" wire:navigate>
Expand Down
12 changes: 12 additions & 0 deletions resources/views/livewire/club/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-crud.header>Clubs</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="clubs.index">Create a new clubs</x-crud.subheader>

<x-crud.content class="mt-8 max-w-xl">
<form method="POST" wire:submit="save" role="form" enctype="multipart/form-data">
@csrf
@include('livewire.club.form')
</form>
</x-crud.content>
</div>
13 changes: 13 additions & 0 deletions resources/views/livewire/club/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<x-crud.header>Clubs</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="clubs.index">Update the {{ $form->name }} club</x-crud.subheader>

<x-crud.content class="mt-8 max-w-xl">
<form method="POST" wire:submit="save" role="form" enctype="multipart/form-data">
{{ method_field('PATCH') }}
@csrf
@include('livewire.club.form')
</form>
</x-crud.content>
</div>
22 changes: 22 additions & 0 deletions resources/views/livewire/club/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="space-y-6">
<div>
<x-input-label for="name" value="Name" />
<x-text-input wire:model="form.name" id="name" name="name" type="text" class="mt-1 block w-full"
autocomplete="name" placeholder="Club's name" />
@error('form.name')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>
<div>
<x-input-label for="venue_id" value="Venue" />
<x-select-input wire:model="form.venue_id" id="venue_id" name="venue_id" class="mt-1 block w-full"
:options="$venues" placeholder="Choose a venue" />
@error('form.venue_id')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>

<div class="flex items-center gap-4">
<x-primary-button>Save</x-primary-button>
</div>
</div>
28 changes: 28 additions & 0 deletions resources/views/livewire/club/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<x-crud.header>Clubs</x-crud.header>

<div class="w-full">
<x-crud.subheader add-route="clubs.create" class="mb-4">
A list of all the clubs in the system
</x-crud.subheader>
<x-crud.content>
<x-crud.index.table columns="name,venue">
@foreach ($clubs as $club)
<x-crud.index.row row-key="{{ $club->getKey() }}">
<x-crud.index.cell>{{ $club->name }}</x-crud.index.cell>
<x-crud.index.cell>{{ $club->venue->name }}</x-crud.index.cell>
<x-crud.index.cell class="flex gap-1 pl-2 pr-0 font-medium">
<x-crud.index.show-button route="clubs.show" :model="$club" />
<x-crud.index.edit-button route="clubs.edit" :model="$club" />
<x-crud.index.delete-button :model="$club">
Are you sure you want to delete the {{ $club->name }} club?
</x-crud.index.delete-button>
</x-crud.index.cell>
</x-crud.index.row>
@endforeach
</x-crud.index.table>

<div class="mt-4 px-4">
{!! $clubs->withQueryString()->links() !!}
</div>
</x-crud.content>
</div>
11 changes: 11 additions & 0 deletions resources/views/livewire/club/show.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<x-crud.header>Clubs</x-crud.header>

<div class="w-full">
<x-crud.subheader back-route="clubs.index">Details of the {{ $club->name }} club</x-crud.subheader>
<x-crud.content>
<x-crud.show.table>
<x-crud.show.model-field label="Name">{{ $club->name }}</x-crud.show.model-field>
<x-crud.show.model-field label="Venue">{{ $club->venue->name }}</x-crud.show.model-field>
</x-crud.show.table>
</x-crud.content>
</div>
7 changes: 6 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
Route::get('/divisions/update/{division}', \App\Livewire\Divisions\Edit::class)->name('divisions.edit');

Route::view('fixtures', 'fixtures')->name('fixtures');
Route::view('clubs', 'clubs')->name('clubs');

Route::get('/clubs', \App\Livewire\Clubs\Index::class)->name('clubs.index');
Route::get('/clubs/create', \App\Livewire\Clubs\Create::class)->name('clubs.create');
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::view('venues', 'venues')->name('venues');
});
Expand Down

0 comments on commit f5f35aa

Please sign in to comment.