Skip to content

Commit

Permalink
feed added
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Oct 15, 2023
1 parent 22a6194 commit 28f3857
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/Http/Controllers/FeedController.php
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)
);
}
}
30 changes: 30 additions & 0 deletions app/Services/Feed.php
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);
}
}
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Controllers\ContinueChallengeController;
use App\Http\Controllers\CountriesController;
use App\Http\Controllers\DeactivationRequestsController;
use App\Http\Controllers\FeedController;
use App\Http\Controllers\FollowSuggestionsController;
use App\Http\Controllers\NotificationsController;
use App\Http\Controllers\ProfileController;
Expand All @@ -32,6 +33,8 @@
Route::get('profile', [ProfileController::class, 'index']);
Route::patch('profile', [ProfileController::class, 'update']);

Route::get('feed', [FeedController::class, 'index']);

Route::apiResource('challenges', ChallengesController::class)->only(['index', 'store', 'destroy']);
Route::post('challenges/{challenge}/continue', ContinueChallengeController::class);

Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/FeedControllerTest.php
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');
}
}

0 comments on commit 28f3857

Please sign in to comment.