diff --git a/app/Actions/Forum/CreateThreadAction.php b/app/Actions/Forum/CreateThreadAction.php new file mode 100644 index 00000000..45f97686 --- /dev/null +++ b/app/Actions/Forum/CreateThreadAction.php @@ -0,0 +1,29 @@ +create($formValues); + + app(SubscribeToThreadAction::class)->execute($thread); + + givePoint(new ThreadCreated($thread)); + + event(new ThreadWasCreated($thread)); + + return $thread; + }); + + } +} diff --git a/app/Actions/Forum/UpdateThreadAction.php b/app/Actions/Forum/UpdateThreadAction.php new file mode 100644 index 00000000..6068380e --- /dev/null +++ b/app/Actions/Forum/UpdateThreadAction.php @@ -0,0 +1,22 @@ +update($formValues); + + return $thread; + }); + + } +} diff --git a/app/Livewire/Components/Slideovers/ThreadForm.php b/app/Livewire/Components/Slideovers/ThreadForm.php index 9b1073bb..94429dab 100644 --- a/app/Livewire/Components/Slideovers/ThreadForm.php +++ b/app/Livewire/Components/Slideovers/ThreadForm.php @@ -4,10 +4,9 @@ namespace App\Livewire\Components\Slideovers; -use App\Actions\Forum\SubscribeToThreadAction; -use App\Events\ThreadWasCreated; +use App\Actions\Forum\CreateThreadAction; +use App\Actions\Forum\UpdateThreadAction; use App\Exceptions\UnverifiedUserException; -use App\Gamify\Points\ThreadCreated; use App\Livewire\Traits\WithAuthenticatedUser; use App\Models\Thread; use Filament\Forms; @@ -122,18 +121,11 @@ public function save(): void $validated = $this->form->getState(); - if ($this->thread?->id) { - $this->thread->update($validated); - $this->form->model($this->thread)->saveRelationships(); - } else { - $thread = Thread::query()->create($validated); - $this->form->model($thread)->saveRelationships(); - - app(SubscribeToThreadAction::class)->execute($thread); + $thread = ($this->thread?->id) + ? app(UpdateThreadAction::class)->execute($validated, $this->thread) + : app(CreateThreadAction::class)->execute($validated); - givePoint(new ThreadCreated($thread)); - event(new ThreadWasCreated($thread)); - } + $this->form->model($thread)->saveRelationships(); Notification::make() ->title( @@ -144,7 +136,7 @@ public function save(): void ->success() ->send(); - $this->redirect(route('forum.show', ['thread' => $thread ?? $this->thread]), navigate: true); + $this->redirect(route('forum.show', ['thread' => $thread]), navigate: true); } public function render(): View diff --git a/tests/Feature/Actions/Forum/CreateThreadActionTest.php b/tests/Feature/Actions/Forum/CreateThreadActionTest.php new file mode 100644 index 00000000..73b09207 --- /dev/null +++ b/tests/Feature/Actions/Forum/CreateThreadActionTest.php @@ -0,0 +1,38 @@ +user = $this->login(); + + Event::fake(); + Notification::fake(); +}); + +it('user can create a thread', function (): void { + $channels = Channel::factory()->count(2)->create(); + + $thread = app(CreateThreadAction::class)->execute([ + 'title' => 'thread title', + 'slug' => 'thread-title', + 'user_id' => $this->user->id, + 'body' => 'This is a test action thread for created or updated thread.', + 'channels' => [$channels->modelKeys()], + ]); + + expect($thread) + ->toBeInstanceOf(Thread::class) + ->and($thread->user_id) + ->toBe($this->user->id); + + Event::assertDispatched(ThreadWasCreated::class); +}); diff --git a/tests/Feature/Actions/Forum/UpdateThreadActionTest.php b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php new file mode 100644 index 00000000..94dbeaa4 --- /dev/null +++ b/tests/Feature/Actions/Forum/UpdateThreadActionTest.php @@ -0,0 +1,38 @@ +user = $this->login(); + + Event::fake(); + Notification::fake(); +}); + +it('user can edit a thread', function (): void { + $thread = Thread::factory()->create(['user_id' => $this->user->id]); + $channels = Channel::factory()->count(3)->create(); + + $thread->channels()->attach($channels->modelKeys()); + + $thread = app(UpdateThreadAction::class)->execute([ + 'title' => 'update thread title', + ], $thread); + + expect($thread) + ->toBeInstanceOf(Thread::class) + ->and($thread->title) + ->toBe('update thread title'); + + Event::assertNotDispatched(ThreadWasCreated::class); + +});