fix : 메뉴 모델 및 일부 서비스파일 response 오류 수정

This commit is contained in:
2025-08-16 04:16:34 +09:00
parent 73d06e03b0
commit 6f1842181e
5 changed files with 312 additions and 211 deletions

View File

@@ -2,12 +2,12 @@
namespace App\Services\Authz;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ApiResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
use App\Helpers\ApiResponse;
class RoleService
{
@@ -26,13 +26,13 @@ public static function index(array $params = [])
->where('guard_name', self::$guard);
if ($q !== '') {
$query->where(function($w) use ($q) {
$query->where(function ($w) use ($q) {
$w->where('name', 'like', "%{$q}%")
->orWhere('description', 'like', "%{$q}%");
});
}
$list = $query->orderBy('id','desc')
$list = $query->orderByDesc('id')
->paginate($size, ['*'], 'page', $page);
return ApiResponse::response('result', $list);
@@ -44,10 +44,10 @@ public static function store(array $params = [])
$tenantId = (int) app('tenant_id');
$v = Validator::make($params, [
'name' => [
'required','string','max:100',
Rule::unique('roles','name')->where(fn($q)=>$q
->where('tenant_id',$tenantId)
'name' => [
'required', 'string', 'max:100',
Rule::unique('roles', 'name')->where(fn($q) => $q
->where('tenant_id', $tenantId)
->where('guard_name', self::$guard)),
],
'description' => 'nullable|string|max:255',
@@ -57,13 +57,14 @@ public static function store(array $params = [])
return ApiResponse::error($v->errors()->first(), 422);
}
// Spatie 팀(테넌트) 컨텍스트
app(PermissionRegistrar::class)->setPermissionsTeamId($tenantId);
$role = Role::create([
'tenant_id' => $tenantId,
'guard_name' => self::$guard,
'name' => $v->validated()['name'],
'description'=> $params['description'] ?? null,
'tenant_id' => $tenantId,
'guard_name' => self::$guard,
'name' => $v->validated()['name'],
'description' => $params['description'] ?? null,
]);
return ApiResponse::response('result', $role);
@@ -90,7 +91,7 @@ public static function update(int $id, array $params = [])
{
$tenantId = (int) app('tenant_id');
$role = Role::where('tenant_id',$tenantId)
$role = Role::where('tenant_id', $tenantId)
->where('guard_name', self::$guard)
->find($id);
@@ -112,18 +113,17 @@ public static function update(int $id, array $params = [])
return ApiResponse::error($v->errors()->first(), 422);
}
$payload = $v->validated();
$role->fill($payload)->save();
$role->fill($v->validated())->save();
return ApiResponse::response('result', $role);
return ApiResponse::response('result', $role->fresh());
}
/** 삭제 (현재는 하드삭제) */
/** 삭제 (하드삭제) */
public static function destroy(int $id)
{
$tenantId = (int) app('tenant_id');
$role = Role::where('tenant_id',$tenantId)
$role = Role::where('tenant_id', $tenantId)
->where('guard_name', self::$guard)
->find($id);
@@ -132,8 +132,7 @@ public static function destroy(int $id)
}
DB::transaction(function () use ($role) {
// 연관 피벗은 스파티가 onDelete cascade 하므로 기본 동작으로 OK
$role->delete(); // ※ 기본 Spatie Role은 SoftDeletes 미사용 → 하드 삭제
$role->delete(); // Spatie Role 기본: soft delete 없음
});
return ApiResponse::response('success');