feat(api): Items API 멀티 item_type 지원 및 파라미터 일관성 개선

- ItemService: 콤마 구분 멀티 item_type 지원 (예: type=FG,PT)
  - parseItemTypes(): 콤마 구분 문자열을 배열로 파싱
  - validateItemTypesInSameGroup(): 같은 group_id 검증
  - index(), search() 메서드에 멀티 타입 로직 적용

- ItemsController: type/item_type 파라미터 일관성
  - show(), showByCode(), destroy() 메서드에서 type 파라미터 지원
  - 모든 엔드포인트에서 type 또는 item_type 둘 다 허용

- ItemBatchDeleteRequest: prepareForValidation()으로 type→item_type 매핑

- i18n: item_types_must_be_same_group 에러 메시지 추가 (ko/en)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-21 01:10:38 +09:00
parent 496aec04f5
commit 65c5de2db1
5 changed files with 104 additions and 15 deletions

View File

@@ -118,6 +118,59 @@ private function newQueryForTypes(array $itemTypes)
->whereIn('item_type', array_map('strtoupper', $itemTypes));
}
/**
* 콤마 구분 item_type 문자열을 배열로 파싱
*
* @param string|null $itemType 콤마 구분 item_type (예: "FG,PT")
* @return array 정규화된 item_type 배열
*/
private function parseItemTypes(?string $itemType): array
{
if (! $itemType) {
return [];
}
return array_filter(array_map('trim', explode(',', strtoupper($itemType))));
}
/**
* 여러 item_type이 같은 group_id에 속하는지 검증
*
* @param array $itemTypes item_type 배열
* @return int 공통 group_id
*
* @throws BadRequestHttpException 다른 group_id에 속하면 예외
*/
private function validateItemTypesInSameGroup(array $itemTypes): int
{
if (empty($itemTypes)) {
throw new BadRequestHttpException(__('error.item_type_required'));
}
// item_type 코드들의 parent_id (group) 조회
$groupCodes = \DB::table('common_codes as t')
->join('common_codes as g', 't.parent_id', '=', 'g.id')
->where('t.code_group', 'item_type')
->whereIn('t.code', $itemTypes)
->where('t.is_active', true)
->where('g.code_group', 'group')
->distinct()
->pluck('g.code')
->toArray();
// 존재하지 않는 item_type이 있는 경우
if (count($groupCodes) === 0) {
throw new BadRequestHttpException(__('error.invalid_item_type'));
}
// 여러 그룹에 속한 경우
if (count($groupCodes) > 1) {
throw new BadRequestHttpException(__('error.item_types_must_be_same_group'));
}
return (int) $groupCodes[0];
}
/**
* items 테이블의 고정 컬럼 목록 조회 (SystemFields + ItemField 기반)
*/
@@ -316,9 +369,19 @@ public function index(array $params): LengthAwarePaginator
$query = $this->newQueryForTypes($itemTypes)
->with(['category:id,name', 'details', 'files']);
} else {
// 단일 item_type 조회
$query = $this->newQuery($itemType)
->with(['category:id,name', 'details', 'files']);
// item_type 조회 (단일 또는 콤마 구분 멀티)
$itemTypes = $this->parseItemTypes($itemType);
if (count($itemTypes) === 1) {
// 단일 item_type
$query = $this->newQuery($itemTypes[0])
->with(['category:id,name', 'details', 'files']);
} else {
// 멀티 item_type - 같은 그룹인지 검증
$this->validateItemTypesInSameGroup($itemTypes);
$query = $this->newQueryForTypes($itemTypes)
->with(['category:id,name', 'details', 'files']);
}
}
// 검색어
@@ -651,7 +714,7 @@ public function destroy(int $id, string $itemType): void
/**
* 간편 검색 (동적 테이블 라우팅, 모달/드롭다운)
*
* @param array $params 검색 파라미터 (item_type 필수)
* @param array $params 검색 파라미터 (item_type 필수, 콤마 구분 멀티 지원)
*/
public function search(array $params)
{
@@ -664,8 +727,17 @@ public function search(array $params)
throw new BadRequestHttpException(__('error.item_type_required'));
}
// 동적 테이블 라우팅
$query = $this->newQuery($itemType);
// item_type 파싱 (단일 또는 콤마 구분 멀티)
$itemTypes = $this->parseItemTypes($itemType);
if (count($itemTypes) === 1) {
// 단일 item_type - 동적 테이블 라우팅
$query = $this->newQuery($itemTypes[0]);
} else {
// 멀티 item_type - 같은 그룹인지 검증
$this->validateItemTypesInSameGroup($itemTypes);
$query = $this->newQueryForTypes($itemTypes);
}
if ($q !== '') {
$query->where(function ($w) use ($q) {