diff --git a/src/app/Models/User.php b/src/app/Models/User.php index 62c8754..af253d1 100644 --- a/src/app/Models/User.php +++ b/src/app/Models/User.php @@ -42,6 +42,6 @@ public function favorites(): BelongsToMany 'user_favorite', 'user_id', 'world_heritage_site_id' - ); + )->withTimestamps(); } } \ No newline at end of file diff --git a/src/app/Packages/Domains/Favorite/FavoriteRepository.php b/src/app/Packages/Domains/Favorite/FavoriteRepository.php index 6fa2205..586538f 100644 --- a/src/app/Packages/Domains/Favorite/FavoriteRepository.php +++ b/src/app/Packages/Domains/Favorite/FavoriteRepository.php @@ -22,4 +22,18 @@ public function addFavorite(int $userId, int $worldHeritageSiteId): void $user->favorites()->attach($worldHeritageSiteId); } + + public function getFavoriteWorldHeritageIds(int $userId): array + { + $user = $this->userModel->find($userId); + + if ($user === null) { + throw new Exception('User not found.'); + } + + return $user->favorites() + ->orderBy('user_favorite.created_at', 'desc') + ->pluck('world_heritage_sites.id') + ->all(); + } } diff --git a/src/app/Packages/Domains/Favorite/Interface/FavoriteRepositoryInterface.php b/src/app/Packages/Domains/Favorite/Interface/FavoriteRepositoryInterface.php index 7b1291a..164187e 100644 --- a/src/app/Packages/Domains/Favorite/Interface/FavoriteRepositoryInterface.php +++ b/src/app/Packages/Domains/Favorite/Interface/FavoriteRepositoryInterface.php @@ -5,4 +5,6 @@ interface FavoriteRepositoryInterface { public function addFavorite(int $userId, int $worldHeritageSiteId): void; + + public function getFavoriteWorldHeritageIds(int $userId): array; } diff --git a/src/app/Packages/Domains/Favorite/Tests/FavoriteRepositoryTest.php b/src/app/Packages/Domains/Favorite/Tests/FavoriteRepositoryTest.php index 7b71a09..7264d60 100644 --- a/src/app/Packages/Domains/Favorite/Tests/FavoriteRepositoryTest.php +++ b/src/app/Packages/Domains/Favorite/Tests/FavoriteRepositoryTest.php @@ -90,4 +90,35 @@ public function test_addFavorite_throws_exception_when_user_not_found(): void $this->repository()->addFavorite(999999, $worldHeritage->id); } + + public function test_getFavoriteWorldHeritageIds_returns_ids_ordered_by_most_recently_favorited(): void + { + $user = $this->seedUser(); + $worldHeritage1 = $this->seedWorldHeritage(1); + $worldHeritage2 = $this->seedWorldHeritage(2); + + $user->favorites()->attach($worldHeritage1->id, ['created_at' => now()->subMinute()]); + $user->favorites()->attach($worldHeritage2->id, ['created_at' => now()]); + + $result = $this->repository()->getFavoriteWorldHeritageIds($user->id); + + $this->assertSame([$worldHeritage2->id, $worldHeritage1->id], $result); + } + + public function test_getFavoriteWorldHeritageIds_returns_empty_array_when_no_favorites(): void + { + $user = $this->seedUser(); + + $result = $this->repository()->getFavoriteWorldHeritageIds($user->id); + + $this->assertSame([], $result); + } + + public function test_getFavoriteWorldHeritageIds_throws_exception_when_user_not_found(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('User not found.'); + + $this->repository()->getFavoriteWorldHeritageIds(999999); + } }