File tree Expand file tree Collapse file tree 15 files changed +79
-30
lines changed
Expand file tree Collapse file tree 15 files changed +79
-30
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,16 @@ final class LikeTweetAction
1414 private $ tweetRepository ;
1515 private $ likeRepository ;
1616
17+ private const ADD_LIKE_STATUS = 'added ' ;
18+ private const REMOVE_LIKE_STATUS = 'removed ' ;
19+
1720 public function __construct (TweetRepository $ tweetRepository , LikeRepository $ likeRepository )
1821 {
1922 $ this ->tweetRepository = $ tweetRepository ;
2023 $ this ->likeRepository = $ likeRepository ;
2124 }
2225
23- public function execute (LikeTweetRequest $ request ): void
26+ public function execute (LikeTweetRequest $ request ): LikeTweetResponse
2427 {
2528 $ tweet = $ this ->tweetRepository ->getById ($ request ->getTweetId ());
2629
@@ -30,12 +33,14 @@ public function execute(LikeTweetRequest $request): void
3033 if ($ this ->likeRepository ->existsForTweetByUser ($ tweet ->id , $ userId )) {
3134 $ this ->likeRepository ->deleteForTweetByUser ($ tweet ->id , $ userId );
3235
33- return ;
36+ return new LikeTweetResponse ( self :: REMOVE_LIKE_STATUS ) ;
3437 }
3538
3639 $ like = new Like ();
3740 $ like ->forTweet (Auth::id (), $ tweet ->id );
3841
3942 $ this ->likeRepository ->save ($ like );
43+
44+ return new LikeTweetResponse (self ::ADD_LIKE_STATUS );
4045 }
4146}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace App \Action \Tweet ;
6+
7+ final class LikeTweetResponse
8+ {
9+ private $ status ;
10+
11+ public function __construct (string $ status )
12+ {
13+ $ this ->status = $ status ;
14+ }
15+
16+ public function getStatus (): string
17+ {
18+ return $ this ->status ;
19+ }
20+ }
Original file line number Diff line number Diff line change @@ -37,8 +37,6 @@ final class Like extends Model
3737 'likeable_type '
3838 ];
3939
40- protected $ with = ['user ' ];
41-
4240 public function likeable (): MorphTo
4341 {
4442 return $ this ->morphTo ();
@@ -56,7 +54,7 @@ public function getId(): int
5654
5755 public function getCreatedAt (): Carbon
5856 {
59- return $ this ->created_at ;
57+ return Carbon:: createFromTimeString ( $ this ->created_at ) ;
6058 }
6159
6260 public function getUserId (): int
Original file line number Diff line number Diff line change @@ -29,9 +29,9 @@ final class Tweet extends Model
2929 protected $ table = 'tweets ' ;
3030
3131 // Relations to eager load on every query.
32- protected $ with = ['author ' ];
32+ protected $ with = ['author ' , ' likes ' ];
3333
34- // Eager load related comments count each time.
34+ // Eager load related entities count each time.
3535 protected $ withCount = ['comments ' , 'likes ' ];
3636
3737 protected $ fillable = [
Original file line number Diff line number Diff line change 22
33namespace App \Events ;
44
5- use Illuminate \Broadcasting \Channel ;
65use Illuminate \Queue \SerializesModels ;
76use Illuminate \Broadcasting \PrivateChannel ;
8- use Illuminate \Broadcasting \PresenceChannel ;
97use Illuminate \Foundation \Events \Dispatchable ;
108use Illuminate \Broadcasting \InteractsWithSockets ;
119use Illuminate \Contracts \Broadcasting \ShouldBroadcast ;
1210use App \Entity \Tweet ;
13- use App \Http \Presenter \Tweet \ TweetArrayPresenter ;
11+ use App \Http \Presenter \TweetArrayPresenter ;
1412use Illuminate \Support \Facades \App ;
1513
1614class TweetAddedEvent implements ShouldBroadcast
Original file line number Diff line number Diff line change 1515use App \Action \Auth \UploadProfileImageAction ;
1616use App \Action \Auth \UploadProfileImageRequest ;
1717use App \Http \Controllers \ApiController ;
18- use App \Http \Presenter \Auth \ AuthenticationResponseArrayPresenter ;
19- use App \Http \Presenter \User \ UserArrayPresenter ;
18+ use App \Http \Presenter \AuthenticationResponseArrayPresenter ;
19+ use App \Http \Presenter \UserArrayPresenter ;
2020use App \Http \Request \Api \Auth \RegisterHttpRequest ;
2121use App \Http \Request \Api \Auth \LoginHttpRequest ;
2222use App \Http \Request \Api \Auth \UpdateProfileHttpRequest ;
Original file line number Diff line number Diff line change 1212use App \Action \GetByIdRequest ;
1313use App \Action \GetCollectionRequest ;
1414use App \Http \Controllers \ApiController ;
15- use App \Http \Presenter \Comment \ CommentAsArrayPresenter ;
15+ use App \Http \Presenter \CommentAsArrayPresenter ;
1616use App \Http \Request \Api \AddCommentHttpRequest ;
1717use App \Http \Response \ApiResponse ;
1818use App \Http \Request \Api \CollectionHttpRequest ;
Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ public function __construct(LikeTweetAction $likeTweetAction)
2020
2121 public function likeOrDislikeTweet (string $ id ): ApiResponse
2222 {
23- $ this ->likeTweetAction ->execute (new LikeTweetRequest ((int )$ id ));
23+ $ response = $ this ->likeTweetAction ->execute (new LikeTweetRequest ((int )$ id ));
2424
25- return $ this ->createEmptyResponse ( );
25+ return $ this ->createSuccessResponse ([ ' status ' => $ response -> getStatus ()] );
2626 }
2727}
Original file line number Diff line number Diff line change 1919use App \Action \Tweet \UploadTweetImageAction ;
2020use App \Action \Tweet \UploadTweetImageRequest ;
2121use App \Http \Controllers \ApiController ;
22- use App \Http \Presenter \Tweet \ TweetArrayPresenter ;
22+ use App \Http \Presenter \TweetArrayPresenter ;
2323use App \Http \Request \Api \CollectionHttpRequest ;
2424use App \Http \Request \Api \Tweet \AddTweetHttpRequest ;
2525use App \Http \Request \Api \Tweet \UpdateTweetHttpRequest ;
Original file line number Diff line number Diff line change 99use App \Action \User \GetUserByIdAction ;
1010use App \Action \User \GetUserCollectionAction ;
1111use App \Http \Controllers \ApiController ;
12- use App \Http \Presenter \User \ UserArrayPresenter ;
12+ use App \Http \Presenter \UserArrayPresenter ;
1313use App \Http \Request \Api \CollectionHttpRequest ;
1414use App \Http \Response \ApiResponse ;
1515
You can’t perform that action at this time.
0 commit comments