Skip to content

Commit 76b127b

Browse files
authored
Merge pull request #68 from BinaryStudioAcademy/feature/likes-logic
Feature/likes logic
2 parents 2036787 + c85112d commit 76b127b

15 files changed

+79
-30
lines changed

backend/app/Action/Tweet/LikeTweetAction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

backend/app/Entity/Like.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff 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

backend/app/Entity/Tweet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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 = [

backend/app/Events/TweetAddedEvent.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace App\Events;
44

5-
use Illuminate\Broadcasting\Channel;
65
use Illuminate\Queue\SerializesModels;
76
use Illuminate\Broadcasting\PrivateChannel;
8-
use Illuminate\Broadcasting\PresenceChannel;
97
use Illuminate\Foundation\Events\Dispatchable;
108
use Illuminate\Broadcasting\InteractsWithSockets;
119
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
1210
use App\Entity\Tweet;
13-
use App\Http\Presenter\Tweet\TweetArrayPresenter;
11+
use App\Http\Presenter\TweetArrayPresenter;
1412
use Illuminate\Support\Facades\App;
1513

1614
class TweetAddedEvent implements ShouldBroadcast

backend/app/Http/Controllers/Api/Auth/AuthController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use App\Action\Auth\UploadProfileImageAction;
1616
use App\Action\Auth\UploadProfileImageRequest;
1717
use 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;
2020
use App\Http\Request\Api\Auth\RegisterHttpRequest;
2121
use App\Http\Request\Api\Auth\LoginHttpRequest;
2222
use App\Http\Request\Api\Auth\UpdateProfileHttpRequest;

backend/app/Http/Controllers/Api/CommentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use App\Action\GetByIdRequest;
1313
use App\Action\GetCollectionRequest;
1414
use App\Http\Controllers\ApiController;
15-
use App\Http\Presenter\Comment\CommentAsArrayPresenter;
15+
use App\Http\Presenter\CommentAsArrayPresenter;
1616
use App\Http\Request\Api\AddCommentHttpRequest;
1717
use App\Http\Response\ApiResponse;
1818
use App\Http\Request\Api\CollectionHttpRequest;

backend/app/Http/Controllers/Api/LikeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

backend/app/Http/Controllers/Api/TweetController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use App\Action\Tweet\UploadTweetImageAction;
2020
use App\Action\Tweet\UploadTweetImageRequest;
2121
use App\Http\Controllers\ApiController;
22-
use App\Http\Presenter\Tweet\TweetArrayPresenter;
22+
use App\Http\Presenter\TweetArrayPresenter;
2323
use App\Http\Request\Api\CollectionHttpRequest;
2424
use App\Http\Request\Api\Tweet\AddTweetHttpRequest;
2525
use App\Http\Request\Api\Tweet\UpdateTweetHttpRequest;

backend/app/Http/Controllers/Api/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use App\Action\User\GetUserByIdAction;
1010
use App\Action\User\GetUserCollectionAction;
1111
use App\Http\Controllers\ApiController;
12-
use App\Http\Presenter\User\UserArrayPresenter;
12+
use App\Http\Presenter\UserArrayPresenter;
1313
use App\Http\Request\Api\CollectionHttpRequest;
1414
use App\Http\Response\ApiResponse;
1515

0 commit comments

Comments
 (0)