Skip to content

Commit

Permalink
feat: [LAR-28] update forum feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Nov 4, 2024
1 parent dfd59e0 commit 03100d2
Show file tree
Hide file tree
Showing 33 changed files with 283 additions and 235 deletions.
6 changes: 4 additions & 2 deletions app/Console/Commands/UpdateUserBestRepliesPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Gamify\Points\BestReply;
use App\Models\Thread;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

final class UpdateUserBestRepliesPoints extends Command
{
Expand All @@ -18,10 +19,11 @@ public function handle(): void
{
$this->info('Updating users bests replies reputations...');

$resolvedThread = Thread::resolved()->with('solutionReply')->get();
/** @var Collection | Thread[] $resolvedThread */
$resolvedThread = Thread::with('solutionReply')->scopes('resolved')->get();

foreach ($resolvedThread as $thread) {
givePoint(new BestReply($thread->solutionReply));
givePoint(new BestReply($thread->solutionReply)); // @phpstan-ignore-line
}

$this->info('All done!');
Expand Down
30 changes: 0 additions & 30 deletions app/Http/Controllers/ThreadController.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Livewire\Forum;
namespace App\Livewire\Components\Forum;

use App\Gamify\Points\BestReply;
use App\Models\Reply as ReplyModel;
Expand Down Expand Up @@ -83,6 +83,6 @@ public function solutionAction(): Action
#[On('reply.save.{reply.id}')]
public function render(): View
{
return view('livewire.forum.reply');
return view('livewire.components.forum.reply');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Livewire\Forum;
namespace App\Livewire\Components\Forum;

use App\Actions\Forum\CreateReplyAction;
use App\Models\Reply;
Expand Down Expand Up @@ -116,6 +116,6 @@ public function updateReply(): void

public function render(): View
{
return view('livewire.forum.reply-form');
return view('livewire.components.forum.reply-form');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Livewire\Forum;
namespace App\Livewire\Components\Forum;

use App\Actions\Forum\SubscribeToThreadAction;
use App\Models\Thread;
Expand All @@ -23,7 +23,7 @@ public function subscribe(): void
app(SubscribeToThreadAction::class)->execute($this->thread);

Notification::make()
->title(__('Vous êtes maintenant abonné à ce sujet.'))
->title(__('notifications.thread.subscribe'))
->success()
->duration(5000)
->send();
Expand All @@ -40,7 +40,7 @@ public function unsubscribe(): void
->delete();

Notification::make()
->title(__('Vous vous êtes désabonné de ce sujet.'))
->title(__('notifications.thread.unsubscribe'))
->success()
->duration(5000)
->send();
Expand All @@ -51,6 +51,6 @@ public function unsubscribe(): void
#[On('subscription.update')]
public function render(): View
{
return view('livewire.forum.subscribe');
return view('livewire.components.forum.subscribe');
}
}
69 changes: 0 additions & 69 deletions app/Livewire/Forum/EditThread.php

This file was deleted.

31 changes: 31 additions & 0 deletions app/Livewire/Pages/Forum/Channels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Pages\Forum;

use App\Models\Channel;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.forum')]
final class Channels extends Component
{
public function render(): View
{
return view('livewire.pages.forum.channels', [
'channels' => Channel::query()
->withCount('threads')
->whereNull('parent_id')
->get()
->sortByDesc('threads_count'),
'childChannels' => Channel::query()
->withCount('threads')
->whereNotNull('parent_id')
->get()
->sortByDesc('threads_count'),
])
->title(__('pages/forum.navigation.channels'));
}
}
90 changes: 82 additions & 8 deletions app/Livewire/Pages/Forum/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use App\Models\Thread;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithoutUrlPagination;
use Livewire\WithPagination;
Expand All @@ -20,12 +22,34 @@ final class Index extends Component
use WithoutUrlPagination;
use WithPagination;

#[Url(as: 'channel')]
public ?string $channel = null;

#[Url(as: 'solved')]
public ?string $solved = null;

#[Url(as: 'me')]
public ?string $user = null;

#[Url(as: 'no-replies')]
public ?string $unAnswered = null;

#[Url]
public ?string $subscribe = null;

public ?Channel $currentChannel = null;

public string $search = '';

public int $perPage = 30;

public function mount(): void
{
if ($this->channel) {
$this->currentChannel = Channel::findBySlug($this->channel);
}
}

#[On('channelUpdated')]
public function reloadThreads(?int $channelId): void
{
Expand All @@ -39,21 +63,71 @@ public function reloadThreads(?int $channelId): void
$this->dispatch('render');
}

public function render(): View
protected function applySearch(Builder $query): Builder
{
$query = Thread::with('channels')
->orderByDesc('created_at');
if ($this->search) {
return $query->where(function (Builder $query): void {
$query->where('title', 'like', '%'.$this->search.'%');
});
}

if ($this->currentChannel?->id) {
$query->scopes(['channel' => $this->currentChannel]);
return $query;
}

protected function applySolved(Builder $query): Builder
{
if ($this->solved) {
return match ($this->solved) {

Check failure on line 80 in app/Livewire/Pages/Forum/Index.php

View workflow job for this annotation

GitHub Actions / phpstan

Match expression does not handle remaining value: non-falsy-string
'no' => $query->scopes('unresolved'),
'yes' => $query->scopes('resolved'),
};
}

if ($this->search) {
$query->where(function (Builder $query): void {
$query->where('title', 'like', '%'.$this->search.'%');
return $query;
}

protected function applyAuthor(Builder $query): Builder
{
if (Auth::check() && $this->user) {
return $query->whereHas('user', function (Builder $query): void {
$query->where('user_id', Auth::id());
});
}

return $query;
}

protected function applyChannel(Builder $query): Builder
{
if ($this->currentChannel?->id) {
return $query->scopes(['channel' => $this->currentChannel]);
}

return $query;
}

protected function applySubscribe(Builder $query): Builder
{
return $query;
}

protected function applyUnAnswer(Builder $query): Builder
{
return $query;
}

public function render(): View
{
$query = Thread::with('channels')
->orderByDesc('created_at');

$query = $this->applyChannel($query);
$query = $this->applySearch($query);
$query = $this->applySolved($query);
$query = $this->applyAuthor($query);
$query = $this->applySubscribe($query);
$query = $this->applyUnAnswer($query);

$threads = $query
->scopes('withViewsCount')
->paginate($this->perPage);
Expand Down
8 changes: 5 additions & 3 deletions app/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,31 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Translatable\HasTranslations;

/**
* @property-read int $id
* @property string $name
* @property string $slug
* @property array $description
* @property string $color
* @property int | null $parent_id
*/
final class Channel extends Model
{
use HasFactory;
use HasSlug;
use HasTranslations;

protected $fillable = [
'name',
'slug',
'description',
'parent_id',
'color',
];

protected $withCount = [
'threads',
];
public array $translatable = ['description'];

protected static function boot(): void
{
Expand Down
Loading

0 comments on commit 03100d2

Please sign in to comment.