Commit 3f9c97bf authored by Ryo Yamashita's avatar Ryo Yamashita Committed by Yas Naoi
Browse files

Issue #3258115 by Ryo Yamashita, yas: Add a REST API to count Cloud Store Resources

parent 8d998e19
Loading
Loading
Loading
Loading
+9 −0
Changes for cloud.routing.yml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -282,6 +282,15 @@ entity.cloud_store.list_update:
        type: 'entity:cloud_store_type'
        converter: 'paramconverter.entity'

# Clour Store REST API.
entity.cloud_store.entity.count:
  path: '/cloud_dashboard/{entity_type_id}/entity/count'
  defaults:
    _controller: '\Drupal\cloud\Controller\CloudStoreController::getEntityCount'
  methods: [GET]
  requirements:
    _permission: 'list cloud store'

# Cloud Admin Settings.
cloud.settings:
  path: '/admin/config/services/cloud/settings'
+21 −0
Changes for src/Controller/CloudStoreController.php: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Route;

/**
@@ -329,4 +330,24 @@ class CloudStoreController extends ControllerBase implements ContainerInjectionI
    return AccessResult::neutral();
  }

  /**
   * Get count of Cloud Store Resources.
   *
   * @param string $bundle
   *   The bundle ID.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON response.
   */
  public function getEntityCount(string $bundle): JsonResponse {
    $ids = $this->entityTypeManager()
      ->getStorage('cloud_store')
      ->getQuery()
      ->accessCheck(TRUE)
      ->condition('type', $bundle)
      ->execute();

    return new JsonResponse(['count' => count($ids)]);
  }

}