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

152 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\QuoteFormulaRangeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class QuoteFormulaRangeController extends Controller
{
public function __construct(
private readonly QuoteFormulaRangeService $rangeService
) {}
/**
* 범위 목록 조회
*/
public function index(int $formulaId): JsonResponse
{
$ranges = $this->rangeService->getRangesByFormula($formulaId);
return response()->json([
'success' => true,
'data' => $ranges,
]);
}
/**
* 범위 상세 조회
*/
public function show(int $formulaId, int $rangeId): JsonResponse
{
// 수식 소속 확인
if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 범위입니다.',
], 404);
}
$range = $this->rangeService->getRangeById($rangeId);
return response()->json([
'success' => true,
'data' => $range,
]);
}
/**
* 범위 생성
*/
public function store(Request $request, int $formulaId): JsonResponse
{
$validated = $request->validate([
'min_value' => 'nullable|numeric',
'max_value' => 'nullable|numeric',
'condition_variable' => 'required|string|max:50',
'result_value' => 'required|string',
'result_type' => 'nullable|in:fixed,formula',
'sort_order' => 'nullable|integer|min:0',
]);
$range = $this->rangeService->createRange($formulaId, $validated);
return response()->json([
'success' => true,
'message' => '범위가 추가되었습니다.',
'data' => $range,
]);
}
/**
* 범위 수정
*/
public function update(Request $request, int $formulaId, int $rangeId): JsonResponse
{
// 수식 소속 확인
if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 범위입니다.',
], 404);
}
$validated = $request->validate([
'min_value' => 'nullable|numeric',
'max_value' => 'nullable|numeric',
'condition_variable' => 'nullable|string|max:50',
'result_value' => 'nullable|string',
'result_type' => 'nullable|in:fixed,formula',
]);
$range = $this->rangeService->updateRange($rangeId, $validated);
return response()->json([
'success' => true,
'message' => '범위가 수정되었습니다.',
'data' => $range,
]);
}
/**
* 범위 삭제
*/
public function destroy(int $formulaId, int $rangeId): JsonResponse
{
// 수식 소속 확인
if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '해당 수식에 속하지 않는 범위입니다.',
], 404);
}
$this->rangeService->deleteRange($rangeId);
return response()->json([
'success' => true,
'message' => '범위가 삭제되었습니다.',
]);
}
/**
* 순서 변경
*/
public function reorder(Request $request, int $formulaId): JsonResponse
{
$validated = $request->validate([
'range_ids' => 'required|array',
'range_ids.*' => 'integer',
]);
// 모든 범위가 해당 수식에 속하는지 확인
foreach ($validated['range_ids'] as $rangeId) {
if (! $this->rangeService->belongsToFormula($rangeId, $formulaId)) {
return response()->json([
'success' => false,
'message' => '유효하지 않은 범위 ID가 포함되어 있습니다.',
], 400);
}
}
$this->rangeService->reorder($validated['range_ids']);
return response()->json([
'success' => true,
'message' => '순서가 변경되었습니다.',
]);
}
}