Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: following recommendation #8

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/Http/Controllers/FollowSuggestionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Http\Resources\TinyUserResource;
use App\Services\FollowSuggestions;

class FollowSuggestionsController extends Controller
{
public function __construct()
{
$this->middleware('auth:sanctum');
}

public function index(FollowSuggestions $followSuggestionsService)
{
$suggestions = $followSuggestionsService->suggest();

return $this->ok(
TinyUserResource::collection($suggestions)
);
}
}
34 changes: 34 additions & 0 deletions app/Services/FollowSuggestions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class FollowSuggestions
{
public function __construct(
private $user = null
) {
}

public function suggest($count = 5)
{
$user = $this->user ?: request()->user();

$followingIds = DB::table('followers')
->select('following_id')
->where('follower_id', $user->id)
->pluck('following_id');

return User::query()
->select('id', 'nick_name', 'avatar_url', 'bio')
->whereNotIn('id', $followingIds)
->whereNot('id', $user->id)
->where('country_id', '=', $user->country_id)
->where('addiction_type', '=', $user->addiction_type)
->inRandomOrder()
->limit($count)
->get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up(): void
$table->string('bio')->nullable();
$table->string('avatar_url')->nullable();
$table->foreignId('country_id')->constrained();
$table->unsignedTinyInteger('addiction_type');
$table->unsignedTinyInteger('addiction_type')->index('addiction_type_index');
$table->boolean('is_recovered')->default(false);
$table->unsignedInteger('score')->default(0);
$table->date('birth_date')->nullable();
Expand Down
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\FollowSuggestionsController;
use App\Http\Controllers\NotificationsController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReportsController;
Expand Down Expand Up @@ -51,6 +52,8 @@
Route::delete('users/{user}/followers', [UserFollowersController::class, 'destroy']);
Route::get('users/{user}/achievements', [UserAchievementsController::class, 'index']);

Route::get('follow-suggestions', [FollowSuggestionsController::class, 'index']);

Route::post('reports', [ReportsController::class, 'store']);

Route::get('countries', [CountriesController::class, 'index']);
66 changes: 66 additions & 0 deletions tests/Feature/FollowSuggestionsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Feature;

use App\Models\Country;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FollowSuggestionsControllerTest 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_can_get_suggestion_list()
{
$followers = User::factory()->count(5)->create();

$this->user->following()->attach($followers->pluck('id'));

User::factory()->create();

User::factory()->count(5)->create([
'country_id' => $this->user->country_id,
'addiction_type' => $this->user->addiction_type,
]);

$this
->getJson('api/follow-suggestions')
->assertOk()
->assertJsonCount(5, 'data');
}

public function test_user_can_not_get_suggestion_for_him_self()
{
$this
->getJson('api/follow-suggestions')
->assertOk()
->assertJsonCount(0, 'data');
}

public function test_user_can_not_get_suggestion_for_his_followings()
{
$followers = User::factory()->count(5)->create();

$this->user->following()->attach($followers->pluck('id'));

$this
->getJson('api/follow-suggestions')
->assertOk()
->assertJsonCount(0, 'data');
}
}
Loading