From 9cd28c8292c62185fa275d8204473bcb054ea416 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sat, 28 Mar 2026 23:50:06 +0900 Subject: [PATCH 1/3] refactor: remove thumbnail relation and image_url from WorldHeritage model --- src/app/Models/Country.php | 1 - src/app/Models/Image.php | 1 - src/app/Models/WorldHeritage.php | 5 ----- 3 files changed, 7 deletions(-) diff --git a/src/app/Models/Country.php b/src/app/Models/Country.php index 1a13703..07b0b5a 100644 --- a/src/app/Models/Country.php +++ b/src/app/Models/Country.php @@ -2,7 +2,6 @@ namespace App\Models; -use App\Models\WorldHeritage; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; diff --git a/src/app/Models/Image.php b/src/app/Models/Image.php index 33dee13..a6bd4c7 100644 --- a/src/app/Models/Image.php +++ b/src/app/Models/Image.php @@ -3,7 +3,6 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\SoftDeletes; class Image extends Model { diff --git a/src/app/Models/WorldHeritage.php b/src/app/Models/WorldHeritage.php index bacb481..a454a4f 100644 --- a/src/app/Models/WorldHeritage.php +++ b/src/app/Models/WorldHeritage.php @@ -35,7 +35,6 @@ class WorldHeritage extends Model 'longitude', 'short_description', 'unesco_site_url', - 'thumbnail_image_id', ]; protected $hidden = [ @@ -61,10 +60,6 @@ public function images(): HasMany ->orderBy('sort_order', 'asc'); } - public function thumbnail(): BelongsTo - { - return $this->belongsTo(Image::class, 'thumbnail_image_id'); - } protected function casts(): array { return [ From d152539df0113b9d80d7e582053a28c275b35e3a Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sat, 28 Mar 2026 23:50:57 +0900 Subject: [PATCH 2/3] fix: replace image_url/thumbnail with images relation in WorldHeritageQueryService --- .../Domains/WorldHeritageQueryService.php | 145 ++---------------- 1 file changed, 10 insertions(+), 135 deletions(-) diff --git a/src/app/Packages/Domains/WorldHeritageQueryService.php b/src/app/Packages/Domains/WorldHeritageQueryService.php index b2cbce9..4346454 100644 --- a/src/app/Packages/Domains/WorldHeritageQueryService.php +++ b/src/app/Packages/Domains/WorldHeritageQueryService.php @@ -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); + }, ]) ->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; 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, 'state_parties' => $statePartyCodeList, 'state_parties_meta' => $statePartiesMeta, - 'image_url' => $heritage->image_url, ]; } From 41b8ab1016c866bd4e062b83354226bd40aa1db1 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sat, 28 Mar 2026 23:51:12 +0900 Subject: [PATCH 3/3] refactor: update WorldHeritageQueryServiceInterface --- .../WorldHeritageQueryServiceInterface.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/app/Packages/Features/QueryUseCases/QueryServiceInterface/WorldHeritageQueryServiceInterface.php b/src/app/Packages/Features/QueryUseCases/QueryServiceInterface/WorldHeritageQueryServiceInterface.php index 48f6e1b..083e2fe 100644 --- a/src/app/Packages/Features/QueryUseCases/QueryServiceInterface/WorldHeritageQueryServiceInterface.php +++ b/src/app/Packages/Features/QueryUseCases/QueryServiceInterface/WorldHeritageQueryServiceInterface.php @@ -21,12 +21,6 @@ public function getHeritageById( int $id ): WorldHeritageDto; - public function getHeritagesByIds( - array $ids, - int $currentPage, - int $perPage - ): PaginationDto; - public function searchHeritages( AlgoliaSearchListQuery $query ): PaginationDto;