Files
sam-manage/app/Http/Controllers/Api/Admin/Quote/QuoteFormulaMappingController.php
hskwon 5742f9a3e4 feat(quote-formula): 매핑/품목 관리 UI 구현 (Phase 2, 3)
Phase 2 - 매핑(Mapping) 관리:
- QuoteFormulaMappingController, QuoteFormulaMappingService 추가
- mappings-tab.blade.php 뷰 생성
- 매핑 CRUD 및 순서 변경 API

Phase 3 - 품목(Item) 관리:
- QuoteFormulaItemController, QuoteFormulaItemService 추가
- items-tab.blade.php 뷰 생성
- 품목 CRUD 및 순서 변경 API
- 수량식/단가식 입력 지원

공통:
- edit.blade.php에 매핑/품목 탭 연동
- routes/api.php에 API 엔드포인트 추가
2025-12-22 19:07:50 +09:00

150 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Quote;
use App\Http\Controllers\Controller;
use App\Services\Quote\QuoteFormulaMappingService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class QuoteFormulaMappingController extends Controller
{
public function __construct(
private readonly QuoteFormulaMappingService $mappingService
) {}
/**
* 매핑 목록 조회
*/
public function index(int $formulaId): JsonResponse
{
$mappings = $this->mappingService->getMappingsByFormula($formulaId);
return response()->json([
'success' => true,
'data' => $mappings,
]);
}
/**
* 매핑 상세 조회
*/
public function show(int $formulaId, int $mappingId): JsonResponse
{
// 수식 소속 확인
if (! $this->mappingService->belongsToFormula($mappingId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 매핑입니다.',
], 404);
}
$mapping = $this->mappingService->getMappingById($mappingId);
return response()->json([
'success' => true,
'data' => $mapping,
]);
}
/**
* 매핑 생성
*/
public function store(Request $request, int $formulaId): JsonResponse
{
$validated = $request->validate([
'source_variable' => 'required|string|max:50',
'source_value' => 'required|string|max:100',
'result_value' => 'required|string',
'result_type' => 'nullable|in:fixed,formula',
'sort_order' => 'nullable|integer|min:0',
]);
$mapping = $this->mappingService->createMapping($formulaId, $validated);
return response()->json([
'success' => true,
'message' => '매핑이 추가되었습니다.',
'data' => $mapping,
]);
}
/**
* 매핑 수정
*/
public function update(Request $request, int $formulaId, int $mappingId): JsonResponse
{
// 수식 소속 확인
if (! $this->mappingService->belongsToFormula($mappingId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 매핑입니다.',
], 404);
}
$validated = $request->validate([
'source_variable' => 'nullable|string|max:50',
'source_value' => 'nullable|string|max:100',
'result_value' => 'nullable|string',
'result_type' => 'nullable|in:fixed,formula',
]);
$mapping = $this->mappingService->updateMapping($mappingId, $validated);
return response()->json([
'success' => true,
'message' => '매핑이 수정되었습니다.',
'data' => $mapping,
]);
}
/**
* 매핑 삭제
*/
public function destroy(int $formulaId, int $mappingId): JsonResponse
{
// 수식 소속 확인
if (! $this->mappingService->belongsToFormula($mappingId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 매핑입니다.',
], 404);
}
$this->mappingService->deleteMapping($mappingId);
return response()->json([
'success' => true,
'message' => '매핑이 삭제되었습니다.',
]);
}
/**
* 순서 변경
*/
public function reorder(Request $request, int $formulaId): JsonResponse
{
$validated = $request->validate([
'mapping_ids' => 'required|array',
'mapping_ids.*' => 'integer',
]);
// 모든 매핑이 해당 수식에 속하는지 확인
foreach ($validated['mapping_ids'] as $mappingId) {
if (! $this->mappingService->belongsToFormula($mappingId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '유효하지 않은 매핑 ID가 포함되어 있습니다.',
], 400);
}
}
$this->mappingService->reorder($validated['mapping_ids']);
return response()->json([
'success' => true,
'message' => '순서가 변경되었습니다.',
]);
}
}