forked from eatvio/eatvio-infra
same at 22.05.2023
This commit is contained in:
67
app/Http/Livewire/BlogPost/Comment/Upvote.php
Normal file
67
app/Http/Livewire/BlogPost/Comment/Upvote.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
namespace App\Http\Livewire\BlogPost\Comment;
|
||||
|
||||
use Livewire\Component;
|
||||
use App\Models\BlogPostComment;
|
||||
|
||||
class Upvote extends Component
|
||||
{
|
||||
public BlogPostComment $comment;
|
||||
|
||||
public function mount(BlogPostComment $comment)
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
$upvotes = $this->comment->upvotes()->forBlogPostCommentsUpVote($this->comment)->count();
|
||||
$downvotes = $this->comment->upvotes()->forBlogPostCommentsDownVote($this->comment)->count();
|
||||
$sum = $upvotes - $downvotes;
|
||||
return view(
|
||||
'livewire.blog-post.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(),
|
||||
'blog_post_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(),
|
||||
'blog_post_comment_id' => $this->comment->id,
|
||||
'type' => 'downvote'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user