Files
sam-manage/app/Http/Controllers/Api/Admin/Quote/QuoteFormulaMappingController.php

150 lines
4.4 KiB
PHP
Raw Normal View History

<?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' => '순서가 변경되었습니다.',
]);
}
}