Files
eatvio-infra/app/Livewire/Recipe/Comment/Upvote.php
2023-10-04 17:57:09 +03:00

68 lines
1.7 KiB
PHP

<?php
namespace App\Livewire\Recipe\Comment;
use Livewire\Component;
use App\Models\RecipeComment;
class Upvote extends Component
{
public RecipeComment $comment;
public function mount(RecipeComment $comment)
{
$this->comment = $comment;
}
public function render()
{
$upvotes = $this->comment->upvotes()->forRecipeCommentsUpVote($this->comment)->count();
$downvotes = $this->comment->upvotes()->forRecipeCommentsDownVote($this->comment)->count();
$sum = $upvotes - $downvotes;
return view(
'livewire.recipe.comment.upvote',
[
'sum' => $sum,
'isUpVoted' => $this->comment->isUpVoted(),
'isDownVoted' => $this->comment->isDownVoted()
]
);
}
public function upvote()
{
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
return ;
}
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'recipe_comment_id' => $this->comment->id,
'type' => 'upvote'
]);
}
public function downvote()
{
if ($this->comment->isDownVoted()) {
$this->comment->removeDownVote();
return ;
}
if ($this->comment->isUpVoted()) {
$this->comment->removeUpVote();
}
$this->comment->upvotes()->create([
'user_id' => auth()->id(),
'recipe_comment_id' => $this->comment->id,
'type' => 'downvote'
]);
}
}