forked from laravelio/laravel.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6da1775
commit ea92bd3
Showing
15 changed files
with
357 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ Homestead.yaml | |
.env | ||
/aliases | ||
/after.sh | ||
.phpunit.result.cache | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class CannotLikeReplyTwice extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: bobbyborisov | ||
* Date: 12/3/17 | ||
* Time: 1:51 PM | ||
*/ | ||
|
||
namespace App\Helpers; | ||
|
||
use App\Models\Like; | ||
use App\User; | ||
|
||
trait HasLikes | ||
{ | ||
protected static function bootHasLikes() | ||
{ | ||
static::deleting(function ($model){ | ||
$model->likes->each->delete(); | ||
}); | ||
} | ||
|
||
public function likedBy(User $user) | ||
{ | ||
$this->likes()->create(['user_id' => $user->id]); | ||
} | ||
|
||
public function dislikedBy(User $user) | ||
{ | ||
$this->likes()->where('user_id', $user->id)->get()->first()->delete(); | ||
} | ||
|
||
public function likes() | ||
{ | ||
return $this->morphMany(Like::class, 'liked'); | ||
} | ||
|
||
public function isLikedBy(User $user) | ||
{ | ||
return $this->likes()->where('user_id', $user->id)->exists(); | ||
} | ||
|
||
public function getLikesCountAttribute() | ||
{ | ||
return $this->likes->count(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\Reply; | ||
use App\User; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
|
||
class DislikeReply implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* @var \App\Models\Reply | ||
*/ | ||
private $reply; | ||
|
||
/** | ||
* @var \App\Jobs\User | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param \App\Models\Reply $reply | ||
* @param \App\Jobs\User $user | ||
*/ | ||
public function __construct(Reply $reply, User $user) | ||
{ | ||
$this->reply = $reply; | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$this->reply->dislikedBy($this->user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Exceptions\CannotLikeReplyTwice; | ||
use App\Models\Reply; | ||
use App\User; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Database\QueryException; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
|
||
class LikeReply implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* @var \App\Models\Reply | ||
*/ | ||
private $reply; | ||
|
||
/** | ||
* @var \App\User | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param \App\Models\Reply $reply | ||
* @param \App\User $user | ||
*/ | ||
public function __construct(Reply $reply, User $user) | ||
{ | ||
$this->reply = $reply; | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
* @throws \App\Exceptions\CannotLikeReplyTwice | ||
*/ | ||
public function handle() | ||
{ | ||
try { | ||
$this->reply->likedBy($this->user); | ||
} catch (QueryException $exception) { | ||
throw new CannotLikeReplyTwice('Sorry, you cannot like a reply twice'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Like extends Model | ||
{ | ||
protected $fillable = ['user_id']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
database/migrations/2017_12_03_111900_create_likes_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateLikesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('likes', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('user_id'); | ||
$table->unsignedInteger('liked_id'); | ||
$table->string('liked_type'); | ||
$table->timestamps(); | ||
|
||
$table->unique(['user_id', 'liked_id' , 'liked_type']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('likes'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Tests\Components\Jobs; | ||
|
||
use App\Jobs\DislikeReply; | ||
use App\Models\Reply; | ||
use App\User; | ||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
|
||
class DislikeReplyTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
/** @test */ | ||
public function we_can_dislike_a_reply() | ||
{ | ||
$user = factory(User::class)->create(); | ||
$reply = factory(Reply::class)->create(); | ||
|
||
$reply->likedBy($user); | ||
$this->assertTrue($reply->fresh()->isLikedBy($user)); | ||
|
||
$this->dispatch(new DislikeReply($reply, $user)); | ||
|
||
$this->assertFalse($reply->fresh()->isLikedBy($user)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\Components\Jobs; | ||
|
||
use App\Exceptions\CannotLikeReplyTwice; | ||
use App\Jobs\LikeReply; | ||
use App\Models\Reply; | ||
use App\User; | ||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
|
||
class LikeReplyTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
/** @test */ | ||
public function we_can_like_a_reply() | ||
{ | ||
$user = factory(User::class)->create(); | ||
$reply = factory(Reply::class)->create(); | ||
|
||
$this->dispatch(new LikeReply($reply, $user)); | ||
|
||
$this->assertTrue($reply->fresh()->isLikedBy($user)); | ||
} | ||
|
||
/** @test */ | ||
public function we_cannot_like_a_reply_twice() | ||
{ | ||
$user = factory(User::class)->create(); | ||
$reply = factory(Reply::class)->create(); | ||
|
||
$this->dispatch(new LikeReply($reply, $user)); | ||
|
||
$this->assertTrue($reply->fresh()->isLikedBy($user)); | ||
|
||
$this->expectException(CannotLikeReplyTwice::class); | ||
$this->dispatch(new LikeReply($reply, $user)); | ||
} | ||
} |
Oops, something went wrong.