Skip to content

feat: implement Application layer for favorite heritages list API / お気に入り一覧・IDs取得APIのApplication層実装 #556

Description

@zigzagdev

Parent Issue / 親Issue

#555

Motivation / 目的

Implement the Application layer use cases for listing favorite heritages and returning favorite ids, as part of #555.

お気に入り一覧・ID一覧取得API(#555)のApplication層として、UseCaseを実装します。

What to do / 実施内容

QueryUseCases

  • ListMyFavoritesUseCase
  • GetFavoriteHeritageIdsUseCase
    • FavoriteRepositoryInterface::findHeritageIdsByUserId() の結果をそのまま返す薄いUseCase

Pagination approach for ListMyFavoritesUseCase

Favorites are bounded by the total World Heritage catalog size (~1,200 sites) and are scoped per user, so the count involved is always small. Because of that, this fetches all favorite IDs and paginates in application code rather than adding a new DB-level paginate() join method to the repository:

$allIds   = $favoriteRepository->findHeritageIdsByUserId($userId); // all ids, ordered by favorited-at desc
$total    = count($allIds);
$pageIds  = array_slice($allIds, ($currentPage - 1) * $perPage, $perPage);
$heritages = $worldHeritageReadQueryService->findByIdsPreserveOrder($pageIds);

$lastPage = (int) ceil($total / $perPage);

Build the pagination metadata (current_page, per_page, total, last_page, from, to, page URLs) by hand, matching the same key structure as the existing PaginationDto::toArray() used by /v1/heritages (items + pagination), rather than wrapping an Eloquent LengthAwarePaginator.

Trade-off: this skips reusing Eloquent's paginator (so URL/meta fields are computed manually instead of generated automatically), but avoids adding a join-based pagination method to FavoriteRepositoryInterface. If favorite counts were ever expected to grow far beyond the heritage catalog size, this assumption would need to be revisited in favor of a DB-level join + paginate().

Tests / テスト

Mockeryを使ったユニットテスト

  • test_handle_returns_favorite_heritages_for_user
  • test_handle_returns_empty_list_when_no_favorites
  • test_handle_paginates_favorite_heritages_correctly
  • test_handle_returns_favorite_ids_for_user
  • test_handle_returns_empty_array_when_no_favorites

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions