From 8f6926c1cc8d4a6370aef170498a9efdf1afb0e3 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Jul 2023 19:35:38 +0200 Subject: [PATCH] Add Decks CRUD --- src/app/Http/Controllers/DeckController.php | 28 ++++ src/app/Http/Livewire/DeckForm.php | 101 +++++++++++ src/app/Models/Deck.php | 12 ++ .../2023_07_08_142515_create_decks_table.php | 3 + src/resources/views/decks.blade.php | 22 +++ .../views/livewire/deck-form.blade.php | 158 ++++++++++++++++++ 6 files changed, 324 insertions(+) create mode 100644 src/app/Http/Controllers/DeckController.php create mode 100644 src/app/Http/Livewire/DeckForm.php create mode 100644 src/resources/views/decks.blade.php create mode 100644 src/resources/views/livewire/deck-form.blade.php diff --git a/src/app/Http/Controllers/DeckController.php b/src/app/Http/Controllers/DeckController.php new file mode 100644 index 0000000..71e91ce --- /dev/null +++ b/src/app/Http/Controllers/DeckController.php @@ -0,0 +1,28 @@ + ['required'], + 'uname' => ['required', 'unique:App\Models\Deck', 'string', 'size:4'], + 'description' => ['nullable'], + 'card_count' => ['required', 'numeric'], + 'cover_path' => ['required','image'], + 'backside_path' => ['required','image'], + 'author' => ['nullable'], + 'publisher' => ['nullable'], + 'type' => ['nullable'], + ]; + + public function storeDeck() + { + $this->validate(); + $deck = Deck::create([ + 'name' => $this->name, + 'uname' =>$this->uname, + 'description' => $this->description, + 'card_count' => $this->card_count, + 'cover_path' => $this->cover_path, + 'backside_path' => $this->backside_path, + 'author' => $this->author, + 'publisher' => $this->publisher, + 'type' => $this->type + ]); + $this->reset(); + } + + public function edit($id) + { + $deck = Deck::find($id); + $this->deck_id = $deck->id; + $this->name = $deck->name; + $this->uname = $deck->uname; + $this->description = $deck->description; + $this->card_count = $deck->card_count; + $this->cover_path = $deck->cover_path; + $this->backside_path = $deck->backside_path; + $this->author = $deck->author; + $this->publisher = $deck->publisher; + $this->type = $deck->type; + } + + public function update() + { + $deck = Deck::updateOrCreate( + [ + 'id' => $this->deck_id, + ], + [ + 'name' => $this->name, + 'uname' =>$this->uname, + 'description' => $this->description, + 'card_count' => $this->card_count, + 'cover_path' => $this->cover_path, + 'backside_path' => $this->backside_path, + 'author' => $this->author, + 'publisher' => $this->publisher, + 'type' => $this->type + ], + + ); + + $this->reset(); + } + + public function destroy($id) + { + Deck::destroy($id); + } + + public function render() + { + return view('livewire.deck-form', ['decks' => Deck::latest()->paginate(10)]); + } +} diff --git a/src/app/Models/Deck.php b/src/app/Models/Deck.php index f42dab4..9288624 100644 --- a/src/app/Models/Deck.php +++ b/src/app/Models/Deck.php @@ -9,6 +9,18 @@ class Deck extends Model { use HasFactory; + protected $fillable = [ + 'name', + 'uname', + 'describtion', + 'card_count', + 'cover_path', + 'backside_path', + 'author', + 'publisher', + 'type' + ]; + /** * The tags that belong to the Deck * diff --git a/src/database/migrations/2023_07_08_142515_create_decks_table.php b/src/database/migrations/2023_07_08_142515_create_decks_table.php index c88888f..286eb4c 100644 --- a/src/database/migrations/2023_07_08_142515_create_decks_table.php +++ b/src/database/migrations/2023_07_08_142515_create_decks_table.php @@ -14,10 +14,13 @@ return new class extends Migration Schema::create('decks', function (Blueprint $table) { $table->id(); $table->string('name'); + $table->string('uname'); $table->string('description')->nullable(); $table->integer('card_count'); $table->string('cover_path'); $table->string('backside_path'); + $table->string('author')->nullable(); + $table->string('publisher')->nullable(); $table->string('type')->nullable(); $table->timestamps(); }); diff --git a/src/resources/views/decks.blade.php b/src/resources/views/decks.blade.php new file mode 100644 index 0000000..08398ce --- /dev/null +++ b/src/resources/views/decks.blade.php @@ -0,0 +1,22 @@ + + + + + Decks + +
+ +
+
+
+
+ +
+
+
+
+ +
+
+
diff --git a/src/resources/views/livewire/deck-form.blade.php b/src/resources/views/livewire/deck-form.blade.php new file mode 100644 index 0000000..8e1ec39 --- /dev/null +++ b/src/resources/views/livewire/deck-form.blade.php @@ -0,0 +1,158 @@ +
+

Deck

+
+
+
+ @csrf +
+ + + @error('name') + {{ $message }} + @enderror +
+
+ + + @error('uname') + {{ $message }} + @enderror +
+
+ + + @error('description') + {{ $message }} + @enderror +
+
+ + + @error('card_count') + {{ $message }} + @enderror +
+
+ + + @error('cover_path') + {{ $message }} + @enderror +
+
+ + + @error('backside_path') + {{ $message }} + @enderror +
+
+ + + @error('author') + {{ $message }} + @enderror +
+
+ + + @error('publisher') + {{ $message }} + @enderror +
+
+ + + @error('type') + {{ $message }} + @enderror +
+ + +
+
+
+
+
+ + + + + + + + + + + + + @foreach($decks as $deck) + + + + + + + + + + + + + @endforeach + +
+ Id + + Name + + U-Name + + Edit + + Delete +
+
+
+
+ {{ $deck->id }} +
+
+
+
+
+ {{ $deck->name }} +
+
+
+ {{ $deck->uname }} +
+
+ + + +
+
+ {{ $decks->links() }} +
+
+
+