Skip to content

Commit

Permalink
Merge pull request #30 from DanielRTRD/user-guest-like-test
Browse files Browse the repository at this point in the history
User guest like
  • Loading branch information
dsbilling authored Mar 25, 2022
2 parents f8429bf + 50983e9 commit 5e251ad
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/public/storage
/storage/*.key
/vendor
/.idea
.env
.env.backup
.phpunit.result.cache
Expand Down
25 changes: 10 additions & 15 deletions app/Http/Livewire/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,30 @@
namespace App\Http\Livewire;

use App\Models\Post;
use App\Models\User;
use Livewire\Component;

class Like extends Component
{
public Post $post;
public int $count;
public $user;

public function mount(Post $post)
{
$this->post = $post;
$this->count = $post->likes_count;
$this->count = $post->likes()->count();
$this->user = auth()->check() ? auth()->user() : null;
}

public function like()
{
if ($this->post->isLiked()) {
$this->post->removeLike();
$this->count--;
} elseif (auth()->user()) {
$this->post->likes()->create([
'user_id' => auth()->id(),
]);
$this->count++;
} elseif (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
$this->post->likes()->create([
'ip' => $ip,
'user_agent' => $userAgent,
]);
$this->count++;
if ($this->post->hasLiked($this->user)) {
$this->post->dislike($this->user);
$this->count = $this->post->likes()->count();
} else {
$this->post->like($this->user);
$this->count = $this->post->likes()->count();
}
}

Expand Down
34 changes: 2 additions & 32 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
use Kilobyteno\LaravelUserGuestLike\Traits\HasUserGuestLike;

class Post extends Model implements Viewable
{
use HasFactory;
use InteractsWithViews;
use SoftDeletes;
use HasTags;
use HasUserGuestLike;

protected $removeViewsOnDelete = true;

Expand Down Expand Up @@ -70,37 +71,6 @@ public function user()
return $this->belongsTo(User::class);
}

public function likes()
{
return $this->hasMany(PostLike::class);
}

public function isLiked()
{
if (auth()->user()) {
return auth()->user()->likes()->forPost($this)->count();
}

if (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
return $this->likes()->forIp($ip)->forUserAgent($userAgent)->count();
}

return false;
}

public function removeLike()
{
if (auth()->user()) {
return auth()->user()->likes()->forPost($this)->delete();
}

if (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
return $this->likes()->forIp($ip)->forUserAgent($userAgent)->delete();
}

return false;
}

public function scopeIsPublished($query)
{
return $query->where('draft', false)->where('published_at', '<=', now());
Expand Down
32 changes: 0 additions & 32 deletions app/Models/PostLike.php

This file was deleted.

5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "laravel/laravel",
"name": "danielrtrd/daniel.rtrd.no",
"type": "project",
"description": "The Laravel Framework.",
"description": "Just my personal website",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
Expand All @@ -12,6 +12,7 @@
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"kilobyteno/laravel-user-guest-like": "^0.0.1",
"laravel/framework": "^8.40",
"laravel/jetstream": "^2.3",
"laravel/sanctum": "^2.6",
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

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

11 changes: 11 additions & 0 deletions config/user-guest-like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [

// Let guests like a model
'guest_like_enabled' => true,

// Save IP and user agent to database
'user_tracking_enabled' => false,

];
35 changes: 0 additions & 35 deletions database/migrations/2021_12_31_122354_create_post_likes_table.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('user_guest_likes', function (Blueprint $table) {
$table->id();
$table->morphs('model');
$table->nullableMorphs('author');
$table->ipAddress('ip')->nullable();
$table->string('user_agent')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('user_guest_likes');
}
};
4 changes: 2 additions & 2 deletions resources/views/livewire/like.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span class="flex items-center w-full mt-12 text-3xl align-center">
<button wire:click="like" wire:loading.attr="disabled" class="flex mx-auto {{ $post->isLiked() ? 'text-orange-500 hover:text-orange-400' : 'text-gray-400 hover:text-orange-300' }} focus:outline-none focus:ring-0 disabled:text-gray-700">
@if($post->isLiked())
<button wire:click="like" wire:loading.attr="disabled" class="flex mx-auto {{ $post->hasLiked($user) ? 'text-orange-500 hover:text-orange-400' : 'text-gray-400 hover:text-orange-300' }} focus:outline-none focus:ring-0 disabled:text-gray-700">
@if($post->hasLiked($user))
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block w-8 h-8" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" clip-rule="evenodd" />
</svg>
Expand Down

0 comments on commit 5e251ad

Please sign in to comment.