fix : 결과 전달시 두번 래핑되는 부분 수정
- 컨트롤러와 서비스에서 각각 래핑 후 결과 전달됨으로 이중 래핑되고 있음 -> 서비스에서 래핑하는 부분을 컨트롤러로 옮겨서 컨트롤러에서만 한번 래핑하는 걸로 수정
This commit is contained in:
@@ -37,7 +37,7 @@ public static function index(array $params)
|
||||
$q->orderBy('parent_id')->orderBy('sort_order');
|
||||
|
||||
// Builder 그대로 전달해야 쿼리로그/표준응답 형식 유지
|
||||
return ApiResponse::response('get', $q);
|
||||
return $q->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,15 +49,12 @@ public static function show(array $params)
|
||||
$tenantId = self::tenantId();
|
||||
|
||||
if (!$id) {
|
||||
return ApiResponse::error('id가 필요합니다.', 400);
|
||||
return ['error' => 'id가 필요합니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$q = Menu::withShared($tenantId)->where('id', $id);
|
||||
|
||||
// first 쿼리를 ApiResponse에 위임 (존재X면 null 반환)
|
||||
$res = ApiResponse::response('first', $q);
|
||||
$res = Menu::withShared($tenantId)->find($id);
|
||||
if (empty($res['data'])) {
|
||||
return ApiResponse::error('Menu not found', 404);
|
||||
return ['error' => 'Menu not found', 'code' => 404];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
@@ -83,7 +80,7 @@ public static function store(array $params)
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
$data = $v->validated();
|
||||
|
||||
@@ -103,7 +100,7 @@ public static function store(array $params)
|
||||
$menu->save();
|
||||
|
||||
// 생성 결과를 그대로 전달
|
||||
return ApiResponse::response('result', $menu->fresh());
|
||||
return $menu->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +113,7 @@ public static function update(array $params)
|
||||
$userId = self::actorId();
|
||||
|
||||
if (!$id) {
|
||||
return ApiResponse::error('id가 필요합니다.', 400);
|
||||
return ['error' => 'id가 필요합니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$v = Validator::make($params, [
|
||||
@@ -132,13 +129,13 @@ public static function update(array $params)
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
$data = $v->validated();
|
||||
|
||||
$menu = Menu::withShared($tenantId)->where('id', $id)->first();
|
||||
if (!$menu) {
|
||||
return ApiResponse::error('Menu not found', 404);
|
||||
return ['error' => 'Menu not found', 'code' => 404];
|
||||
}
|
||||
|
||||
$update = Arr::only($data, [
|
||||
@@ -147,13 +144,13 @@ public static function update(array $params)
|
||||
$update = array_filter($update, fn($v) => !is_null($v));
|
||||
|
||||
if (empty($update)) {
|
||||
return ApiResponse::error('수정할 데이터가 없습니다.', 400);
|
||||
return ['error' => '수정할 데이터가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$update['updated_by'] = $userId;
|
||||
$menu->fill($update)->save();
|
||||
|
||||
return ApiResponse::response('result', $menu->fresh());
|
||||
return $menu->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,19 +163,19 @@ public static function destroy(array $params)
|
||||
$userId = self::actorId();
|
||||
|
||||
if (!$id) {
|
||||
return ApiResponse::error('id가 필요합니다.', 400);
|
||||
return ['error' => 'id가 필요합니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$menu = Menu::withShared($tenantId)->where('id', $id)->first();
|
||||
if (!$menu) {
|
||||
return ApiResponse::error('Menu not found', 404);
|
||||
return ['error' => 'Menu not found', 'code' => 404];
|
||||
}
|
||||
|
||||
$menu->deleted_by = $userId;
|
||||
$menu->save();
|
||||
$menu->delete();
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +185,7 @@ public static function destroy(array $params)
|
||||
public static function reorder(array $params)
|
||||
{
|
||||
if (!is_array($params) || empty($params)) {
|
||||
return ApiResponse::error('유효한 정렬 목록이 필요합니다.', 422);
|
||||
return ['error' => '유효한 정렬 목록이 필요합니다.', 'code' => 422];
|
||||
}
|
||||
$tenantId = self::tenantId();
|
||||
|
||||
@@ -204,7 +201,7 @@ public static function reorder(array $params)
|
||||
}
|
||||
});
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +214,7 @@ public static function toggle(array $params)
|
||||
$userId = self::actorId();
|
||||
|
||||
if (!$id) {
|
||||
return ApiResponse::error('id가 필요합니다.', 400);
|
||||
return ['error' => 'id가 필요합니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$payload = array_filter([
|
||||
@@ -227,17 +224,17 @@ public static function toggle(array $params)
|
||||
], fn($v) => !is_null($v));
|
||||
|
||||
if (empty($payload)) {
|
||||
return ApiResponse::error('변경할 필드가 없습니다.', 422);
|
||||
return ['error' => '변경할 필드가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$menu = Menu::withShared($tenantId)->find($id);
|
||||
if (!$menu) {
|
||||
return ApiResponse::error('Menu not found', 404);
|
||||
return ['error' => 'Menu not found', 'code' => 404];
|
||||
}
|
||||
|
||||
$payload['updated_by'] = $userId;
|
||||
$menu->fill($payload)->save();
|
||||
|
||||
return ApiResponse::response('result', $menu->fresh());
|
||||
return $menu->fresh();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user