From 6d873bcbcd5630193b02beb2e90b59cd2c738b88 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 27 Sep 2024 18:21:27 +0200 Subject: [PATCH] Implement listing of collections --- bin/paramcli | 29 ++++++++++++++++++++++++++--- src/Daemon.php | 6 ++++++ src/Storage/Database.php | 8 ++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/bin/paramcli b/bin/paramcli index 1d97f9f..0dbdeff 100755 --- a/bin/paramcli +++ b/bin/paramcli @@ -1,6 +1,8 @@ #!/usr/bin/env php post("http://{$opts->server}/{$url}", $headers, $body??''); } +function action_list(object $opts) { + http_get("", [])->then( + function ($response) { + echo join("\n", json_decode($response->getBody()->getContents(),true))."\n"; + }, + function ($error) { + echo $error->getMessage()."\n"; + } + ); + +} + function action_get(object $opts) { $collection = array_shift($opts->args); if (!$collection) { @@ -179,9 +194,13 @@ function action_set(object $opts) { } if ($opts->from || $opts->until) { - $vfrom = $opts->from ? [ 'from' => $opts->from->format('Y-m-d H:i:s P') ] : null; - $vuntil = $opts->until ? [ 'until' => $opts->until->format('Y-m-d H:i:s P') ] : null; - $op['validity'] = [ ...$vfrom, ...$vuntil ]; + $op['validity'] = []; + if ($opts->from) { + $op['validity']['from'] = $opts->from->format('Y-m-d H:i:s P'); + } + if ($opts->until) { + $op['validity']['until'] = $opts->until->format('Y-m-d H:i:s P'); + } } $body[$key] = $op; } @@ -199,6 +218,10 @@ $action = array_shift($opts->args); switch ($action) { + case 'list': + action_list($opts); + break; + case 'get': action_get($opts); break; diff --git a/src/Daemon.php b/src/Daemon.php index be3ea7e..3133084 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -72,6 +72,12 @@ class Daemon $collectionName = array_shift($paths); $collectionOp = array_shift($paths); + if ($collectionName == null) { + $collections = $this->db->getCollectionNames(); + return Response::json($collections); + } + + switch ($collectionOp) { case null: diff --git a/src/Storage/Database.php b/src/Storage/Database.php index 8eca121..ad69f8b 100644 --- a/src/Storage/Database.php +++ b/src/Storage/Database.php @@ -72,6 +72,14 @@ class Database return new Collection($this->pdo, $id); } + public function getCollectionNames(): array + { + $query = $this->pdo->prepare("SELECT * FROM collections ORDER BY name ASC"); + $query->execute(); + $rows = $query->fetchAll(PDO::FETCH_ASSOC); + + return array_column($rows, "name"); + } public function createCollection(string $name, array $meta = []): Collection {