Skip to content

Commit f1b7f95

Browse files
committed
Make threads likeable
1 parent bda7fb8 commit f1b7f95

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class CannotLikeThreadMultipleTimes extends Exception
8+
{
9+
protected $message = 'A thread cannot be liked multiple times.';
10+
}

app/Http/Controllers/Forum/ThreadsController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use App\Http\Requests\ThreadRequest;
88
use App\Jobs\CreateThread;
99
use App\Jobs\DeleteThread;
10+
use App\Jobs\LikeThread;
1011
use App\Jobs\MarkThreadSolution;
1112
use App\Jobs\SubscribeToSubscriptionAble;
13+
use App\Jobs\UnlikeThread;
1214
use App\Jobs\UnmarkThreadSolution;
1315
use App\Jobs\UnsubscribeFromSubscriptionAble;
1416
use App\Jobs\UpdateThread;
@@ -133,4 +135,18 @@ public function unsubscribe(Request $request, Thread $thread)
133135

134136
return redirect()->route('thread', $thread->slug());
135137
}
138+
139+
public function like(Thread $thread)
140+
{
141+
$this->dispatchNow(new LikeThread($thread, auth()->user()));
142+
143+
return back();
144+
}
145+
146+
public function unlike(Thread $thread)
147+
{
148+
$this->dispatchNow(new UnlikeThread($thread, auth()->user()));
149+
150+
return back();
151+
}
136152
}

app/Jobs/LikeThread.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\User;
6+
use Illuminate\Database\QueryException;
7+
use App\Exceptions\CannotLikeThreadMultipleTimes;
8+
use App\Models\Thread;
9+
10+
class LikeThread
11+
{
12+
/**
13+
* @var \App\Models\Thread
14+
*/
15+
private $thread;
16+
17+
/**
18+
* @var \App\User
19+
*/
20+
private $user;
21+
22+
/**
23+
* Create a new job instance.
24+
*
25+
* @param \App\Models\Thread $thread
26+
* @param \App\User $user
27+
*/
28+
public function __construct(Thread $thread, User $user)
29+
{
30+
$this->thread = $thread;
31+
$this->user = $user;
32+
}
33+
34+
/**
35+
* Execute the job.
36+
*
37+
* @return void
38+
* @throws \App\Exceptions\CannotLikeReplyTwice
39+
*/
40+
public function handle()
41+
{
42+
try {
43+
$this->thread->likedBy($this->user);
44+
} catch (QueryException $exception) {
45+
throw new CannotLikeThreadMultipleTimes();
46+
}
47+
}
48+
}

app/Jobs/UnlikeThread.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\User;
6+
use App\Models\Thread;
7+
8+
class UnlikeThread
9+
{
10+
/**
11+
* @var \App\Models\Thread
12+
*/
13+
private $thread;
14+
15+
/**
16+
* @var \App\User
17+
*/
18+
private $user;
19+
20+
/**
21+
* Create a new job instance.
22+
*
23+
* @param \App\Models\Thread $thread
24+
* @param \App\User $user
25+
*/
26+
public function __construct(Thread $thread, User $user)
27+
{
28+
$this->thread = $thread;
29+
$this->user = $user;
30+
}
31+
32+
/**
33+
* Execute the job.
34+
*
35+
* @return void
36+
*/
37+
public function handle()
38+
{
39+
$this->thread->dislikedBy($this->user);
40+
}
41+
}

app/Models/Thread.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Exceptions\CouldNotMarkReplyAsSolution;
66
use App\Helpers\HasAuthor;
7+
use App\Helpers\HasLikes;
78
use App\Helpers\HasSlug;
89
use App\Helpers\HasTags;
910
use App\Helpers\HasTimestamps;
@@ -25,7 +26,7 @@
2526

2627
final class Thread extends Model implements ReplyAble, SubscriptionAble, Feedable
2728
{
28-
use HasAuthor, HasSlug, HasTimestamps, ModelHelpers, ProvidesSubscriptions, ReceivesReplies, HasTags;
29+
use HasAuthor, HasSlug, HasTimestamps, ModelHelpers, ProvidesSubscriptions, ReceivesReplies, HasTags, HasLikes;
2930

3031
const TABLE = 'threads';
3132

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
Route::put('{thread}/unmark-solution', 'ThreadsController@unmarkSolution')->name('threads.solution.unmark');
6363
Route::get('{thread}/subscribe', 'ThreadsController@subscribe')->name('threads.subscribe');
6464
Route::get('{thread}/unsubscribe', 'ThreadsController@unsubscribe')->name('threads.unsubscribe');
65+
Route::put('{thread}/like', 'ThreadsController@like')->name('threads.like');
66+
Route::delete('{thread}/unlike', 'ThreadsController@unlike')->name('threads.unlike');
6567

6668
Route::get('tags/{tag}', 'TagsController@show')->name('forum.tag');
6769
});

0 commit comments

Comments
 (0)