Skip to content

feat:[lar-140] Remove point after deleted article ,thread and discussion #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/Livewire/Pages/Discussions/SingleDiscussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Livewire\Pages\Discussions;

use App\Actions\Discussion\ConvertDiscussionToThreadAction;
use App\Gamify\Points\DiscussionCreated;
use App\Models\Discussion;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
Expand All @@ -13,6 +14,7 @@
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

final class SingleDiscussion extends Component implements HasActions, HasForms
Expand Down Expand Up @@ -80,7 +82,17 @@ public function deleteAction(): Action
->authorize('delete', $this->discussion)
->requiresConfirmation()
->successNotificationTitle(__('notifications.discussion.deleted'))
->successRedirectUrl(route('discussions.index'));
->action(function (): void {
DB::beginTransaction();

undoPoint(new DiscussionCreated($this->discussion));

$this->discussion->delete();

DB::commit();

$this->redirectRoute('discussions.index', navigate: true);
});
}

public function render(): View
Expand Down
7 changes: 7 additions & 0 deletions app/Livewire/Pages/Forum/DetailThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace App\Livewire\Pages\Forum;

use App\Gamify\Points\ThreadCreated;
use App\Models\Thread;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
Expand Down Expand Up @@ -53,8 +55,13 @@ public function deleteAction(): Action
->authorize('delete', $this->thread)
->requiresConfirmation()
->action(function (): void {
DB::beginTransaction();

undoPoint(new ThreadCreated($this->thread));
$this->thread->delete();

DB::commit();

$this->redirectRoute('forum.index', navigate: true);
});
}
Expand Down
88 changes: 44 additions & 44 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
->assertSuccessful();
});

it('user can create a new thread', function (): void {
it('user can create a new discussion', function (): void {
$user = $this->login();
$tags = Tag::factory()->count(3)->create();

Expand All @@ -36,10 +36,13 @@
->assertHasNoFormErrors();

$discussion = Discussion::query()->first();
$user->refresh();

expect($discussion?->user)->toBeInstanceOf(User::class)
->and($discussion?->user->is($user))
->toBeTrue();
->toBeTrue()
->and($user->getPoints())
->toBe(20);
});

it('validate forms input', function (): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@
->assertHasNoFormErrors();

$thread = Thread::query()->first();
$user->refresh();

expect($thread?->user)->toBeInstanceOf(User::class)
->and($thread?->user->is($user))
->toBeTrue();
->toBeTrue()
->and($user->getPoints())
->toBe(55);
});

it('user cannot create thread with and unverified email address', function (): void {
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/Livewire/Pages/Discussion/SIngleDiscussionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use App\Gamify\Points\DiscussionCreated;
use App\Livewire\Pages\Discussions\SingleDiscussion;
use App\Models\Discussion;
use App\Models\Tag;
use Livewire\Livewire;

it('delete user action can remove discussion point ', function (): void {
$user = $this->login();
$discussion = Discussion::factory()->create(['user_id' => $user->id]);
$tags = Tag::factory()->count(3)->create();

$discussion->tags()->attach($tags->modelKeys());

givePoint(new DiscussionCreated($discussion));

Livewire::test(SingleDiscussion::class, ['discussion' => $discussion])
->callAction('deleteAction')
->assertStatus(200);

$user->refresh();

expect($user->getPoints())
->toBe(0);

});
Loading
Loading