-
Notifications
You must be signed in to change notification settings - Fork 0
Fix/api detail layout #387
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
base: chore/integrate-image_url-into-image-table
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,15 +25,11 @@ public function __construct( | |
| private readonly WorldHeritageSearchPort $searchPort, | ||
| ) {} | ||
|
|
||
| /** | ||
| * 一覧(最大30件): サムネのみ、state_party/state_party_code を要件通りに整形 | ||
| */ | ||
| public function getAllHeritages( | ||
| int $currentPage, | ||
| int $perPage, | ||
| string $order | ||
| ): PaginationDto | ||
| { | ||
| ): PaginationDto { | ||
| $items = WorldHeritage::query() | ||
| ->select([ | ||
| 'world_heritage_sites.id', | ||
|
|
@@ -50,7 +46,7 @@ public function getAllHeritages( | |
| 'world_heritage_sites.latitude', | ||
| 'world_heritage_sites.longitude', | ||
| 'world_heritage_sites.short_description', | ||
| 'world_heritage_sites.image_url', | ||
| 'world_heritage_sites.unesco_site_url', | ||
| ]) | ||
| ->with([ | ||
| 'countries' => function ($q) { | ||
|
|
@@ -60,7 +56,10 @@ public function getAllHeritages( | |
| 'countries.name_jp', | ||
| 'countries.region', | ||
| ]); | ||
| } | ||
| }, | ||
| 'images' => function ($imagesQuery) { | ||
| $imagesQuery->where('is_primary', true)->limit(1); | ||
| }, | ||
|
Comment on lines
+60
to
+62
|
||
| ]) | ||
| ->orderBy('world_heritage_sites.id', $order) | ||
| ->paginate($perPage, page: $currentPage); | ||
|
|
@@ -107,12 +106,12 @@ public function getHeritageById(int $id): WorldHeritageDto | |
| $imageCollection = new ImageDtoCollection(); | ||
| $images = ($heritage->images ?? collect())->values(); | ||
|
|
||
| foreach ($images as $idx => $img) { | ||
| foreach ($images as $img) { | ||
| $imageCollection->add(new ImageDto( | ||
| id: $img->id, | ||
| url: $img->url, | ||
| sortOrder: $img->sort_order, | ||
| isPrimary: $idx === 0, | ||
| isPrimary: (bool) $img->is_primary, | ||
| )); | ||
| } | ||
|
|
||
|
|
@@ -189,121 +188,6 @@ public function getHeritageById(int $id): WorldHeritageDto | |
| ]); | ||
| } | ||
|
|
||
| public function getHeritagesByIds(array $ids, int $currentPage, int $perPage): PaginationDto | ||
| { | ||
| $paginator = $this->model | ||
| ->select([ | ||
| 'id', | ||
| 'official_name', | ||
| 'name', | ||
| 'name_jp', | ||
| 'country', | ||
| 'region', | ||
| 'study_region', | ||
| 'category', | ||
| 'criteria', | ||
| 'year_inscribed', | ||
| 'area_hectares', | ||
| 'buffer_zone_hectares', | ||
| 'is_endangered', | ||
| 'latitude', | ||
| 'longitude', | ||
| 'short_description', | ||
| 'unesco_site_url', | ||
| 'thumbnail_image_id', | ||
| ]) | ||
| ->with([ | ||
| 'countries' => function ($countriesQuery) { | ||
| $countriesQuery->withPivot(['is_primary'])->orderBy('countries.state_party_code', 'asc')->orderBy( | ||
| 'site_state_parties.inscription_year', | ||
| 'asc', | ||
| ); | ||
| }, | ||
| 'thumbnail' => function ($thumbnailQuery) { | ||
| $thumbnailQuery->select([ | ||
| 'images.id', | ||
| 'images.world_heritage_id', | ||
| 'path', | ||
| 'sort_order', | ||
| ]); | ||
| }, | ||
| ]) | ||
| ->whereIn('id', $ids) | ||
| ->paginate($perPage, ['*'], 'current_page', $currentPage) | ||
| ->through(function ($heritage) { | ||
| $countries = $heritage->countries ?? collect(); | ||
|
|
||
| $codes = $countries | ||
| ->pluck('state_party_code') | ||
| ->filter() | ||
| ->map($this->statePartyCodeNormalize(...)) | ||
| ->unique() | ||
| ->values(); | ||
|
|
||
| $statePartyName = null; | ||
| $statePartyCodes = null; | ||
| $stateParties = []; | ||
|
|
||
| if ($codes->count() === 1) { | ||
| $onlyCode = $codes->first(); | ||
|
|
||
| $countryModel = $heritage->countries->first( | ||
| fn($country) => $this->statePartyCodeNormalize($country->state_party_code) === $onlyCode, | ||
| ); | ||
|
|
||
| $statePartyName = $countryModel?->name; | ||
| $statePartyCodes = null; | ||
| } elseif ($codes->count() > 1) { | ||
| $statePartyName = null; | ||
| $statePartyCodes = $codes->all(); | ||
| } | ||
|
|
||
| $statePartiesMeta = []; | ||
|
|
||
| foreach ($countries as $country) { | ||
| $code = strtoupper($country->state_party_code); | ||
| if ($code === '' || $code === '0') { | ||
| continue; | ||
| } | ||
|
|
||
| $statePartiesMeta[$code] = [ | ||
| 'is_primary' => (bool) data_get($country, 'pivot.is_primary', false), | ||
| ]; | ||
| } | ||
|
|
||
| return [ | ||
| 'id' => $heritage->id, | ||
| 'official_name' => $heritage->official_name, | ||
| 'name' => $heritage->name, | ||
| 'name_jp' => $heritage->name_jp, | ||
| 'country' => $heritage->country, | ||
| 'region' => $heritage->study_region, | ||
| 'category' => $heritage->category, | ||
| 'criteria' => $heritage->criteria, | ||
| 'state_party' => $statePartyName, | ||
| 'state_party_code' => $statePartyCodes, | ||
| 'year_inscribed' => $heritage->year_inscribed, | ||
| 'area_hectares' => $heritage->area_hectares, | ||
| 'buffer_zone_hectares' => $heritage->buffer_zone_hectares, | ||
| 'is_endangered' => (bool) $heritage->is_endangered, | ||
| 'latitude' => $heritage->latitude, | ||
| 'longitude' => $heritage->longitude, | ||
| 'short_description' => $heritage->short_description, | ||
| 'unesco_site_url' => $heritage->unesco_site_url, | ||
| 'state_parties' => $stateParties, | ||
| 'state_parties_meta' => $statePartiesMeta, | ||
| ]; | ||
| }); | ||
|
|
||
| $paginationArray = $paginator->toArray(); | ||
| $dtoCollection = $this->buildDtoFromCollection($paginationArray['data']); | ||
|
|
||
| return new PaginationDto( | ||
| collection: $dtoCollection, | ||
| pagination: collect($paginationArray)->except('data')->toArray(), | ||
| ); | ||
| } | ||
|
|
||
| public function searchHeritages(AlgoliaSearchListQuery $query): PaginationDto | ||
| { | ||
| $result = $this->searchPort->search( | ||
|
|
@@ -359,8 +243,6 @@ public function getEachRegionsHeritagesCount(): array | |
| $counts[$region->value] ??= 0; | ||
| } | ||
|
|
||
| // id: 148 (Old City of Jerusalem) is stored as Unknown | ||
| // but geographically belongs to Asia | ||
| $counts[StudyRegion::ASIA->value] += 1; | ||
|
Comment on lines
244
to
246
|
||
|
|
||
| return $counts; | ||
|
|
@@ -377,6 +259,7 @@ private function buildWorldHeritagePayload($heritage): array | |
| ->unique() | ||
| ->values(); | ||
|
|
||
| $statePartyName = null; | ||
| $statePartyCodeValue = null; | ||
| $statePartyCodeList = []; | ||
|
|
||
|
|
@@ -385,16 +268,11 @@ private function buildWorldHeritagePayload($heritage): array | |
| $primaryCountry = $countryRelations->first( | ||
| fn($country) => strtoupper($country->state_party_code) === $onlyStateParty, | ||
| ); | ||
|
|
||
| $statePartyName = $primaryCountry?->name_en ?? $heritage->country ?? null; | ||
| } elseif ($statePartyCodeCollection->count() > 1) { | ||
| $statePartyName = null; | ||
| $statePartyCodeValue = $statePartyCodeCollection->all(); | ||
| $statePartyCodeList = $statePartyCodeCollection->all(); | ||
| } else { | ||
| $statePartyName = null; | ||
| $statePartyCodeValue = null; | ||
| $statePartyCodeList = []; | ||
| } | ||
|
|
||
| $statePartiesMeta = []; | ||
|
|
@@ -409,8 +287,6 @@ private function buildWorldHeritagePayload($heritage): array | |
| ]; | ||
| } | ||
|
|
||
| $thumbnailModel = $heritage->thumbnail; | ||
|
|
||
| return [ | ||
| 'id' => $heritage->id, | ||
| 'official_name' => $heritage->official_name, | ||
|
|
@@ -430,11 +306,10 @@ private function buildWorldHeritagePayload($heritage): array | |
| 'latitude' => $heritage->latitude, | ||
| 'longitude' => $heritage->longitude, | ||
| 'short_description' => $heritage->short_description, | ||
| 'thumbnail_id' => $thumbnailModel?->id, | ||
| 'image_url' => $heritage->images->first()?->url, | ||
| 'unesco_site_url' => $heritage->unesco_site_url, | ||
|
Comment on lines
307
to
310
|
||
| 'state_parties' => $statePartyCodeList, | ||
| 'state_parties_meta' => $statePartiesMeta, | ||
| 'image_url' => $heritage->image_url, | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The eager-load constraint
->limit(1)insidewith(['images' => ...])will limit the total number of loaded images across all parent heritages, not 1 per heritage, so most items will end up with no image. Prefer modeling a dedicatedprimaryImage/thumbnailrelationship (e.g.,hasOne/ofMany) and eager load that, or drop thelimit(1)and select the primary in PHP.