관리자 상품목록을 한 페이지 여는 것만으로 그 페이지 모든 상품의 옵션이 응답에 실렸다. 같은 패턴을 저장소 전역에서 찾아 14개 목록 엔드포인트를 함께 정리했다. 근본 원인은 둘이다. Resource 가 whenLoaded 로 방어하는데 Repository 가 목록 쿼리에서 관계를 무조건 로드해 가드가 항상 참이 되는 가짜 가드, 그리고 toListArray 경량 표현을 정의해 두고도 컬렉션이 toArray 를 부르는 목록/상세 미분리다. 둘 다 응답만 보면 정상이라 오류도 경고도 없이 페이로드만 불어난다. 목록은 화면이 실제로 그리는 것만 싣는다. 개수·합계는 PHP 컬렉션 연산이 아니라 DB 집계로, 대표 1건이 필요한 곳은 관계 자체를 oldestOfMany 로 좁힌다. eager load 의 limit(1) 은 부모별이 아니라 배치 쿼리 전체에 걸려 첫 행만 값을 갖게 되므로 쓸 수 없다. 뺀 값에는 대체 경로를 먼저 만들었다. 상품 옵션은 행을 펼칠 때 배치로 불러오고(상품 수와 무관하게 쿼리 상수), 종전 동작이 필요한 호출자를 위해 ?with_options=1 등 opt-in 을 남겼다. 배송정책 국가설정과 리뷰 첨부 이미지는 소비처를 실측한 결과 화면이 실제로 그리고 있어 제거하지 않았다 — 그 소비 사실을 회귀 테스트로 고정했다. 재발 방지로 정적 검사 룰 4종과 규정 문서 항목을 함께 넣었다.
180 lines
6.9 KiB
PHP
180 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
class MenuResource extends BaseApiResource
|
|
{
|
|
/**
|
|
* 메뉴 리소스를 배열로 변환합니다.
|
|
*
|
|
* @param Request $request HTTP 요청 객체
|
|
* @return array<string, mixed> 변환된 메뉴 데이터 배열
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->getValue('id'),
|
|
'name' => $this->getValue('name'), // 다국어 객체 그대로 반환 (프론트엔드에서 $localized() 사용)
|
|
'slug' => $this->getValue('slug'),
|
|
'url' => $this->getValue('url'),
|
|
'icon' => $this->getValue('icon'),
|
|
'order' => $this->getValue('order'),
|
|
'is_active' => $this->getValue('is_active', true),
|
|
|
|
// 관계형 데이터
|
|
'parent_id' => $this->getValue('parent_id'), // parent_id 추가 (프론트엔드에서 필요)
|
|
'extension_type' => $this->getValue('extension_type'),
|
|
'extension_identifier' => $this->getValue('extension_identifier'),
|
|
'parent' => $this->whenLoaded('parent', function () {
|
|
return $this->parent ? [
|
|
'id' => $this->parent->id,
|
|
'name' => $this->parent->name, // 다국어 객체 그대로 반환
|
|
'url' => $this->parent->url,
|
|
'icon' => $this->parent->icon,
|
|
] : null;
|
|
}),
|
|
|
|
'children' => $this->whenLoaded('children', function () {
|
|
return $this->children->map(function ($child) {
|
|
$data = [
|
|
'id' => $child->id,
|
|
'name' => $child->name, // 다국어 객체 그대로 반환
|
|
'slug' => $child->slug,
|
|
'url' => $child->url,
|
|
'icon' => $child->icon,
|
|
'order' => $child->order,
|
|
'is_active' => $child->is_active,
|
|
'parent_id' => $child->parent_id,
|
|
'extension_type' => $child->extension_type?->value,
|
|
'extension_identifier' => $child->extension_identifier,
|
|
];
|
|
|
|
// 역할은 로드된 경우에만 싣는다. 미로드 시 `[]` 를 돌려주면 "역할 제한이
|
|
// 없다" 는 사실이 아닌 단언이 되어, 화면이 그 값을 그대로 저장에 실어
|
|
// 보내면 해당 메뉴의 역할 제한이 통째로 지워진다. 키 자체를 생략해
|
|
// 소비 측이 "모른다" 를 구분할 수 있게 한다 (단건 조회로 보강).
|
|
if ($child->relationLoaded('roles')) {
|
|
$data['roles'] = $child->roles->map(function ($role) {
|
|
return [
|
|
'id' => $role->id,
|
|
'name' => $role->name,
|
|
'permission_type' => $role->pivot->permission_type ?? null,
|
|
];
|
|
})->values();
|
|
}
|
|
|
|
return $data;
|
|
})->sortBy('order')->values();
|
|
}),
|
|
|
|
'creator' => $this->whenLoaded('creator', function () {
|
|
return $this->creator ? [
|
|
'uuid' => $this->creator->uuid,
|
|
'name' => $this->creator->name,
|
|
'email' => $this->creator->email,
|
|
] : null;
|
|
}),
|
|
|
|
'roles' => $this->whenLoaded('roles', function () {
|
|
return $this->roles->map(function ($role) {
|
|
return [
|
|
'id' => $role->id,
|
|
'name' => $role->name,
|
|
'permission_type' => $role->pivot->permission_type ?? null,
|
|
];
|
|
});
|
|
}),
|
|
|
|
// 통계 정보
|
|
'children_count' => $this->when(
|
|
isset($this->children_count),
|
|
$this->getValue('children_count')
|
|
),
|
|
'depth_level' => $this->when(
|
|
isset($this->depth_level),
|
|
$this->getValue('depth_level')
|
|
),
|
|
|
|
...$this->formatTimestamps(),
|
|
...$this->resourceMeta($request),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 소유자 필드명을 반환합니다.
|
|
*
|
|
* @return string|null 소유자 필드명
|
|
*/
|
|
protected function ownerField(): ?string
|
|
{
|
|
return 'created_by';
|
|
}
|
|
|
|
/**
|
|
* 리소스별 권한 매핑을 반환합니다.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function abilityMap(): array
|
|
{
|
|
return [
|
|
'can_create' => 'core.menus.create',
|
|
'can_update' => 'core.menus.update',
|
|
'can_delete' => 'core.menus.delete',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 네비게이션용 단순한 형태의 배열을 반환합니다 (계층 구조 포함).
|
|
*
|
|
* @return array<string, mixed> 네비게이션용 메뉴 정보
|
|
*/
|
|
public function toNavigationArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name, // 다국어 객체 그대로 반환
|
|
'slug' => $this->slug,
|
|
'url' => $this->url,
|
|
'icon' => $this->icon,
|
|
'order' => $this->order,
|
|
'is_active' => $this->is_active,
|
|
'children' => $this->whenLoaded('activeChildren', function () {
|
|
return $this->activeChildren
|
|
->sortBy('order')
|
|
->map(function ($child) {
|
|
return [
|
|
'id' => $child->id,
|
|
'name' => $child->name, // 다국어 객체 그대로 반환
|
|
'slug' => $child->slug,
|
|
'url' => $child->url,
|
|
'icon' => $child->icon,
|
|
'order' => $child->order,
|
|
'is_active' => $child->is_active,
|
|
'parent_id' => $child->parent_id,
|
|
'extension_type' => $child->extension_type?->value,
|
|
'extension_identifier' => $child->extension_identifier,
|
|
];
|
|
})->values();
|
|
}),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 관리자용 상세 정보를 포함한 배열을 반환합니다.
|
|
*
|
|
* @return array<string, mixed> 관리자용 상세 메뉴 정보
|
|
*/
|
|
public function withAdminInfo(): array
|
|
{
|
|
return array_merge($this->toArray(request()), [
|
|
'admin_notes' => $this->admin_notes,
|
|
'created_by' => $this->creator?->uuid,
|
|
'last_accessed_at' => $this->when($this->last_accessed_at, fn () => $this->formatDateTimeForUser($this->last_accessed_at)),
|
|
'access_count' => $this->access_count ?? 0,
|
|
]);
|
|
}
|
|
}
|