Skip to content

Commit 8fe19d6

Browse files
feat:[lar-134] use theadId in action , remove array variable etc...
1 parent 9e68938 commit 8fe19d6

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

app/Actions/Forum/CreateOrUpdateThreadAction.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
final class CreateOrUpdateThreadAction
1313
{
14-
public function execute(array $formValues, ?Thread $thread = null): Thread
14+
public function execute(array $formValues, ?int $threadId = null): Thread
1515
{
16-
return DB::transaction(function () use ($formValues, $thread) {
17-
$edit = (bool) $thread?->id;
18-
$thread = Thread::query()->updateOrCreate(['id' => $thread?->id], $formValues);
16+
return DB::transaction(function () use ($formValues, $threadId) {
17+
$thread = Thread::query()->updateOrCreate(['id' => $threadId], $formValues);
1918

20-
if (! $edit) {
19+
if (! $threadId) {
2120
app(SubscribeToThreadAction::class)->execute($thread);
2221

2322
givePoint(new ThreadCreated($thread));

app/Livewire/Components/Slideovers/ThreadForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function save(): void
122122

123123
$thread = app(CreateOrUpdateThreadAction::class)->execute(
124124
formValues: $validated,
125-
thread: $this->thread
125+
threadId: $this->thread?->id
126126
);
127127

128128
$this->form->model($thread)->saveRelationships();
@@ -136,7 +136,7 @@ public function save(): void
136136
->success()
137137
->send();
138138

139-
$this->redirect(route('forum.show', ['thread' => $thread ?? $this->thread]), navigate: true); // @phpstan-ignore-line
139+
$this->redirect(route('forum.show', ['thread' => $thread]), navigate: true);
140140
}
141141

142142
public function render(): View

tests/Feature/Actions/Forum/CreateOrUpdateThreadActionTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@
1313

1414
beforeEach(function (): void {
1515
$this->user = $this->login();
16+
1617
Event::fake();
1718
Notification::fake();
1819
});
1920

2021
it('user can create a thread', function (): void {
21-
$channelOne = Channel::factory()->create(['name' => 'channel 1', 'slug' => 'channel-1']);
22-
$channelTwo = Channel::factory()->create(['name' => 'channel 2', 'slug' => 'channel-2']);
23-
$threadData = [
22+
$channels = Channel::factory()->count(2)->create();
23+
24+
$thread = app(createOrUpdateThreadAction::class)->execute([
2425
'title' => 'thread title',
2526
'slug' => 'thread-title',
2627
'user_id' => $this->user->id,
2728
'body' => 'This is a test action thread for created or updated thread.',
28-
'channels' => [$channelOne->id, $channelTwo->id],
29-
];
30-
31-
$thread = app(createOrUpdateThreadAction::class)->execute($threadData);
29+
'channels' => [$channels->first(), $channels->last()],
30+
]);
3231

3332
expect($thread)
3433
->toBeInstanceOf(Thread::class)
@@ -44,11 +43,9 @@
4443

4544
$thread->channels()->attach($channels->modelKeys());
4645

47-
$threadData = [
46+
$thread = app(createOrUpdateThreadAction::class)->execute([
4847
'title' => 'update thread title',
49-
];
50-
51-
$thread = app(createOrUpdateThreadAction::class)->execute($threadData, $thread);
48+
], $thread->id);
5249

5350
expect($thread)
5451
->toBeInstanceOf(Thread::class)

0 commit comments

Comments
 (0)