Skip to content

Commit 71a8252

Browse files
committed
Deck: create, index and show | Improve test coverage | Change logo and app name
1 parent 3fbd776 commit 71a8252

32 files changed

+728
-63
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
APP_NAME=Laravel
1+
APP_NAME="Flashcard Pro"
22
APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true

app/Livewire/Auth/Login.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Login extends Component
2525

2626
/**
2727
* Handle an incoming authentication request.
28+
*
29+
* @throws ValidationException
2830
*/
2931
public function login(): void
3032
{

app/Livewire/Cards/Create.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Livewire\Cards;
4+
5+
use Livewire\Component;
6+
7+
class Create extends Component
8+
{
9+
public function render()
10+
{
11+
return view('livewire.cards.create');
12+
}
13+
}

app/Livewire/Decks/Create.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Livewire\Decks;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Validation\Rule;
7+
use Livewire\Component;
8+
9+
class Create extends Component
10+
{
11+
public string $name = '';
12+
13+
protected function rules(): array
14+
{
15+
return [
16+
'name' => [
17+
'required',
18+
'string',
19+
'max:20',
20+
Rule::unique('decks', 'name')->where('user_id', Auth::id()),
21+
],
22+
];
23+
}
24+
25+
public function store(): void
26+
{
27+
$validated = $this->validate();
28+
29+
Auth::user()->decks()->create($validated);
30+
31+
$this->redirect(route('decks.index', absolute: false), navigate: true);
32+
}
33+
}

app/Livewire/Decks/Index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Livewire\Decks;
4+
5+
use Illuminate\Contracts\Support\Renderable;
6+
use Illuminate\Support\Facades\Auth;
7+
use Livewire\Component;
8+
use Livewire\WithPagination;
9+
10+
class Index extends Component
11+
{
12+
use WithPagination;
13+
14+
public function render(): Renderable
15+
{
16+
return view('livewire.decks.index', [
17+
'decks' => Auth::user()->decks()->latest('id')->paginate(),
18+
]);
19+
}
20+
}

app/Livewire/Decks/Show.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Livewire\Decks;
4+
5+
use App\Models\Deck;
6+
use Livewire\Component;
7+
8+
class Show extends Component
9+
{
10+
public Deck $deck;
11+
12+
public function mount(Deck $deck): void
13+
{
14+
$this->deck = $deck;
15+
}
16+
}

app/Models/Deck.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class Deck extends Model
10+
{
11+
/** @use HasFactory<\Database\Factories\DeckFactory> */
12+
use HasFactory;
13+
14+
protected $fillable = ['name'];
15+
16+
public function user(): BelongsTo
17+
{
18+
return $this->belongsTo(User::class);
19+
}
20+
}

app/Models/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// use Illuminate\Contracts\Auth\MustVerifyEmail;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Foundation\Auth\User as Authenticatable;
89
use Illuminate\Notifications\Notifiable;
910
use Illuminate\Support\Str;
@@ -58,4 +59,9 @@ public function initials(): string
5859
->map(fn ($word) => Str::substr($word, 0, 1))
5960
->implode('');
6061
}
62+
63+
public function decks(): HasMany
64+
{
65+
return $this->hasMany(Deck::class);
66+
}
6167
}

app/Policies/DeckPolicy.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Deck;
6+
use App\Models\User;
7+
8+
class DeckPolicy
9+
{
10+
public function view(User $user, Deck $deck): bool
11+
{
12+
return $user->id === $deck->user_id;
13+
}
14+
15+
public function create(User $user): bool
16+
{
17+
return true;
18+
}
19+
}

database/factories/DeckFactory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
/**
9+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Deck>
10+
*/
11+
class DeckFactory extends Factory
12+
{
13+
/**
14+
* Define the model's default state.
15+
*
16+
* @return array<string, mixed>
17+
*/
18+
public function definition(): array
19+
{
20+
return [
21+
'user_id' => User::factory(),
22+
'name' => $this->faker->unique()->words(2, true),
23+
];
24+
}
25+
}

0 commit comments

Comments
 (0)