Skip to content

Commit a92eff3

Browse files
authored
Merge pull request #387 from zigzagdev/fix/api-detail-layout
Fix/api detail layout
2 parents 22f12dc + 41b8ab1 commit a92eff3

5 files changed

Lines changed: 10 additions & 148 deletions

File tree

src/app/Models/Country.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Models;
44

5-
use App\Models\WorldHeritage;
65
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
87

src/app/Models/Image.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Database\Eloquent\SoftDeletes;
76

87
class Image extends Model
98
{

src/app/Models/WorldHeritage.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class WorldHeritage extends Model
3535
'longitude',
3636
'short_description',
3737
'unesco_site_url',
38-
'thumbnail_image_id',
3938
];
4039

4140
protected $hidden = [
@@ -61,10 +60,6 @@ public function images(): HasMany
6160
->orderBy('sort_order', 'asc');
6261
}
6362

64-
public function thumbnail(): BelongsTo
65-
{
66-
return $this->belongsTo(Image::class, 'thumbnail_image_id');
67-
}
6863
protected function casts(): array
6964
{
7065
return [

src/app/Packages/Domains/WorldHeritageQueryService.php

Lines changed: 10 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@ public function __construct(
2525
private readonly WorldHeritageSearchPort $searchPort,
2626
) {}
2727

28-
/**
29-
* 一覧(最大30件): サムネのみ、state_party/state_party_code を要件通りに整形
30-
*/
3128
public function getAllHeritages(
3229
int $currentPage,
3330
int $perPage,
3431
string $order
35-
): PaginationDto
36-
{
32+
): PaginationDto {
3733
$items = WorldHeritage::query()
3834
->select([
3935
'world_heritage_sites.id',
@@ -50,7 +46,7 @@ public function getAllHeritages(
5046
'world_heritage_sites.latitude',
5147
'world_heritage_sites.longitude',
5248
'world_heritage_sites.short_description',
53-
'world_heritage_sites.image_url',
49+
'world_heritage_sites.unesco_site_url',
5450
])
5551
->with([
5652
'countries' => function ($q) {
@@ -60,7 +56,10 @@ public function getAllHeritages(
6056
'countries.name_jp',
6157
'countries.region',
6258
]);
63-
}
59+
},
60+
'images' => function ($imagesQuery) {
61+
$imagesQuery->where('is_primary', true)->limit(1);
62+
},
6463
])
6564
->orderBy('world_heritage_sites.id', $order)
6665
->paginate($perPage, page: $currentPage);
@@ -107,12 +106,12 @@ public function getHeritageById(int $id): WorldHeritageDto
107106
$imageCollection = new ImageDtoCollection();
108107
$images = ($heritage->images ?? collect())->values();
109108

110-
foreach ($images as $idx => $img) {
109+
foreach ($images as $img) {
111110
$imageCollection->add(new ImageDto(
112111
id: $img->id,
113112
url: $img->url,
114113
sortOrder: $img->sort_order,
115-
isPrimary: $idx === 0,
114+
isPrimary: (bool) $img->is_primary,
116115
));
117116
}
118117

@@ -189,121 +188,6 @@ public function getHeritageById(int $id): WorldHeritageDto
189188
]);
190189
}
191190

192-
public function getHeritagesByIds(array $ids, int $currentPage, int $perPage): PaginationDto
193-
{
194-
$paginator = $this->model
195-
->select([
196-
'id',
197-
'official_name',
198-
'name',
199-
'name_jp',
200-
'country',
201-
'region',
202-
'study_region',
203-
'category',
204-
'criteria',
205-
'year_inscribed',
206-
'area_hectares',
207-
'buffer_zone_hectares',
208-
'is_endangered',
209-
'latitude',
210-
'longitude',
211-
'short_description',
212-
'unesco_site_url',
213-
'thumbnail_image_id',
214-
])
215-
->with([
216-
'countries' => function ($countriesQuery) {
217-
$countriesQuery->withPivot(['is_primary'])->orderBy('countries.state_party_code', 'asc')->orderBy(
218-
'site_state_parties.inscription_year',
219-
'asc',
220-
);
221-
},
222-
'thumbnail' => function ($thumbnailQuery) {
223-
$thumbnailQuery->select([
224-
'images.id',
225-
'images.world_heritage_id',
226-
'path',
227-
'sort_order',
228-
]);
229-
},
230-
])
231-
->whereIn('id', $ids)
232-
->paginate($perPage, ['*'], 'current_page', $currentPage)
233-
->through(function ($heritage) {
234-
$countries = $heritage->countries ?? collect();
235-
236-
$codes = $countries
237-
->pluck('state_party_code')
238-
->filter()
239-
->map($this->statePartyCodeNormalize(...))
240-
->unique()
241-
->values();
242-
243-
$statePartyName = null;
244-
$statePartyCodes = null;
245-
$stateParties = [];
246-
247-
if ($codes->count() === 1) {
248-
$onlyCode = $codes->first();
249-
250-
$countryModel = $heritage->countries->first(
251-
fn($country) => $this->statePartyCodeNormalize($country->state_party_code) === $onlyCode,
252-
);
253-
254-
$statePartyName = $countryModel?->name;
255-
$statePartyCodes = null;
256-
} elseif ($codes->count() > 1) {
257-
$statePartyName = null;
258-
$statePartyCodes = $codes->all();
259-
}
260-
261-
$statePartiesMeta = [];
262-
263-
foreach ($countries as $country) {
264-
$code = strtoupper($country->state_party_code);
265-
if ($code === '' || $code === '0') {
266-
continue;
267-
}
268-
269-
$statePartiesMeta[$code] = [
270-
'is_primary' => (bool) data_get($country, 'pivot.is_primary', false),
271-
];
272-
}
273-
274-
return [
275-
'id' => $heritage->id,
276-
'official_name' => $heritage->official_name,
277-
'name' => $heritage->name,
278-
'name_jp' => $heritage->name_jp,
279-
'country' => $heritage->country,
280-
'region' => $heritage->study_region,
281-
'category' => $heritage->category,
282-
'criteria' => $heritage->criteria,
283-
'state_party' => $statePartyName,
284-
'state_party_code' => $statePartyCodes,
285-
'year_inscribed' => $heritage->year_inscribed,
286-
'area_hectares' => $heritage->area_hectares,
287-
'buffer_zone_hectares' => $heritage->buffer_zone_hectares,
288-
'is_endangered' => (bool) $heritage->is_endangered,
289-
'latitude' => $heritage->latitude,
290-
'longitude' => $heritage->longitude,
291-
'short_description' => $heritage->short_description,
292-
'unesco_site_url' => $heritage->unesco_site_url,
293-
'state_parties' => $stateParties,
294-
'state_parties_meta' => $statePartiesMeta,
295-
];
296-
});
297-
298-
$paginationArray = $paginator->toArray();
299-
$dtoCollection = $this->buildDtoFromCollection($paginationArray['data']);
300-
301-
return new PaginationDto(
302-
collection: $dtoCollection,
303-
pagination: collect($paginationArray)->except('data')->toArray(),
304-
);
305-
}
306-
307191
public function searchHeritages(AlgoliaSearchListQuery $query): PaginationDto
308192
{
309193
$result = $this->searchPort->search(
@@ -359,8 +243,6 @@ public function getEachRegionsHeritagesCount(): array
359243
$counts[$region->value] ??= 0;
360244
}
361245

362-
// id: 148 (Old City of Jerusalem) is stored as Unknown
363-
// but geographically belongs to Asia
364246
$counts[StudyRegion::ASIA->value] += 1;
365247

366248
return $counts;
@@ -377,6 +259,7 @@ private function buildWorldHeritagePayload($heritage): array
377259
->unique()
378260
->values();
379261

262+
$statePartyName = null;
380263
$statePartyCodeValue = null;
381264
$statePartyCodeList = [];
382265

@@ -385,16 +268,11 @@ private function buildWorldHeritagePayload($heritage): array
385268
$primaryCountry = $countryRelations->first(
386269
fn($country) => strtoupper($country->state_party_code) === $onlyStateParty,
387270
);
388-
389271
$statePartyName = $primaryCountry?->name_en ?? $heritage->country ?? null;
390272
} elseif ($statePartyCodeCollection->count() > 1) {
391273
$statePartyName = null;
392274
$statePartyCodeValue = $statePartyCodeCollection->all();
393275
$statePartyCodeList = $statePartyCodeCollection->all();
394-
} else {
395-
$statePartyName = null;
396-
$statePartyCodeValue = null;
397-
$statePartyCodeList = [];
398276
}
399277

400278
$statePartiesMeta = [];
@@ -409,8 +287,6 @@ private function buildWorldHeritagePayload($heritage): array
409287
];
410288
}
411289

412-
$thumbnailModel = $heritage->thumbnail;
413-
414290
return [
415291
'id' => $heritage->id,
416292
'official_name' => $heritage->official_name,
@@ -430,11 +306,10 @@ private function buildWorldHeritagePayload($heritage): array
430306
'latitude' => $heritage->latitude,
431307
'longitude' => $heritage->longitude,
432308
'short_description' => $heritage->short_description,
433-
'thumbnail_id' => $thumbnailModel?->id,
309+
'image_url' => $heritage->images->first()?->url,
434310
'unesco_site_url' => $heritage->unesco_site_url,
435311
'state_parties' => $statePartyCodeList,
436312
'state_parties_meta' => $statePartiesMeta,
437-
'image_url' => $heritage->image_url,
438313
];
439314
}
440315

src/app/Packages/Features/QueryUseCases/QueryServiceInterface/WorldHeritageQueryServiceInterface.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public function getHeritageById(
2121
int $id
2222
): WorldHeritageDto;
2323

24-
public function getHeritagesByIds(
25-
array $ids,
26-
int $currentPage,
27-
int $perPage
28-
): PaginationDto;
29-
3024
public function searchHeritages(
3125
AlgoliaSearchListQuery $query
3226
): PaginationDto;

0 commit comments

Comments
 (0)