refactor(item-master): 독립 엔티티 아키텍처 적용 및 Swagger 보완
- FK 컬럼 제거: item_sections.page_id, item_fields.section_id, item_bom_items.section_id - entity_relationships 테이블로 전환하여 독립 엔티티 구조 확립 - ItemMasterField 관련 파일 삭제 (Controller, Service, Model, Requests) - destroy 메서드 독립 엔티티 아키텍처 적용 (관계 링크만 삭제) - Swagger 스키마에서 FK 참조 제거 - FormRequest 및 Swagger에 group_id(계층번호) 필드 추가
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
class ItemSectionService extends Service
|
||||
{
|
||||
/**
|
||||
* 독립 섹션 목록 조회
|
||||
* 모든 섹션 목록 조회
|
||||
*
|
||||
* GET /api/v1/item-master/sections
|
||||
*/
|
||||
@@ -21,8 +21,7 @@ public function index(?bool $isTemplate = null): Collection
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
|
||||
$query = ItemSection::where('tenant_id', $tenantId)
|
||||
->with(['fields', 'bomItems']);
|
||||
$query = ItemSection::where('tenant_id', $tenantId);
|
||||
|
||||
if ($isTemplate === true) {
|
||||
$query->templates();
|
||||
@@ -46,7 +45,6 @@ public function storeIndependent(array $data): ItemSection
|
||||
$section = ItemSection::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'group_id' => $data['group_id'] ?? 1,
|
||||
'page_id' => null,
|
||||
'title' => $data['title'],
|
||||
'type' => $data['type'],
|
||||
'order_no' => 0,
|
||||
@@ -82,7 +80,6 @@ public function clone(int $id): ItemSection
|
||||
$cloned = ItemSection::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'group_id' => $original->group_id,
|
||||
'page_id' => null,
|
||||
'title' => $original->title.' (복사본)',
|
||||
'type' => $original->type,
|
||||
'order_no' => 0,
|
||||
@@ -150,46 +147,51 @@ public function getUsage(int $id): array
|
||||
throw new NotFoundHttpException(__('error.section_not_found'));
|
||||
}
|
||||
|
||||
// 1. 기존 FK 기반 연결 (page_id)
|
||||
$directPage = $section->page;
|
||||
|
||||
// 2. entity_relationships 기반 연결
|
||||
$linkedPageIds = EntityRelationship::where('child_type', EntityRelationship::TYPE_SECTION)
|
||||
->where('child_id', $id)
|
||||
->where('parent_type', EntityRelationship::TYPE_PAGE)
|
||||
->pluck('parent_id')
|
||||
->toArray();
|
||||
// entity_relationships 기반 연결
|
||||
$linkedPages = $section->linkedPages()->get();
|
||||
|
||||
return [
|
||||
'section_id' => $id,
|
||||
'direct_page' => $directPage,
|
||||
'linked_pages' => $section->linkedPages()->get(),
|
||||
'total_usage_count' => ($directPage ? 1 : 0) + count($linkedPageIds),
|
||||
'linked_pages' => $linkedPages,
|
||||
'total_usage_count' => $linkedPages->count(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 섹션 생성
|
||||
* 섹션 생성 및 페이지에 연결
|
||||
*/
|
||||
public function store(int $pageId, array $data): ItemSection
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
// order_no 자동 계산 (해당 페이지의 마지막 섹션 + 1)
|
||||
$maxOrder = ItemSection::where('tenant_id', $tenantId)
|
||||
->where('page_id', $pageId)
|
||||
// order_no 자동 계산 (해당 페이지에 연결된 마지막 섹션 + 1)
|
||||
$maxOrder = EntityRelationship::where('parent_type', EntityRelationship::TYPE_PAGE)
|
||||
->where('parent_id', $pageId)
|
||||
->where('child_type', EntityRelationship::TYPE_SECTION)
|
||||
->max('order_no');
|
||||
|
||||
// 섹션 생성
|
||||
$section = ItemSection::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'page_id' => $pageId,
|
||||
'group_id' => $data['group_id'] ?? 1,
|
||||
'title' => $data['title'],
|
||||
'type' => $data['type'],
|
||||
'order_no' => ($maxOrder ?? -1) + 1,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
// 페이지-섹션 관계 생성
|
||||
EntityRelationship::link(
|
||||
EntityRelationship::TYPE_PAGE,
|
||||
$pageId,
|
||||
EntityRelationship::TYPE_SECTION,
|
||||
$section->id,
|
||||
$tenantId,
|
||||
$data['group_id'] ?? 1,
|
||||
($maxOrder ?? -1) + 1
|
||||
);
|
||||
|
||||
$section->load(['fields', 'bomItems']);
|
||||
|
||||
return $section;
|
||||
@@ -223,6 +225,8 @@ public function update(int $id, array $data): ItemSection
|
||||
|
||||
/**
|
||||
* 섹션 삭제 (Soft Delete)
|
||||
*
|
||||
* 독립 엔티티 아키텍처: 섹션만 삭제하고 연결된 필드/BOM은 unlink만 수행
|
||||
*/
|
||||
public function destroy(int $id): void
|
||||
{
|
||||
@@ -237,39 +241,41 @@ public function destroy(int $id): void
|
||||
throw new NotFoundHttpException(__('error.not_found'));
|
||||
}
|
||||
|
||||
// 1. entity_relationships에서 이 섹션의 모든 부모 관계 해제
|
||||
// (page→section 관계에서 이 섹션 제거)
|
||||
EntityRelationship::where('child_type', EntityRelationship::TYPE_SECTION)
|
||||
->where('child_id', $id)
|
||||
->delete();
|
||||
|
||||
// 2. entity_relationships에서 이 섹션의 모든 자식 관계 해제
|
||||
// (section→field, section→bom 관계 삭제)
|
||||
EntityRelationship::where('parent_type', EntityRelationship::TYPE_SECTION)
|
||||
->where('parent_id', $id)
|
||||
->delete();
|
||||
|
||||
// 3. 섹션만 Soft Delete (필드/BOM은 독립 엔티티로 유지)
|
||||
$section->update(['deleted_by' => $userId]);
|
||||
$section->delete();
|
||||
|
||||
// Cascade: 하위 필드도 Soft Delete
|
||||
foreach ($section->fields as $field) {
|
||||
$field->update(['deleted_by' => $userId]);
|
||||
$field->delete();
|
||||
}
|
||||
|
||||
foreach ($section->bomItems as $bomItem) {
|
||||
$bomItem->update(['deleted_by' => $userId]);
|
||||
$bomItem->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 섹션 순서 변경
|
||||
* 섹션 순서 변경 (entity_relationships 기반)
|
||||
*
|
||||
* @param array $items [['id' => 1, 'order_no' => 0], ['id' => 2, 'order_no' => 1], ...]
|
||||
*/
|
||||
public function reorder(int $pageId, array $items): void
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
foreach ($items as $item) {
|
||||
ItemSection::where('tenant_id', $tenantId)
|
||||
->where('page_id', $pageId)
|
||||
->where('id', $item['id'])
|
||||
->update([
|
||||
'order_no' => $item['order_no'],
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
// entity_relationships에서 순서 업데이트
|
||||
EntityRelationship::where('parent_type', EntityRelationship::TYPE_PAGE)
|
||||
->where('parent_id', $pageId)
|
||||
->where('child_type', EntityRelationship::TYPE_SECTION)
|
||||
->where('child_id', $item['id'])
|
||||
->update(['order_no' => $item['order_no']]);
|
||||
|
||||
// 섹션 자체의 order_no도 동기화
|
||||
ItemSection::where('id', $item['id'])
|
||||
->update(['order_no' => $item['order_no']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user