-
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.
list following recommendation base on country and addiction type
- Loading branch information
1 parent
ed295dc
commit d0fef1f
Showing
6 changed files
with
124 additions
and
3 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
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,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class UserRecommendationResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'nick_name' => $this->nick_name, | ||
'avatar_url' => $this->avatar_url, | ||
'bio' => $this->bio, | ||
]; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class FollowingRecommendation | ||
{ | ||
public function __construct( | ||
private User $user, | ||
) { | ||
} | ||
|
||
public function recommend($count = 5) | ||
{ | ||
$followingIds = DB::table('followers') | ||
->select('following_id') | ||
->where('follower_id', $this->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) | ||
->inRandomOrder() | ||
->limit($count) | ||
->get(); | ||
} | ||
} |
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
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,48 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Country; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class UserFollowingRecommendationTest 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_gets_recommendation_list_contain_users_from_same_addiction_and_country() | ||
{ | ||
$followers = User::factory()->count(5)->create(); | ||
$this->user->following()->attach($followers->pluck('id')); | ||
User::factory()->create(); | ||
$recommendations = 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']), | ||
); | ||
} | ||
} |