|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Livewire\Albums; |
| 6 | + |
| 7 | +use App\Enums\CollectionStatus; |
| 8 | +use App\Enums\OwnershipStatus; |
| 9 | +use App\Models\Album; |
| 10 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 11 | +use Illuminate\Support\Facades\Auth; |
| 12 | +use Illuminate\Validation\Rule; |
| 13 | +use Livewire\Component; |
| 14 | + |
| 15 | +class AlbumForm extends Component |
| 16 | +{ |
| 17 | + use AuthorizesRequests; |
| 18 | + |
| 19 | + public ?Album $album = null; |
| 20 | + |
| 21 | + public string $title = ''; |
| 22 | + |
| 23 | + public string $artist = ''; |
| 24 | + |
| 25 | + public string $genre = ''; |
| 26 | + |
| 27 | + public string $styles = ''; |
| 28 | + |
| 29 | + public ?int $year = null; |
| 30 | + |
| 31 | + public string $format = ''; |
| 32 | + |
| 33 | + public string $label = ''; |
| 34 | + |
| 35 | + public string $country = ''; |
| 36 | + |
| 37 | + public string $cover_url = ''; |
| 38 | + |
| 39 | + public string $status = 'wishlist'; |
| 40 | + |
| 41 | + public string $ownership = 'not_owned'; |
| 42 | + |
| 43 | + public ?int $rating = null; |
| 44 | + |
| 45 | + public ?int $discogs_id = null; |
| 46 | + |
| 47 | + public ?int $discogs_master_id = null; |
| 48 | + |
| 49 | + public string $notes = ''; |
| 50 | + |
| 51 | + public function mount(?Album $album = null): void |
| 52 | + { |
| 53 | + if ($album && $album->exists) { |
| 54 | + $this->authorize('update', $album); |
| 55 | + $this->album = $album; |
| 56 | + $this->fill([ |
| 57 | + 'title' => $album->title, |
| 58 | + 'artist' => $album->artist ?? '', |
| 59 | + 'genre' => is_array($album->genre) ? implode(', ', $album->genre) : '', |
| 60 | + 'styles' => is_array($album->styles) ? implode(', ', $album->styles) : '', |
| 61 | + 'year' => $album->year, |
| 62 | + 'format' => $album->format ?? '', |
| 63 | + 'label' => $album->label ?? '', |
| 64 | + 'country' => $album->country ?? '', |
| 65 | + 'cover_url' => $album->cover_url ?? '', |
| 66 | + 'status' => $album->status->value, |
| 67 | + 'ownership' => $album->ownership?->value ?? 'not_owned', |
| 68 | + 'rating' => $album->rating, |
| 69 | + 'discogs_id' => $album->discogs_id, |
| 70 | + 'discogs_master_id' => $album->discogs_master_id, |
| 71 | + 'notes' => $album->notes ?? '', |
| 72 | + ]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public function rules(): array |
| 77 | + { |
| 78 | + return [ |
| 79 | + 'title' => ['required', 'string', 'max:255'], |
| 80 | + 'artist' => ['nullable', 'string', 'max:255'], |
| 81 | + 'genre' => ['nullable', 'string', 'max:500'], |
| 82 | + 'styles' => ['nullable', 'string', 'max:500'], |
| 83 | + 'year' => ['nullable', 'integer', 'min:1900', 'max:2100'], |
| 84 | + 'format' => ['nullable', 'string', 'max:255'], |
| 85 | + 'label' => ['nullable', 'string', 'max:255'], |
| 86 | + 'country' => ['nullable', 'string', 'max:255'], |
| 87 | + 'cover_url' => ['nullable', 'url', 'max:2048'], |
| 88 | + 'status' => ['required', Rule::enum(CollectionStatus::class)], |
| 89 | + 'ownership' => ['required', Rule::enum(OwnershipStatus::class)], |
| 90 | + 'rating' => ['nullable', 'integer', 'min:1', 'max:5'], |
| 91 | + 'discogs_id' => ['nullable', 'integer'], |
| 92 | + 'discogs_master_id' => ['nullable', 'integer'], |
| 93 | + 'notes' => ['nullable', 'string', 'max:10000'], |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + public function save(): void |
| 98 | + { |
| 99 | + $validated = $this->validate(); |
| 100 | + |
| 101 | + $genreArray = ! empty($validated['genre']) |
| 102 | + ? array_map('trim', explode(',', $validated['genre'])) |
| 103 | + : []; |
| 104 | + |
| 105 | + $stylesArray = ! empty($validated['styles']) |
| 106 | + ? array_map('trim', explode(',', $validated['styles'])) |
| 107 | + : []; |
| 108 | + |
| 109 | + $data = [ |
| 110 | + 'title' => $validated['title'], |
| 111 | + 'artist' => $validated['artist'] ?: null, |
| 112 | + 'genre' => $genreArray, |
| 113 | + 'styles' => $stylesArray, |
| 114 | + 'year' => $validated['year'], |
| 115 | + 'format' => $validated['format'] ?: null, |
| 116 | + 'label' => $validated['label'] ?: null, |
| 117 | + 'country' => $validated['country'] ?: null, |
| 118 | + 'cover_url' => $validated['cover_url'] ?: null, |
| 119 | + 'status' => $validated['status'], |
| 120 | + 'ownership' => $validated['ownership'], |
| 121 | + 'rating' => $validated['rating'], |
| 122 | + 'discogs_id' => $validated['discogs_id'], |
| 123 | + 'discogs_master_id' => $validated['discogs_master_id'], |
| 124 | + 'notes' => $validated['notes'] ?: null, |
| 125 | + ]; |
| 126 | + |
| 127 | + if ($this->album) { |
| 128 | + $this->album->update($data); |
| 129 | + $message = 'Album updated successfully.'; |
| 130 | + } else { |
| 131 | + $data['user_id'] = Auth::id(); |
| 132 | + $this->album = Album::create($data); |
| 133 | + $message = 'Album created successfully.'; |
| 134 | + } |
| 135 | + |
| 136 | + session()->flash('message', $message); |
| 137 | + |
| 138 | + $this->redirect(route('albums.show', $this->album)); |
| 139 | + } |
| 140 | + |
| 141 | + public function isEditing(): bool |
| 142 | + { |
| 143 | + return $this->album !== null && $this->album->exists; |
| 144 | + } |
| 145 | + |
| 146 | + public function render() |
| 147 | + { |
| 148 | + return view('livewire.albums.album-form', [ |
| 149 | + 'statuses' => CollectionStatus::cases(), |
| 150 | + 'ownershipStatuses' => OwnershipStatus::cases(), |
| 151 | + 'isEditing' => $this->isEditing(), |
| 152 | + ])->layout('layouts.app'); |
| 153 | + } |
| 154 | +} |
0 commit comments