queryService->listRoles($request->validated()); return $this->success([ 'roles' => RoleResource::collection($roles->items()), 'meta' => [ 'total' => $roles->total(), 'per_page' => $roles->perPage(), 'current_page' => $roles->currentPage(), 'last_page' => $roles->lastPage(), ], ]); } public function showRole(int $roleId): JsonResponse { $role = Role::query()->find($roleId); if (! $role) { return $this->error('Role not found.', Response::HTTP_NOT_FOUND); } return $this->success([ 'role' => new RoleResource($role), ]); } public function storeRole(RoleStoreRequest $request): JsonResponse { try { $role = $this->roleCrudService->create($request->validated()); } catch (\Throwable $e) { Log::error('Role store failed: '.$e->getMessage()); return $this->error('Failed to create role.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success([ 'role' => new RoleResource($role), ], 'Role created successfully.', Response::HTTP_CREATED); } public function updateRole(RoleUpdateRequest $request, int $roleId): JsonResponse { $role = Role::query()->find($roleId); if (! $role) { return $this->error('Role not found.', Response::HTTP_NOT_FOUND); } try { $role = $this->roleCrudService->update($role, $request->validated()); } catch (\Throwable $e) { Log::error('Role update failed: '.$e->getMessage()); return $this->error('Failed to update role.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success([ 'role' => new RoleResource($role), ], 'Role updated successfully.'); } public function deleteRole(int $roleId): JsonResponse { $role = Role::query()->find($roleId); if (! $role) { return $this->error('Role not found.', Response::HTTP_NOT_FOUND); } try { $this->roleCrudService->delete($role); } catch (\Throwable $e) { Log::error('Role delete failed: '.$e->getMessage()); return $this->error('Failed to delete role.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success(null, 'Role deleted successfully.'); } public function users(UserRoleListRequest $request): JsonResponse { $users = $this->queryService->listUsersWithRoles($request->validated()); return $this->success([ 'users' => UserWithRolesResource::collection($users->items()), 'meta' => [ 'total' => $users->total(), 'per_page' => $users->perPage(), 'current_page' => $users->currentPage(), 'last_page' => $users->lastPage(), ], ]); } public function assignRoles(AssignUserRolesRequest $request, int $userId): JsonResponse { try { $result = $this->assignmentService->assignRoles( $userId, (array) ($request->validated()['role_ids'] ?? []), (int) (auth()->id() ?? 0) ); } catch (\Throwable $e) { Log::error('Assign roles failed: '.$e->getMessage()); return $this->error('Failed to update roles.', Response::HTTP_INTERNAL_SERVER_ERROR); } if (! ($result['ok'] ?? false)) { return $this->error($result['message'] ?? 'Failed to update roles.', Response::HTTP_UNPROCESSABLE_ENTITY); } return $this->success([ 'role_names' => $result['role_names'] ?? [], ], 'Roles updated successfully.'); } public function permissions(): JsonResponse { $permissions = $this->permissionService->listPermissions(); return $this->success([ 'permissions' => PermissionResource::collection($permissions), ]); } public function showPermission(int $permissionId): JsonResponse { $permission = Permission::query()->find($permissionId); if (! $permission) { return $this->error('Permission not found.', Response::HTTP_NOT_FOUND); } return $this->success([ 'permission' => new PermissionResource($permission), ]); } public function storePermission(PermissionStoreRequest $request): JsonResponse { try { $permission = $this->permissionCrudService->create($request->validated()); } catch (\Throwable $e) { Log::error('Permission store failed: '.$e->getMessage()); return $this->error('Failed to create permission.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success([ 'permission' => new PermissionResource($permission), ], 'Permission created successfully.', Response::HTTP_CREATED); } public function updatePermission(PermissionUpdateRequest $request, int $permissionId): JsonResponse { $permission = Permission::query()->find($permissionId); if (! $permission) { return $this->error('Permission not found.', Response::HTTP_NOT_FOUND); } try { $permission = $this->permissionCrudService->update($permission, $request->validated()); } catch (\Throwable $e) { Log::error('Permission update failed: '.$e->getMessage()); return $this->error('Failed to update permission.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success([ 'permission' => new PermissionResource($permission), ], 'Permission updated successfully.'); } public function deletePermission(int $permissionId): JsonResponse { $permission = Permission::query()->find($permissionId); if (! $permission) { return $this->error('Permission not found.', Response::HTTP_NOT_FOUND); } try { $this->permissionCrudService->delete($permission); } catch (\Throwable $e) { Log::error('Permission delete failed: '.$e->getMessage()); return $this->error('Failed to delete permission.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success(null, 'Permission deleted successfully.'); } public function rolePermissions(int $roleId): JsonResponse { $rows = $this->permissionService->listRolePermissions($roleId); return $this->success([ 'permissions' => RolePermissionResource::collection($rows), ]); } public function updateRolePermissions(RolePermissionUpdateRequest $request, int $roleId): JsonResponse { try { $this->permissionService->saveRolePermissions($roleId, $request->validated()['permissions'] ?? []); } catch (\Throwable $e) { Log::error('Role permissions update failed: '.$e->getMessage()); return $this->error('Failed to update role permissions.', Response::HTTP_INTERNAL_SERVER_ERROR); } return $this->success(null, 'Permissions saved successfully.'); } }