itemService->getItemsByFormula($formulaId); return response()->json([ 'success' => true, 'data' => $items, ]); } /** * 품목 상세 조회 */ public function show(int $formulaId, int $itemId): JsonResponse { // 수식 소속 확인 if (! $this->itemService->belongsToFormula($itemId, $formulaId)) { return response()->json([ 'success' => false, 'message' => '해당 수식에 속하지 않는 품목입니다.', ], 404); } $item = $this->itemService->getItemById($itemId); return response()->json([ 'success' => true, 'data' => $item, ]); } /** * 품목 생성 */ public function store(Request $request, int $formulaId): JsonResponse { $validated = $request->validate([ 'item_code' => 'required|string|max:50', 'item_name' => 'required|string|max:200', 'specification' => 'nullable|string|max:200', 'unit' => 'nullable|string|max:20', 'quantity_formula' => 'nullable|string|max:500', 'unit_price_formula' => 'nullable|string|max:500', 'sort_order' => 'nullable|integer|min:0', ]); $item = $this->itemService->createItem($formulaId, $validated); return response()->json([ 'success' => true, 'message' => '품목이 추가되었습니다.', 'data' => $item, ]); } /** * 품목 수정 */ public function update(Request $request, int $formulaId, int $itemId): JsonResponse { // 수식 소속 확인 if (! $this->itemService->belongsToFormula($itemId, $formulaId)) { return response()->json([ 'success' => false, 'message' => '해당 수식에 속하지 않는 품목입니다.', ], 404); } $validated = $request->validate([ 'item_code' => 'nullable|string|max:50', 'item_name' => 'nullable|string|max:200', 'specification' => 'nullable|string|max:200', 'unit' => 'nullable|string|max:20', 'quantity_formula' => 'nullable|string|max:500', 'unit_price_formula' => 'nullable|string|max:500', ]); $item = $this->itemService->updateItem($itemId, $validated); return response()->json([ 'success' => true, 'message' => '품목이 수정되었습니다.', 'data' => $item, ]); } /** * 품목 삭제 */ public function destroy(int $formulaId, int $itemId): JsonResponse { // 수식 소속 확인 if (! $this->itemService->belongsToFormula($itemId, $formulaId)) { return response()->json([ 'success' => false, 'message' => '해당 수식에 속하지 않는 품목입니다.', ], 404); } $this->itemService->deleteItem($itemId); return response()->json([ 'success' => true, 'message' => '품목이 삭제되었습니다.', ]); } /** * 순서 변경 */ public function reorder(Request $request, int $formulaId): JsonResponse { $validated = $request->validate([ 'item_ids' => 'required|array', 'item_ids.*' => 'integer', ]); // 모든 품목이 해당 수식에 속하는지 확인 foreach ($validated['item_ids'] as $itemId) { if (! $this->itemService->belongsToFormula($itemId, $formulaId)) { return response()->json([ 'success' => false, 'message' => '유효하지 않은 품목 ID가 포함되어 있습니다.', ], 400); } } $this->itemService->reorder($validated['item_ids']); return response()->json([ 'success' => true, 'message' => '순서가 변경되었습니다.', ]); } }