Files
eatvio-infra/app/Livewire/Diet/Comment/Upvote.php
2024-01-15 17:11:55 +03:00

73 lines
1.8 KiB
PHP

<?php
namespace App\Livewire\Diet\Comment;
use App\Models\DietComment;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\View\View;
use Livewire\Component;
class Upvote extends Component
{
public DietComment $comment;
public function mount(DietComment $comment): void
{
$this->comment = $comment;
}
public function render(): View|ViewFactory
{
$upvotes = $this->comment->upvotes()->forDietCommentsUpVote($this->comment)->count();
$downvotes = $this->comment->upvotes()->forDietCommentsDownVote($this->comment)->count();
$sum = $upvotes - $downvotes;
return view(
'livewire.diet.comment.upvote',
[
'sum' => $sum,
'isUpVoted' => $this->comment->isUpVoted(),
'isDownVoted' => $this->comment->isDownVoted(),
]
);
}
public function upvote(): void
{
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
return;
}
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'comment_id' => $this->comment->id,
'type' => 'upvote',
]);
}
public function downvote(): void
{
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
return;
}
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'comment_id' => $this->comment->id,
'type' => 'downvote',
]);
}
}