refactor: CategoryField API를 SAM API Rules에 맞게 리팩토링
- FormRequest 패턴 적용 (CategoryFieldStoreRequest, CategoryFieldUpdateRequest)
- Service에서 Validator::make() 제거
- Controller 메시지 i18n 키로 변경 (__('message.category_field.*'))
- is_required 타입을 'Y'/'N' → boolean으로 통일
- Swagger 스키마 is_required boolean 타입으로 업데이트
- Model scopeRequired() boolean 조건으로 변경
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
use App\Models\Commons\Category;
|
||||
use App\Models\Commons\CategoryField;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator; // 가정: Eloquent 모델 경로
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
class CategoryFieldService extends Service
|
||||
@@ -32,31 +31,22 @@ public function store(int $categoryId, array $data)
|
||||
|
||||
$this->assertCategoryExists($tenantId, $categoryId);
|
||||
|
||||
$v = Validator::make($data, [
|
||||
'field_key' => 'required|string|max:30|alpha_dash',
|
||||
'field_name' => 'required|string|max:100',
|
||||
'field_type' => 'required|string|max:20',
|
||||
'is_required' => 'nullable|in:Y,N',
|
||||
'sort_order' => 'nullable|integer|min:0',
|
||||
'default_value' => 'nullable|string|max:100',
|
||||
'options' => 'nullable|json',
|
||||
'description' => 'nullable|string|max:255',
|
||||
]);
|
||||
$payload = $v->validate();
|
||||
// FormRequest에서 이미 검증됨
|
||||
$payload = $data;
|
||||
|
||||
// 카테고리 내 field_key 유니크 검증
|
||||
$exists = CategoryField::query()
|
||||
->where(compact('tenant_id'))
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('category_id', $categoryId)
|
||||
->where('field_key', $payload['field_key'])
|
||||
->exists();
|
||||
if ($exists) {
|
||||
throw new BadRequestHttpException(__('error.duplicate_key')); // ko/error.php에 매핑
|
||||
throw new BadRequestHttpException(__('error.duplicate_key'));
|
||||
}
|
||||
|
||||
$payload['tenant_id'] = $tenantId;
|
||||
$payload['category_id'] = $categoryId;
|
||||
$payload['is_required'] = $payload['is_required'] ?? 'N';
|
||||
$payload['is_required'] = $payload['is_required'] ?? false;
|
||||
$payload['sort_order'] = $payload['sort_order'] ?? 0;
|
||||
$payload['created_by'] = $userId;
|
||||
|
||||
@@ -91,17 +81,8 @@ public function update(int $fieldId, array $data)
|
||||
throw new BadRequestHttpException(__('error.not_found'));
|
||||
}
|
||||
|
||||
$v = Validator::make($data, [
|
||||
'field_key' => 'sometimes|string|max:30|alpha_dash',
|
||||
'field_name' => 'sometimes|string|max:100',
|
||||
'field_type' => 'sometimes|string|max:20',
|
||||
'is_required' => 'sometimes|in:Y,N',
|
||||
'sort_order' => 'sometimes|integer|min:0',
|
||||
'default_value' => 'nullable|string|max:100',
|
||||
'options' => 'nullable|json',
|
||||
'description' => 'nullable|string|max:255',
|
||||
]);
|
||||
$payload = $v->validate();
|
||||
// FormRequest에서 이미 검증됨
|
||||
$payload = $data;
|
||||
|
||||
if (isset($payload['field_key']) && $payload['field_key'] !== $field->field_key) {
|
||||
$dup = CategoryField::query()
|
||||
@@ -170,19 +151,11 @@ public function bulkUpsert(int $categoryId, array $items): array
|
||||
$result = ['created' => 0, 'updated' => 0];
|
||||
|
||||
DB::transaction(function () use ($tenantId, $userId, $categoryId, $items, &$result) {
|
||||
foreach ($items as $it) {
|
||||
$v = Validator::make($it, [
|
||||
'id' => 'nullable|integer',
|
||||
'field_key' => 'sometimes|required_without:id|string|max:30|alpha_dash',
|
||||
'field_name' => 'required|string|max:100',
|
||||
'field_type' => 'required|string|max:20',
|
||||
'is_required' => 'nullable|in:Y,N',
|
||||
'sort_order' => 'nullable|integer|min:0',
|
||||
'default_value' => 'nullable|string|max:100',
|
||||
'options' => 'nullable|json',
|
||||
'description' => 'nullable|string|max:255',
|
||||
]);
|
||||
$payload = $v->validate();
|
||||
foreach ($items as $payload) {
|
||||
// 기본적인 검증 (FormRequest가 배열 내부까지는 검증 못함)
|
||||
if (! isset($payload['field_name'], $payload['field_type'])) {
|
||||
throw new BadRequestHttpException(__('error.invalid_payload'));
|
||||
}
|
||||
|
||||
if (! empty($payload['id'])) {
|
||||
$model = CategoryField::query()
|
||||
@@ -224,7 +197,7 @@ public function bulkUpsert(int $categoryId, array $items): array
|
||||
|
||||
$payload['tenant_id'] = $tenantId;
|
||||
$payload['category_id'] = $categoryId;
|
||||
$payload['is_required'] = $payload['is_required'] ?? 'N';
|
||||
$payload['is_required'] = $payload['is_required'] ?? false;
|
||||
$payload['sort_order'] = $payload['sort_order'] ?? 0;
|
||||
$payload['created_by'] = $userId;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user