-
Notifications
You must be signed in to change notification settings - Fork 2
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
22a6194
commit 28f3857
Showing
4 changed files
with
100 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Resources\ChallengeResource; | ||
use App\Services\Feed; | ||
|
||
class FeedController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth:sanctum'); | ||
} | ||
|
||
public function index(Feed $feedService) | ||
{ | ||
$challenges = $feedService->get(); | ||
|
||
return $this->ok( | ||
ChallengeResource::collection($challenges) | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\Challenge; | ||
use App\Models\User; | ||
|
||
class Feed | ||
{ | ||
public function __construct( | ||
private $user = null | ||
) { | ||
} | ||
|
||
public function get($perPage = 10) | ||
{ | ||
$user = $this->user ?: request()->user(); | ||
|
||
$followingIds = $user | ||
->following() | ||
->pluck('following_id'); | ||
|
||
return Challenge::query() | ||
->with(['user']) | ||
->withCount(['likes', 'comments']) | ||
->latest('continued_at') | ||
->whereIn('user_id', $followingIds) | ||
->paginate($perPage); | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Challenge; | ||
use App\Models\Country; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class FeedControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
private $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Country::create([ | ||
'code' => 'GB', | ||
'name' => 'United Kingdom', | ||
]); | ||
|
||
$this->user = $this->signIn(); | ||
} | ||
|
||
public function test_user_gets_following_challenges() | ||
{ | ||
$userToFollow = User::factory()->create(); | ||
|
||
$this->user | ||
->following() | ||
->attach($userToFollow->id); | ||
|
||
Challenge::factory()->count(3)->create(['user_id' => 2]); | ||
|
||
$this | ||
->getJson('api/feed') | ||
->assertOk() | ||
->assertJsonCount(3, 'data'); | ||
} | ||
} |