feat: ItemMaster Phase 1 API 구현 (핵심 기능 13개 엔드포인트)
- Controller 4개 생성 (ItemMaster, ItemPage, ItemSection, ItemField)
- Service 4개 생성 (비즈니스 로직 및 Multi-tenant 지원)
- FormRequest 7개 생성 (Validation)
- Routes 등록 (/v1/item-master/*)
주요 API:
- GET /init - 전체 초기 데이터 로드
- 페이지 관리 (GET/POST/PUT/DELETE)
- 섹션 관리 (POST/PUT/DELETE/reorder)
- 필드 관리 (POST/PUT/DELETE/reorder)
기술적 특징:
- Service-First 패턴 (Controller는 DI + ApiResponse만)
- Multi-tenant 지원 (tenantId() 검증 + BelongsToTenant)
- Cascade Soft Delete (하위 엔티티 자동 처리)
- i18n 메시지 키 사용 (__('message.xxx'))
- order_no 자동 계산 및 reorder 지원
검증:
- 라우트 테스트: 13개 엔드포인트 정상 등록
- Pint 검사: 15 files PASS
SAM API Development Rules 준수
This commit is contained in:
115
app/Services/ItemMaster/ItemFieldService.php
Normal file
115
app/Services/ItemMaster/ItemFieldService.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ItemMaster;
|
||||
|
||||
use App\Models\ItemMaster\ItemField;
|
||||
use App\Services\Service;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class ItemFieldService extends Service
|
||||
{
|
||||
/**
|
||||
* 필드 생성
|
||||
*/
|
||||
public function store(int $sectionId, array $data): ItemField
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
// order_no 자동 계산 (해당 섹션의 마지막 필드 + 1)
|
||||
$maxOrder = ItemField::where('tenant_id', $tenantId)
|
||||
->where('section_id', $sectionId)
|
||||
->max('order_no');
|
||||
|
||||
$field = ItemField::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'section_id' => $sectionId,
|
||||
'field_name' => $data['field_name'],
|
||||
'field_type' => $data['field_type'],
|
||||
'order_no' => ($maxOrder ?? -1) + 1,
|
||||
'is_required' => $data['is_required'] ?? false,
|
||||
'default_value' => $data['default_value'] ?? null,
|
||||
'placeholder' => $data['placeholder'] ?? null,
|
||||
'display_condition' => $data['display_condition'] ?? null,
|
||||
'validation_rules' => $data['validation_rules'] ?? null,
|
||||
'options' => $data['options'] ?? null,
|
||||
'properties' => $data['properties'] ?? null,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* 필드 수정
|
||||
*/
|
||||
public function update(int $id, array $data): ItemField
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$field = ItemField::where('tenant_id', $tenantId)
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
if (! $field) {
|
||||
throw new NotFoundHttpException(__('error.not_found'));
|
||||
}
|
||||
|
||||
$field->update([
|
||||
'field_name' => $data['field_name'] ?? $field->field_name,
|
||||
'field_type' => $data['field_type'] ?? $field->field_type,
|
||||
'is_required' => $data['is_required'] ?? $field->is_required,
|
||||
'default_value' => $data['default_value'] ?? $field->default_value,
|
||||
'placeholder' => $data['placeholder'] ?? $field->placeholder,
|
||||
'display_condition' => $data['display_condition'] ?? $field->display_condition,
|
||||
'validation_rules' => $data['validation_rules'] ?? $field->validation_rules,
|
||||
'options' => $data['options'] ?? $field->options,
|
||||
'properties' => $data['properties'] ?? $field->properties,
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* 필드 삭제 (Soft Delete)
|
||||
*/
|
||||
public function destroy(int $id): void
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$field = ItemField::where('tenant_id', $tenantId)
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
if (! $field) {
|
||||
throw new NotFoundHttpException(__('error.not_found'));
|
||||
}
|
||||
|
||||
$field->update(['deleted_by' => $userId]);
|
||||
$field->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 필드 순서 변경
|
||||
*
|
||||
* @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...]
|
||||
*/
|
||||
public function reorder(int $sectionId, array $items): void
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
foreach ($items as $item) {
|
||||
ItemField::where('tenant_id', $tenantId)
|
||||
->where('section_id', $sectionId)
|
||||
->where('id', $item['id'])
|
||||
->update([
|
||||
'order_no' => $item['order_no'],
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user