Skip to content

Commit

Permalink
refactor following-recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Sep 23, 2023
1 parent d0fef1f commit fcd723c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 56 deletions.
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)
);
}
}
12 changes: 0 additions & 12 deletions app/Http/Controllers/UserFollowingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace App\Http\Controllers;

use App\Http\Resources\TinyUserResource;
use App\Http\Resources\UserRecommendationResource;
use App\Models\User;
use App\Services\FollowingRecommendation;

class UserFollowingController extends Controller
{
Expand All @@ -25,14 +23,4 @@ public function index(User $user)
TinyUserResource::collection($following)
);
}

public function recommendations(User $user)
{
$recommendations = (new FollowingRecommendation($user))
->recommend();

return $this->ok(
UserRecommendationResource::collection($recommendations)
);
}
}
24 changes: 0 additions & 24 deletions app/Http/Resources/UserRecommendationResource.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
use App\Models\User;
use Illuminate\Support\Facades\DB;

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

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

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

return User::query()
->select('id', 'nick_name', 'avatar_url', 'bio')
->whereNotIn('id', $followingIds)
->whereNot('id', $this->user->id)
->where('country_id', '=', $this->user->country_id)
->where('addiction_type', '=', $this->user->addiction_type)
->whereNot('id', $user->id)
->where('country_id', '=', $user->country_id)
->where('addiction_type', '=', $user->addiction_type)
->inRandomOrder()
->limit($count)
->get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up(): void
$table->string('nick_name');
$table->string('bio')->nullable();
$table->string('avatar_url')->nullable();
$table->foreignId('country_id')->index('country_id_index')->constrained();
$table->foreignId('country_id')->constrained();
$table->unsignedTinyInteger('addiction_type')->index('addiction_type_index');
$table->boolean('is_recovered')->default(false);
$table->unsignedInteger('score')->default(0);
Expand Down
4 changes: 4 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,11 +52,14 @@
});

Route::apiResource('users', UsersController::class)->only('index', 'show');
Route::get('users/{user}/following', [UserFollowingController::class, 'index']);
Route::get('users/{user}/followers', [UserFollowersController::class, 'index']);
Route::post('users/{user}/followers', [UserFollowersController::class, 'store']);
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']);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserFollowingRecommendationTest extends TestCase
class FollowSuggestionsControllerTest extends TestCase
{
use RefreshDatabase;

Expand All @@ -25,24 +25,22 @@ public function setUp(): void
$this->user = $this->signIn();
}

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

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

User::factory()->create();
$recommendations = User::factory()->count(5)->create([

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

$response = $this
->getJson("api/users/{$this->user->id}/following/recommendations");

$response->assertOk();
$this->assertCount(5, $response['data']);
$this->assertEquals(
$recommendations->pluck(['id', 'nike_name', 'avatar_url', 'bio']),
collect($response['data'])->pluck(['id', 'nike_name', 'avatar_url', 'bio']),
);
$this
->getJson('api/follow-suggestions')
->assertOk()
->assertJsonCount(5, 'data');
}
}

0 comments on commit fcd723c

Please sign in to comment.